I'm new to VBA and am trying to get a macro I'm writing to check if there is already a pre-existing record corresponding to the date/shift combination of the new record to be added. To do so, I'm trying to use DLookup to search through the database and only allow the new record if DLookup returns a Null, which should happen unless it finds a record which has both the date and shift matching the new data (I'm using a composite primary key using these two pieces of data). Alas, as the title says, I keep getting run-time error 424 and when I debug it highlights
lookTest = Access.DLookup("Shift", "trialTable", "[Date of Production]=#" & shiftDate & "#" & "And [Shift]='" & currentShift & "'")
as the problem line. ("Shift" and "Date of Production" are the column headings for the two columns used as the composite key, "trialTable" is the table I'm working with, "shiftDate" is a date and "currentShift" is a string) As far as I can tell my syntax is correct, so I have no idea where I'm going wrong with this at this point (It's probably something dumb and obvious, I'm sure). If it helps, below is the rest of the code leading up to that point in case I've screwed something up earlier on and the computer just isn't realizing it until it hits that line. Any assistance is greatly appreciated, thanks!
Sub DatabaseUpdate()
' exports data from the active spreadsheet to a table in an Access database
' this procedure must be edited before use
Dim cn As ADODB.Connection, rs As ADODB.Recordset
' connect to the Access database
Set cn = New ADODB.Connection
cn.Open "Provider=Microsoft.Ace.OLEDB.12.0; " & "Data Source=H:\TestingDatabase.accdb;"
' open a recordset
Set rs = New ADODB.Recordset
rs.Open "trialTable", cn, adOpenKeyset, adLockOptimistic, adCmdTable
' all records on a table
With rs
.AddNew 'create a new record
'Ask the user which shift the report is for.
Dim currentShift As String
Dim shiftDate As Date
Dim shiftCheck As Boolean
shiftCheck = False
' Request and check if the user has entered in a valid shift. If so, continue. If not, inform them and then repeat the request for the shift identity.
Do While shiftCheck = False
currentShift = UCase(InputBox("Which shift is this entry for? Please input only A, B, or C. (not case-sensitive)"))
If currentShift = "A" Or currentShift = "B" Or currentShift = "C" Then
shiftCheck = True
Else
wrongLetterWarning = MsgBox("Sorry, that is not an accepted response (A, B, or C). Please try again.", vbOKOnly)
End If
Loop
' Request the date the shift occured on. MM/DD/YYYY format is important as the date and shift together form the database's primary key.
shiftDate = InputBox("On which date did this shift occur? Please use MM/DD/YYYY format.")
'Check to make sure that there isn't already a pre-existing record for the date/shift combination.
Dim lookTest As Variant
lookTest = Access.DLookup("Shift", "trialTable", "[Date of Production]=#" & shiftDate & "#" & "And [Shift]='" & currentShift & "'")