i have a form that has several options (cmboboxes, text, date and time picker) and depending on what options are select a make a sql string to query a table. my problem is when i add a between dates criteria to the string it breaks.
here is an example of a string that wont work, it states that it is missing the two parameters of the between criteria:
SELECT * FROM tblChanges_local WHERE ((tblChanges_local.numOperation) = 'Reformer') AND ((tblChanges_local.txtType) = 'Reformer Cartridge') AND ((tblChanges_local.dateInstall) Between [Forms]![NeckerTools].[startDate] And [Forms]![NeckerTools].[endDate]) ORDER BY tblChanges_local.numNecker, tblChanges_local.numOperation, tblChanges_local.numPocket, tblChanges_local.txtType, tblChanges_local.dateInstall DESC;
to me i see nothing wrong, but i have been trying to get this one last thing to work for the last day or so.
the following is my code to create the string. the boolean variables are set so that if the corresponding data field on the form is blank that particular criteria will be left out of the search.
sqlNeck = "SELECT * FROM tblChanges_local"
If noVars = False Then
sqlNeck = sqlNeck + " WHERE "
End If
If noNeck = False Then
sqlNeck = sqlNeck + " ((tblChanges_local.numNecker) = '" & numNeck & "')"
End If
If noOp = False Then
If noNeck = False Then
sqlNeck = sqlNeck + " AND "
End If
sqlNeck = sqlNeck + " ((tblChanges_local.numOperation) = '" & numOperation & "')"
End If
If noPock = False Then
If (noNeck = False) Or (noOp = False) Then
sqlNeck = sqlNeck + " AND "
End If
sqlNeck = sqlNeck + " ((tblChanges_local.numPocket) = '" & numPock & "')"
End If
If noType = False Then
If (noNeck = False) Or (noOp = False) Or (noPock = False) Then
sqlNeck = sqlNeck + " AND "
End If
sqlNeck = sqlNeck + " ((tblChanges_local.txtType) = '" & txtType & "')"
End If
If noSerial = False Then
If (noNeck = False) Or (noOp = False) Or (noPock = False) Or (noType = False) Then
sqlNeck = sqlNeck + " AND "
End If
sqlNeck = sqlNeck + " ((tblChanges_local.txtSerial) = '" & txtSerial & "')"
End If
If Me.chkEnable.Value = -1 Then
If (noNeck = False) Or (noOp = False) Or (noPock = False) Or (noType = False) Or (noSerial = False) Then
sqlNeck = sqlNeck + " AND "
End If
sqlNeck = sqlNeck + "((tblChanges_local.dateInstall) Between [Forms]![NeckerTools].[startDate] And [Forms]![NeckerTools].[endDate])"
End If
If noVars = False Then
sqlNeck = sqlNeck + " ORDER BY tblChanges_local.numNecker, " & _
"tblChanges_local.numOperation, tblChanges_local.numPocket, " & _
"tblChanges_local.txtType, tblChanges_local.dateInstall DESC;"
Else
sqlNeck = sqlNeck + " ORDER BY tblChanges_local.dateInstall DESC;"
End If
here is an example of a string that wont work, it states that it is missing the two parameters of the between criteria:
SELECT * FROM tblChanges_local WHERE ((tblChanges_local.numOperation) = 'Reformer') AND ((tblChanges_local.txtType) = 'Reformer Cartridge') AND ((tblChanges_local.dateInstall) Between [Forms]![NeckerTools].[startDate] And [Forms]![NeckerTools].[endDate]) ORDER BY tblChanges_local.numNecker, tblChanges_local.numOperation, tblChanges_local.numPocket, tblChanges_local.txtType, tblChanges_local.dateInstall DESC;
to me i see nothing wrong, but i have been trying to get this one last thing to work for the last day or so.
the following is my code to create the string. the boolean variables are set so that if the corresponding data field on the form is blank that particular criteria will be left out of the search.
sqlNeck = "SELECT * FROM tblChanges_local"
If noVars = False Then
sqlNeck = sqlNeck + " WHERE "
End If
If noNeck = False Then
sqlNeck = sqlNeck + " ((tblChanges_local.numNecker) = '" & numNeck & "')"
End If
If noOp = False Then
If noNeck = False Then
sqlNeck = sqlNeck + " AND "
End If
sqlNeck = sqlNeck + " ((tblChanges_local.numOperation) = '" & numOperation & "')"
End If
If noPock = False Then
If (noNeck = False) Or (noOp = False) Then
sqlNeck = sqlNeck + " AND "
End If
sqlNeck = sqlNeck + " ((tblChanges_local.numPocket) = '" & numPock & "')"
End If
If noType = False Then
If (noNeck = False) Or (noOp = False) Or (noPock = False) Then
sqlNeck = sqlNeck + " AND "
End If
sqlNeck = sqlNeck + " ((tblChanges_local.txtType) = '" & txtType & "')"
End If
If noSerial = False Then
If (noNeck = False) Or (noOp = False) Or (noPock = False) Or (noType = False) Then
sqlNeck = sqlNeck + " AND "
End If
sqlNeck = sqlNeck + " ((tblChanges_local.txtSerial) = '" & txtSerial & "')"
End If
If Me.chkEnable.Value = -1 Then
If (noNeck = False) Or (noOp = False) Or (noPock = False) Or (noType = False) Or (noSerial = False) Then
sqlNeck = sqlNeck + " AND "
End If
sqlNeck = sqlNeck + "((tblChanges_local.dateInstall) Between [Forms]![NeckerTools].[startDate] And [Forms]![NeckerTools].[endDate])"
End If
If noVars = False Then
sqlNeck = sqlNeck + " ORDER BY tblChanges_local.numNecker, " & _
"tblChanges_local.numOperation, tblChanges_local.numPocket, " & _
"tblChanges_local.txtType, tblChanges_local.dateInstall DESC;"
Else
sqlNeck = sqlNeck + " ORDER BY tblChanges_local.dateInstall DESC;"
End If