lilsincere
New member
- Local time
- Today, 12:07
- Joined
- Jul 10, 2016
- Messages
- 6
evening folks... this is my first post here but i have ghosted this site for years learning.
to get to it... I have a form with a listbox of different voting demographics. I will be taking notes and i want to select various demographics that are correlated to said notes.
so far I have gather a bit of code mostly from here:
Private Sub Command371_Click()
On Error GoTo Err_cmdOpenQuery_Click
Dim MyDB As DAO.Database
Dim qdef As DAO.QueryDef
Dim i As Integer
Dim strSQL As String
Dim strWhere As String
Dim strIN As String
Dim flgSelectAll As Boolean
Dim varItem As Variant
Set MyDB = CurrentDb()
strSQL = "SELECT * FROM tblDemographic"
'Build the IN string by looping through the listbox
For i = 0 To lstDemographic.ListCount - 1
If lstDemographic.Selected(i) Then
If lstDemographic.Column(1, i) = "All" Then
flgSelectAll = True
End If
strIN = strIN & "'" & lstDemographic.Column(1, i) & "',"
End If
Next i
'Create the WHERE string, and strip off the last comma of the IN string
strWhere = " WHERE [demographic] in " & _
"(" & Left(strIN, Len(strIN) - 1) & ")"
'If "All" was selected in the listbox, don't add the WHERE condition
If Not flgSelectAll Then
strSQL = strSQL & strWhere
End If
MyDB.QueryDefs.Delete "qryDemographic"
Set qdef = MyDB.CreateQueryDef("qryDemographic", strSQL)
'Open the query, built using the IN clause to set the criteria
DoCmd.OpenQuery "qryDemographic", acViewNormal
'Clear listbox selection after running query
For Each varItem In Me.lstDemographic.ItemsSelected
Me.lstDemographic.Selected(varItem) = False
Next varItem
Exit_cmdOpenQuery_Click:
Exit Sub
Err_cmdOpenQuery_Click:
If Err.Number = 5 Then
MsgBox "You must make a selection(s) from the list" _
, , "Selection Required !"
Resume Exit_cmdOpenQuery_Click
Else
'Write out the error and exit the sub
MsgBox Err.Description
Resume Exit_cmdOpenQuery_Click
End If
End Sub
to append my results to a table I used:
MyDB.execute INSERT INTO tblNotesDemographic
SELECT *
FROM qryDemographic
MyDB.close
Is this the correct code? Where would I properly insert this, as I have tried various places throughout my code and i get all kinds of crazy syntax?
Thank you.
to get to it... I have a form with a listbox of different voting demographics. I will be taking notes and i want to select various demographics that are correlated to said notes.
so far I have gather a bit of code mostly from here:
Private Sub Command371_Click()
On Error GoTo Err_cmdOpenQuery_Click
Dim MyDB As DAO.Database
Dim qdef As DAO.QueryDef
Dim i As Integer
Dim strSQL As String
Dim strWhere As String
Dim strIN As String
Dim flgSelectAll As Boolean
Dim varItem As Variant
Set MyDB = CurrentDb()
strSQL = "SELECT * FROM tblDemographic"
'Build the IN string by looping through the listbox
For i = 0 To lstDemographic.ListCount - 1
If lstDemographic.Selected(i) Then
If lstDemographic.Column(1, i) = "All" Then
flgSelectAll = True
End If
strIN = strIN & "'" & lstDemographic.Column(1, i) & "',"
End If
Next i
'Create the WHERE string, and strip off the last comma of the IN string
strWhere = " WHERE [demographic] in " & _
"(" & Left(strIN, Len(strIN) - 1) & ")"
'If "All" was selected in the listbox, don't add the WHERE condition
If Not flgSelectAll Then
strSQL = strSQL & strWhere
End If
MyDB.QueryDefs.Delete "qryDemographic"
Set qdef = MyDB.CreateQueryDef("qryDemographic", strSQL)
'Open the query, built using the IN clause to set the criteria
DoCmd.OpenQuery "qryDemographic", acViewNormal
'Clear listbox selection after running query
For Each varItem In Me.lstDemographic.ItemsSelected
Me.lstDemographic.Selected(varItem) = False
Next varItem
Exit_cmdOpenQuery_Click:
Exit Sub
Err_cmdOpenQuery_Click:
If Err.Number = 5 Then
MsgBox "You must make a selection(s) from the list" _
, , "Selection Required !"
Resume Exit_cmdOpenQuery_Click
Else
'Write out the error and exit the sub
MsgBox Err.Description
Resume Exit_cmdOpenQuery_Click
End If
End Sub
to append my results to a table I used:
MyDB.execute INSERT INTO tblNotesDemographic
SELECT *
FROM qryDemographic
MyDB.close
Is this the correct code? Where would I properly insert this, as I have tried various places throughout my code and i get all kinds of crazy syntax?
Thank you.