Insert default value into ms access table from code - ms-access-2007

I want to insert OfficerID of the officer currently logged in as a default value into the table T_FileCases.
Already in the login form I have a field that holds officer's name (UserName) when they are logged in successfully but when this happens I also want to automatically insert their OfficerID into the table T_FileCases automatically, instead of using a combo box for selecting officers name when creating a new case.
Me.Hold_User_ID = Nz(DLookup("OfficerID", "Table_Officers", "Names='" & Me.UserName & "' and NRC='" & Me.PWD & "'"), -1)
' check to see if we have a good login
If Me.Hold_User_ID = -1 Then
MsgBox "Sorry boss, you typed invalid username or password.", vbExclamation
Exit Sub
Else
' load the users access level into the holder form field
Me.hold_level = DLookup("Access_Level", "Table_Officers", "Names='" & Me.UserName & "' and NRC='" & Me.PWD & "'")
End If

On the Before Insert event of the data entry form you could create an event procedure to check for the UserID and populate the field with the appropriate value.
This would require that the UserID is able to be retrieved at this point, either by keeping the login form hidden in the background or by assigning the value from the login form to a Global variable at the point of validation.

Related

I want to move a data entry form in microsoft access to an existing record if a matching record already exists

I have a unique index of fields in my table in microsoft access. The fields are Shift, Operator, Date_Field, and Machine. I have a data entry form with combobox selections for these fields, except for the date which autopopulates today's date. I want to be able to navigate the form te the record that matches the existing Shift/Operator/Date/Machine combo if it already exists. I have a DLookup function that checks to see if such a record exists already, but now I need to know how to change the form so it is entering the data on that record instead of a new one.
Here's what I have so far. It is being activated in the AfterUdate of one of the last combobox in the tab order.
Dim int_ID As Integer
With Me
'checks if duplicate record exists and stores it as int_ID variable
int_ID = Nz(DLookup("ID", "Tracking", "Shift= " & Val(.ShiftCbo) & " And Operator='" & .OpCbo.Column(1) & "' And Date_Field=#" & .DateBox & "# And Machine='" & .MachineCbo.Column(1) & "'"), 0)
End With
If int_ID <> 0 Then
'I need to know what to put here to take the form to the existing record.
End If
I've tried to use Cmd.GoToRecord but that doesn't work.
One of my favorite (and simplest) methods is using Me.Recordsource
Once you have your code working correctly which defines the filter criteria, try something like this:
Me.Recordsource= "select * from UnderlyingFormMainQuery where TextColumnName='" & varString & "' or NumericColumnName=" & intSomething
Me.Requery

Ensure Unique Records in Form (MS Access)

Using a Form in MS Access, I need to ensure only unique records are entered into the table.
It's unfortunately in a situation where I can't enforce unique records via the table's primary keys.
I have the following code in the BeforeUpdate for the Form, but it is not working. Any help would be greatly appreciated!
Private Sub Form_BeforeUpdate(Cancel As Integer)
If DCount("*", "[Role_Details]", "[Role] = " & Me.[ComboRole] & " AND [Session] = " & Me.[ComboSession]) > 0 Then
MsgBox "Duplicate!"
Cancel = True
Me.[ComboSession].SetFocus
Exit Sub
End If
End Sub
Note: The table name is "Role_Details". Field names are "Role" and "Session". With "ComboRole" and "ComboSession" being the Form Field Labels.
Any thoughts on where I've gone wrong here?
Updates##
When I open the DataSheet Form, it presents a popup box saying "Enter Parameter Value" and "frm_Role_Details.Session". I'm not sure why this is, but I can enter past it and open the Form.
Then, them i'm entering a record an error pops up saying "Run-time error '2465': Can't find the field '|1' referred to in your expression. Both Fields are Text Strings. I'm at a loss!
When concatenating in VBA, text fields require apostrophe delimiters for inputs.
If DCount("*", "[Role_Details]", "[Role] = '" & Me.[ComboRole] & "' AND [Session] = '" & Me.[ComboSession] & "'")> 0 Then
Date/Time fields use # delimiter.
Number fields do not use any delimiter.

How to make a field in access show the current value in the table, and accept input, but not update the table

I have been tasked with designing an Access front end for a SQL database I built. Unfortunately my VBA and Access knowledge are not as strong as they should be and I have run into a roadblock.
Currently I'm working on a script that will identify and flag high dollar assets that are entered into the database through the form. I have the update code written, but when I enter in a high value in the field the act of updating the value in the form and the update script itself clash and there is a 'Write Conflict'.
I can unbind the field in the form and that resoles the write conflict, but then the user first browsing to the record won't see the current value in the table.
My question: is there a way to have a field in a form show the value in a table, accept input, but not write the input to the table and leave the writing to the update query. If it helps I have included the update action I have written.
Private Sub Cost_AfterUpdate()
Dim CostCheck As String
CostCheck = "Update dbo_Purchasing SET Capitolasset = -1, COST = " & Me.Cost & " " & _
"Where itemMaster = " & Me.ItemMaster & " and " & Me.Cost & " >= " & 5000 & ""
DoCmd.RunSQL CostCheck
Me.Refresh
End Sub
It seems that you are trying to set the Capitolasset flag of the current record to -1 if the Cost entered is >= 5000. If so, you don't need to run an extra query, just update the Capitolasset value according to the Cost entered:
Private Sub Cost_AfterUpdate()
If IsNumeric(Me.Cost) Then
If Me.Cost >= 5000 Then
Me.Capitolasset = -1
Else
Me.Capitolasset = 0
End If
End If
End Sub
This will update the field before the record is saved.

VB.NET - Overwrite field in Acess Database

In my game I need to save the score after the user has logged in. The user who has logged in has their name displayed in a textbox on the form. I am having trouble overwriting the score field specific to the user.
My current code that saves the score and level to a new row and not the user who is logged in:
cmd.CommandText = "INSERT INTO tbl_User ([Score], [Level])VALUES(#Score, #Level) WHERE User='" & txtUser.Text & "'"
cmd.Parameters.AddWithValue("#Score", lblScore.Text) 'Adds score to table
cmd.Parameters.AddWithValue("#Level", lblLevel.Text) 'Adds level to table
Thanks
If you need to overwrite you need to use UPDATE command and not INSERT, like:
cmd.CommandText = "UPDATE tbl_User SET [Score]=#Score, [Level] = #Level WHERE User=#User"
cmd.Parameters.AddWithValue("#Score", lblScore.Text) 'Adds score to table
cmd.Parameters.AddWithValue("#Level", lblLevel.Text) 'Adds level to table
cmd.Parameters.AddWithValue("#User", txtUser.Text) 'Adds user to table

Delete A Row In a Listbox that Gets Data from Access database

So right now I have a listbox that gets data from an access database and prints it out like below:
When the user clicks on "Just Lose It" for example, and then clicks "Remove Song" button, how can I delete that entry from the database and then refresh the list box with the updated table.
I was thinking of creating an SQL DELETE Statement and then use a ADODB.Recordset to run that statement but I'm not sure how to tell the user clicked that entry.
Any ideas?
Use something like the following... add unique key if in hidden column
sqlStr = "DELETE FROM music WHERE [Title] = '" & music_lb.column(0) & "' AND [Artist] = '" & music_lb.column(1) & "' AND [Album] = '" & music_lb.column(2) & "';"