Problem removing query definition to load report

M

MarkJ

I borrowed the following code from somewhere for the purpose of using a combo
box on a form to load a report for selected categories of records. I
modified slightly to open reports for facilities located in the selected
states. The code runs fine, but when I open the report w/o the loader form,
only the records for the previously selected states open.

Can I modify the code so when I open the report w/o the loader I get all
records. I tried adding "qdf.close" and "qdf.cancel" which didn't work, but
I don't really know what I'm doing either.


Private Sub LAUNCH_COMP_Click()
' Declare variables
Dim db As DAO.Database
Dim qdf As QueryDef
Dim varItem As Variant
Dim strCriteria As String
Dim strSQL As String
' Get the database and stored query
Set db = CurrentDb()
Set qdf = db.QueryDefs("_STATE_SELECT")
' Loop through the selected items in the list box and build a text string
For Each varItem In Me!lstStates.ItemsSelected
strCriteria = strCriteria & ",'" & Me!lstStates.ItemData(varItem) &
"'"
Next varItem
' Check that user selected something
If Len(strCriteria) = 0 Then
MsgBox "You did not select any states." _
& vbCrLf & "All reports will be returned.", vbExclamation, "No
States Selected."
strSQL = "SELECT * FROM _STATES"
Else
' Remove the leading comma from the string
strCriteria = Right(strCriteria, Len(strCriteria) - 1)
' Build the new SQL statement incorporating the string
strSQL = "SELECT * FROM _STATES " & _
"WHERE STATE IN(" & strCriteria & ");"
End If
' Apply the new SQL statement to the query
qdf.sql = strSQL
' Open the reports
DoCmd.OpenReport "R_ComplianceReport", acViewPreview
' Close the launch form
DoCmd.Close acForm, "COMPLIANCE_REPORTS_SELECT", acSaveNo
' Empty the memory
qdf.Close 'mkj add, but doesn't stop from being in memory
db.Close 'mkj add
Set db = Nothing
Set qdf = Nothing
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top