Trevor G
Registered User.
- Local time
- Today, 21:32
- Joined
- Oct 1, 2009
- Messages
- 2,341
I am looking to generate a query based on selected controls in a form. The idea is select items from a list box (Multi Selection) then check some check boxes and it then generates a new query showing only the items selected from the listbox and what I have checked from the check boxes.
I am getting an error message which first states it cannot find the database.
The data comes from a single table called TempImport.
The form name is frmAdhocReportCreatorApril2012
The listbox name is lstSALCLV
Then each checkbox name begins with chk (sample would be chkSALNCL or chkSALORIG or chkSALSTC and so on)
The code I am developing is shown below. When I first select items from the list box and check some of the check boxes then click the Generate button it gives me this error "Could not find File" and Error Number 3024. The path it indicates is completely wrong as it indicates "M:\TempImport.mdb" as the database is located on my desktop and has a different name.
If I reset it and try again it then tells me the query already exist and gives me a run time error of 3012. Then highlights the line indicated in Red
I am getting an error message which first states it cannot find the database.
The data comes from a single table called TempImport.
The form name is frmAdhocReportCreatorApril2012
The listbox name is lstSALCLV
Then each checkbox name begins with chk (sample would be chkSALNCL or chkSALORIG or chkSALSTC and so on)
The code I am developing is shown below. When I first select items from the list box and check some of the check boxes then click the Generate button it gives me this error "Could not find File" and Error Number 3024. The path it indicates is completely wrong as it indicates "M:\TempImport.mdb" as the database is located on my desktop and has a different name.
If I reset it and try again it then tells me the query already exist and gives me a run time error of 3012. Then highlights the line indicated in Red
Private Sub cmdGenerate_Click()
Dim frm As Form, ctl As Control
Dim I As Long
Dim LngCnt As Long
Dim dummy As Variant
Dim strsql As String
Dim db As Database
Dim qry As QueryDef
Dim varItm As Variant
Dim strparam As String
Dim strfrom As String
strsql = "SELECT [tempImport].SALCLV"
strfrom = " FROM [tempImport].SALCLV"
strparam = " WHERE "
Set frm = Forms!frmAdhocReportCreatorApril2012
Set ctl = frm!lstSALCLV
For Each varItm In ctl.ItemsSelected
strparam = strparam & "[tempImport].SALCLV=" & "'" & ctl.ItemData(varItm) & "'" & " OR "
Next varItm
strparam = Left(strparam, Len(strparam) - 4)
On Error GoTo start
Do While I = 0
LngCnt = LngCnt + 1
dummy = Me.Controls.Item(LngCnt).Name
Loop
start:
For I = 1 To LngCnt - 1
If Me.Controls.Item(I).Tag = "CHK" Then
If Me.Controls.Item(I).Value = True Then
strsql = strsql & ", "
End If
End If
Next I
strsql = strsql & strfrom & strparam
Set db = CurrentDb()
On Error Resume Next
Set qry = db.CreateQueryDef("qryAReportGenerator", strsql)
If Err > 0 Then
MsgBox ("You must select at least one product")
Exit Sub
End If
DoCmd.OpenQuery "qryAReportGenerator"
db.QueryDefs.Delete ("qryAReportGenerator")
End Sub