I'm stumped and need some help! I understand what this error message means, but I don't know why I am getting it. Below is my code.
The strFilter value I'm sending to this sub is "11/27/2011". The Period Start field is a date field, so I'm wrapping the passed date in hash marks (#). When I copy the value of strSQL to the SQL of a new query in Access (outside of VBA), it works just fine without any changes and returns several records.
When I removed the hash marks from the code, I did not receive an error. However, my query resulted in no records being selected.
The strFilter value I'm sending to this sub is "11/27/2011". The Period Start field is a date field, so I'm wrapping the passed date in hash marks (#). When I copy the value of strSQL to the SQL of a new query in Access (outside of VBA), it works just fine without any changes and returns several records.
When I removed the hash marks from the code, I did not receive an error. However, my query resulted in no records being selected.
Code:
[FONT=Calibri][SIZE=3]Public Sub MoveDeferredToExtract(Optional strFilter As String)[/SIZE][/FONT]
[FONT=Calibri][SIZE=3] Dim strMsg As String, strSQL As String, db As Database, strWhereClause As String[/SIZE][/FONT]
[FONT=Calibri][SIZE=3] On Error GoTo Err_MoveDeferredToExtract[/SIZE][/FONT]
[FONT=Calibri][SIZE=3] Set db = CurrentDb[/SIZE][/FONT]
[FONT=Calibri][SIZE=3] If IsNull(strFilter) Then[/SIZE][/FONT]
[FONT=Calibri][SIZE=3] strWhereClause = ";"[/SIZE][/FONT]
[FONT=Calibri][SIZE=3] Else[/SIZE][/FONT]
[FONT=Calibri][SIZE=3] strWhereClause = " WHERE tbl_Deferred.[Period Start] = #" & strFilter & "#;"[/SIZE][/FONT]
[FONT=Calibri][SIZE=3] End If[/SIZE][/FONT]
[FONT=Calibri][SIZE=3] strSQL = CurrentDb.QueryDefs("qry_DeferredToExtract").SQL[/SIZE][/FONT]
[FONT=Calibri][SIZE=3] strSQL = Replace(strSQL, ";", strWhereClause)[/SIZE][/FONT]
[FONT=Calibri][SIZE=3] db.Execute strSQL, dbFailOnError[/SIZE][/FONT]
[FONT=Calibri][SIZE=3]Exit_MoveDeferredToExtract:[/SIZE][/FONT]
[FONT=Calibri][SIZE=3] Set db = Nothing[/SIZE][/FONT]
[FONT=Calibri][SIZE=3] Exit Sub[/SIZE][/FONT]
[FONT=Calibri][SIZE=3]Err_MoveDeferredToExtract:[/SIZE][/FONT]
[FONT=Calibri][SIZE=3] 'Build an error message.[/SIZE][/FONT]
[FONT=Calibri][SIZE=3] strMsg = ("An error within the program has occurred. Error number: " & _[/SIZE][/FONT]
[FONT=Calibri][SIZE=3] Err.Number & "; Error description: " & Err.Description & vbNewLine & _[/SIZE][/FONT]
[FONT=Calibri][SIZE=3] vbNewLine & "Error occurred at: '" & strProc & ", " & strCodeLocn & "'.")[/SIZE][/FONT]
[FONT=Calibri][SIZE=3] ' Display the error message.[/SIZE][/FONT]
[FONT=Calibri][SIZE=3] MsgBox strMsg, vbCritical, "Critical Error"[/SIZE][/FONT]
[FONT=Calibri][SIZE=3] Resume Exit_MoveDeferredToExtract[/SIZE][/FONT]
[FONT=Calibri][SIZE=3]End Sub[/SIZE][/FONT]