Private Sub print5160_Click()
On Error GoTo Err_print5160_Click
Dim db As DAO.Database
Dim loqd As DAO.QueryDef
Dim vItm As Variant
Dim stWhat As String
Dim stCriteria As String
Dim stSQL As String
Dim counter As Integer
stWhat = "": stCriteria = ",": counter = 0
For Each vItm In Me.CatSelectCombo.ItemsSelected
counter = counter + 1
stWhat = stWhat & "'" & Me!CatSelectCombo.ItemData(vItm) & "'"
stWhat = stWhat & stCriteria
Next vItm
If counter = 0 Then
MsgBox "You did not select a choice from the listbox!"
Exit Sub
End If
Me!txtCriteria = CStr(Left$(stWhat, Len(stWhat) - Len(stCriteria)))
stSQL = "SELECT DISTINCT Main.Envelope1, Main.Envelope2, Main.EnvelopeTitle, Main.Envelope3, Main.Envelope4, "
stSQL = stSQL & "Main.Envelope5, Main.[First Name], Main.[Middle Initial], Main.Envelope6,"
stSQL = stSQL & "Main.[Last Name], Main.[Company Name], Main.Address, Main.Suite, Main.City, "
stSQL = stSQL & "Main.State, Main.[Zip Code] FROM Main INNER JOIN Categories ON Main.ID = Categories.ID "
stSQL = stSQL & "WHERE Categories.Category IN (" & Me!txtCriteria & ");"
Set db = CurrentDb
Set loqd = db.QueryDefs("LabelQuery")
With loqd
.SQL = stSQL
.Close
End With
Set loqd = Nothing
Set db = Nothing
'Opens label report to print labels, then closes form
DoCmd.OpenReport "Labels 5160", acPreview, "", ""
DoCmd.Close acForm, "CatSelector"
Exit_print5160_Click:
Exit Sub
Err_print5160_Click:
MsgBox Err.Description
Resume Exit_print5160_Click
End Sub