Trevor G
Registered User.
- Local time
- Today, 01:57
- Joined
- Oct 1, 2009
- Messages
- 2,368
I am in the process of creating a form which will use a listbox and have multiple check boxes, what I am aiming for is select multiple years form the listbox, then use some or all of the checkboxes to build a query, if the check box is checked the field is to be included in the query, if not then ignore it.
So far I have created the form add the list box and checkboxes named each item, added the code to reset and also created the code to run the query, I am stuck on the check box part.
Here is my code so far, I have named each checkbox with a prefix of CHK and also set a tag called CHK in there properties. I have created the Dim strParam and this is where I am stuck, the query currently will open all the fields in the table.
The form name is "frmCreateCriteria" and I have attached a copy of the testing database.
Private Sub cmdShowResults_Click()
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim varItem As Variant
Dim strCriteria As String
Dim strSQL As String
Dim strParam As String
Set db = CurrentDb()
Set qdf = db.QueryDefs("qryAdhocSearch")
For Each varItem In Me!lstUWYear.ItemsSelected
strCriteria = strCriteria & "," & Me!lstUWYear.ItemData(varItem) & ""
Next varItem
If Len(strCriteria) = 0 Then
MsgBox "You did not select anything from the list", vbExclamation, "Nothing to find!"
Exit Sub
End If
strCriteria = Right(strCriteria, Len(strCriteria) - 1)
strSQL = "SELECT * FROM Combined " & _
"WHERE [Combined].[Underwriting Year] IN(" & strCriteria & ");"
qdf.SQL = strSQL
DoCmd.OpenQuery "qryAdhocSearch"
Set db = Nothing
Set qdf = Nothing
End Sub
So far I have created the form add the list box and checkboxes named each item, added the code to reset and also created the code to run the query, I am stuck on the check box part.
Here is my code so far, I have named each checkbox with a prefix of CHK and also set a tag called CHK in there properties. I have created the Dim strParam and this is where I am stuck, the query currently will open all the fields in the table.
The form name is "frmCreateCriteria" and I have attached a copy of the testing database.
Private Sub cmdShowResults_Click()
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim varItem As Variant
Dim strCriteria As String
Dim strSQL As String
Dim strParam As String
Set db = CurrentDb()
Set qdf = db.QueryDefs("qryAdhocSearch")
For Each varItem In Me!lstUWYear.ItemsSelected
strCriteria = strCriteria & "," & Me!lstUWYear.ItemData(varItem) & ""
Next varItem
If Len(strCriteria) = 0 Then
MsgBox "You did not select anything from the list", vbExclamation, "Nothing to find!"
Exit Sub
End If
strCriteria = Right(strCriteria, Len(strCriteria) - 1)
strSQL = "SELECT * FROM Combined " & _
"WHERE [Combined].[Underwriting Year] IN(" & strCriteria & ");"
qdf.SQL = strSQL
DoCmd.OpenQuery "qryAdhocSearch"
Set db = Nothing
Set qdf = Nothing
End Sub