B
bjschon
I am building a custom query form allowing the user to choose the fields and
criteria for each field in the query. This has required me to build the
query via VBA. I almost have it, but am struggling with one line of code:
If Me.Active = True Then
SQL = SQL & " where ((([Active?]) =" & True & ")" And (([DEALER]) = ""
& Me.DEALER & """)")
End If
This line basically allows the user to select criteria for the field
“active†(which is a check box yes/no field) and also allows the user to
choose a specific type of auto dealer from the dealer field. I’m getting the
error “Data type mismatch†for some reason.
Any thoughts would be greatly appreciated!
The entire code is below:
Private Sub cmdRunQuery_Click()
If Me.lstFieldList.ItemsSelected.Count = 0 Then
MsgBox "Select some field names first."
Exit Sub
End If
If Me.Active = False And Me.Inactive = False And Me.All = False Then
MsgBox "Select Active, Inactive, or All Dealers"
Exit Sub
End If
Dim qDef As Object
Dim SQL As String
Dim vItem As Variant
' loop through selected field names
For Each vItem In Me.lstFieldList.ItemsSelected
SQL = SQL & ",[" & Me.lstFieldList.ItemData(vItem) & "]"
Next vItem
' build new SQL statement
SQL = "Select " & Mid(SQL, 2) & " from [IndirectTableQuery]"
' add criteria for Component if a component has been selected
If Me.Active = True Then
SQL = SQL & " where ((([Active?]) =" & True & ")" And (([DEALER]) = ""
& Me.DEALER & """)")
End If
' save query with new SQL statement
Set qDef = CurrentDb.QueryDefs("CustomReportQuery")
qDef.SQL = SQL
Set qDef = Nothing
' run query
DoCmd.OpenQuery "CustomReportQuery"
End Sub
criteria for each field in the query. This has required me to build the
query via VBA. I almost have it, but am struggling with one line of code:
If Me.Active = True Then
SQL = SQL & " where ((([Active?]) =" & True & ")" And (([DEALER]) = ""
& Me.DEALER & """)")
End If
This line basically allows the user to select criteria for the field
“active†(which is a check box yes/no field) and also allows the user to
choose a specific type of auto dealer from the dealer field. I’m getting the
error “Data type mismatch†for some reason.
Any thoughts would be greatly appreciated!
The entire code is below:
Private Sub cmdRunQuery_Click()
If Me.lstFieldList.ItemsSelected.Count = 0 Then
MsgBox "Select some field names first."
Exit Sub
End If
If Me.Active = False And Me.Inactive = False And Me.All = False Then
MsgBox "Select Active, Inactive, or All Dealers"
Exit Sub
End If
Dim qDef As Object
Dim SQL As String
Dim vItem As Variant
' loop through selected field names
For Each vItem In Me.lstFieldList.ItemsSelected
SQL = SQL & ",[" & Me.lstFieldList.ItemData(vItem) & "]"
Next vItem
' build new SQL statement
SQL = "Select " & Mid(SQL, 2) & " from [IndirectTableQuery]"
' add criteria for Component if a component has been selected
If Me.Active = True Then
SQL = SQL & " where ((([Active?]) =" & True & ")" And (([DEALER]) = ""
& Me.DEALER & """)")
End If
' save query with new SQL statement
Set qDef = CurrentDb.QueryDefs("CustomReportQuery")
qDef.SQL = SQL
Set qDef = Nothing
' run query
DoCmd.OpenQuery "CustomReportQuery"
End Sub