I want to change the SQL of my query depending on the users choice but this isn't happening. From what I read qdf.SQL = strSQL should be doing this. Any help to get me over this would be welcome.
tamus121
tamus121
Code:
Dim stDocName As String
stDocName = "PH4Part1"
'Check to see that Ending Date is later than Beginning Date.
If LastDateTxt < FirstDateTxt Then
MsgBox "The Last Date must be later than the First Date."
DoCmd.CancelEvent
FirstDateTxt.SetFocus
Exit Sub
End If
Dim strSQL As String
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Set db = CurrentDb
Set qdf = db.QueryDefs("NotPH4Part1Query")
strSQL = "SELECT CustomerRecordsQuery.Name, CustomerRecordsQuery.RegNo, CustomerRecordsQuery.AreaCode, CustomerRecordsQuery.Address1, CustomerRecordsQuery.Address2, CustomerRecordsQuery.Address3, CustomerRecordsQuery.Town, CustomerRecordsQuery.County, PH4Part1.Crecords, PH4Part1.Dpassports, PH4Part1.Etraceback, PH4Part1.Fisolation, PH4Part1.Girrigating, PH4Part1.Action, PH4Part1.Insp, PH4Part1.DateInsp, Right([RegNo],4) AS Exper1 FROM CustomerRecordsQuery LEFT JOIN PH4Part1 ON CustomerRecordsQuery.[RegNo] = PH4Part1.[UKNI] WHERE "
If Frame39 = 1 Then
strSQL = strSQL & "(((CustomerRecordsQuery.AreaCode) Like IIf([Forms]![PH4Part1Report]![AreaCmb]=""ALL"",""*"",[Forms]![PH4Part1Report]![AreaCmb])) AND ((PH4Part1.DateInsp)>=[Forms]![PH4Part1Report]![FirstDateTxt] AND (PH4Part1.DateInsp)<=[Forms]![PH4Part1Report]![LastDateTxt]);"
ElseIf Frame39 = 2 Then
strSQL = strSQL & "((PH4Part1.DateInsp)<[Forms]![PH4Part1Report]![FirstDateTxt] And (PH4Part1.DateInsp)>[Forms]![PH4Part1Report]![LastDateTxt] Or (PH4Part1.DateInsp) Is Null));"
End If
On Error Resume Next
qdf.SQL = strSQL
DoCmd.OpenReport stDocName, acPreview, , , acDialog
If Err = 2501 Then Err.Clear
Set db = Nothing
Set qdf = Nothing