M
Marianne
I need to be able to run a make a table query using the selection in a multi
select list box as the criteria. The code I have tried (along with many
other) gives me Error 3067 - Query input must contain at least one table or
query. Could you please let me know what is wrong with the code I'm using.
Private Sub cmdPreview_Click()
On Error GoTo Err_Handler
'Purpose: Open the report filtered to the items selected in the list box.
'Author: Allen J Browne, 2004. http://allenbrowne.com
Dim varItem As Variant 'Selected items
Dim strWhere As String 'String to use as WhereCondition
Dim strDescrip As String 'Description of WhereCondition
Dim lngLen As Long 'Length of string
Dim strDelim As String 'Delimiter for this field type.
Dim strDoc As String 'Name of report to open.
Dim flgSelectAll As Boolean
Dim strSQL As String
Dim db As Database
Dim Q As QueryDef
strDelim = """" 'Delimiter appropriate to field type. See
note 1.
strDoc = "MYOB_activityslip"
'Loop through the ItemsSelected in the list box.
With Me.lstCarer
For Each varItem In .ItemsSelected
If Not IsNull(varItem) Then
'Build up the filter from the bound column (hidden).
strWhere = strWhere & strDelim & .ItemData(varItem) &
strDelim & ","
'Build up the description from the text in the visible
column. See note 2.
strDescrip = strDescrip & """" & .Column(1, varItem) & """, "
End If
Next
End With
'Remove trailing comma. Add field name, IN operator, and brackets.
lngLen = Len(strWhere) - 1
If lngLen > 0 Then
strWhere = "[Carer] IN (" & Left$(strWhere, lngLen) & ")"
lngLen = Len(strDescrip) - 2
If lngLen > 0 Then
strDescrip = "Carer: " & Left$(strDescrip, lngLen)
End If
End If
Set db = CurrentDb()
Set Q = db.QueryDefs("MYOB_activityslip")
strSQL = "SELECT qry_Staff.[Last Name], qry_Staff.[First Name],
IIf(tblService_Date_and_Times!Sleepover=True,'Sleepover',tblContracts![Service
Type]) AS ServiceType,
Format(Sum(IIf(tblService_Date_and_Times!Sleepover=True,4,IIf([Start_Time]>[End_Time]
And
tblService_Date_and_Times!Sleepover=False,(([End_Time]-[Start_Time])*24)+24,([End_Time]-[Start_Time])*24))),'Fixed')
AS Units, IIf(tblContracts![Service No]>100000 And
tblService_Date_and_Times!ServDate>tblContracts![End Date],'Ex' &
tblContracts![Service No],tblContracts![Service No]) AS Job,
tblContracts.Customer, tblContracts.Rate, 'Base Hourly' AS PayCat INTO
tblMYOBActivity" & _
"FROM (tblContracts RIGHT JOIN tblService_Date_and_Times ON
tblContracts.[Service No] = tblService_Date_and_Times.Service_Plan_ID) LEFT
JOIN qry_Staff ON tblService_Date_and_Times.Carer = qry_Staff.Carer" & _
"Where ((((tblService_Date_and_Times.ServDate) >=
[Forms]![frmprStHrsSelect]![StartDate] And
(tblService_Date_and_Times.ServDate) <= [Forms]![frmprStHrsSelect]![EndDate])
And ((tblService_Date_and_Times.ShiftCan) = False) And
((tblService_Date_and_Times.Verified) = True)AND ([Carer] In (" & strWhere &
"))" & _
"GROUP BY qry_Staff.[Last Name], qry_Staff.[First Name],
IIf(tblService_Date_and_Times!Sleepover=True,'Sleepover',tblContracts![Service
Type]), IIf(tblContracts![Service No]>100000 And
tblService_Date_and_Times!ServDate>tblContracts![End Date],'Ex' &
tblContracts![Service No],tblContracts![Service No]), tblContracts.Customer,
tblContracts.Rate, 'Base Hourly'" & _
"ORDER BY
IIf(tblService_Date_and_Times!Sleepover=True,'Sleepover',tblContracts![Service Type]);"
Q.SQL = strSQL
DoCmd.OpenQuery "MYOB_activityslip"
Exit_Handler:
Exit Sub
Err_Handler:
If Err.Number <> 2501 Then 'Ignore "Report cancelled" error.
MsgBox "Error " & Err.Number & " - " & Err.Description, ,
"cmdPreview_Click"
End If
Resume Exit_Handler
End Sub
select list box as the criteria. The code I have tried (along with many
other) gives me Error 3067 - Query input must contain at least one table or
query. Could you please let me know what is wrong with the code I'm using.
Private Sub cmdPreview_Click()
On Error GoTo Err_Handler
'Purpose: Open the report filtered to the items selected in the list box.
'Author: Allen J Browne, 2004. http://allenbrowne.com
Dim varItem As Variant 'Selected items
Dim strWhere As String 'String to use as WhereCondition
Dim strDescrip As String 'Description of WhereCondition
Dim lngLen As Long 'Length of string
Dim strDelim As String 'Delimiter for this field type.
Dim strDoc As String 'Name of report to open.
Dim flgSelectAll As Boolean
Dim strSQL As String
Dim db As Database
Dim Q As QueryDef
strDelim = """" 'Delimiter appropriate to field type. See
note 1.
strDoc = "MYOB_activityslip"
'Loop through the ItemsSelected in the list box.
With Me.lstCarer
For Each varItem In .ItemsSelected
If Not IsNull(varItem) Then
'Build up the filter from the bound column (hidden).
strWhere = strWhere & strDelim & .ItemData(varItem) &
strDelim & ","
'Build up the description from the text in the visible
column. See note 2.
strDescrip = strDescrip & """" & .Column(1, varItem) & """, "
End If
Next
End With
'Remove trailing comma. Add field name, IN operator, and brackets.
lngLen = Len(strWhere) - 1
If lngLen > 0 Then
strWhere = "[Carer] IN (" & Left$(strWhere, lngLen) & ")"
lngLen = Len(strDescrip) - 2
If lngLen > 0 Then
strDescrip = "Carer: " & Left$(strDescrip, lngLen)
End If
End If
Set db = CurrentDb()
Set Q = db.QueryDefs("MYOB_activityslip")
strSQL = "SELECT qry_Staff.[Last Name], qry_Staff.[First Name],
IIf(tblService_Date_and_Times!Sleepover=True,'Sleepover',tblContracts![Service
Type]) AS ServiceType,
Format(Sum(IIf(tblService_Date_and_Times!Sleepover=True,4,IIf([Start_Time]>[End_Time]
And
tblService_Date_and_Times!Sleepover=False,(([End_Time]-[Start_Time])*24)+24,([End_Time]-[Start_Time])*24))),'Fixed')
AS Units, IIf(tblContracts![Service No]>100000 And
tblService_Date_and_Times!ServDate>tblContracts![End Date],'Ex' &
tblContracts![Service No],tblContracts![Service No]) AS Job,
tblContracts.Customer, tblContracts.Rate, 'Base Hourly' AS PayCat INTO
tblMYOBActivity" & _
"FROM (tblContracts RIGHT JOIN tblService_Date_and_Times ON
tblContracts.[Service No] = tblService_Date_and_Times.Service_Plan_ID) LEFT
JOIN qry_Staff ON tblService_Date_and_Times.Carer = qry_Staff.Carer" & _
"Where ((((tblService_Date_and_Times.ServDate) >=
[Forms]![frmprStHrsSelect]![StartDate] And
(tblService_Date_and_Times.ServDate) <= [Forms]![frmprStHrsSelect]![EndDate])
And ((tblService_Date_and_Times.ShiftCan) = False) And
((tblService_Date_and_Times.Verified) = True)AND ([Carer] In (" & strWhere &
"))" & _
"GROUP BY qry_Staff.[Last Name], qry_Staff.[First Name],
IIf(tblService_Date_and_Times!Sleepover=True,'Sleepover',tblContracts![Service
Type]), IIf(tblContracts![Service No]>100000 And
tblService_Date_and_Times!ServDate>tblContracts![End Date],'Ex' &
tblContracts![Service No],tblContracts![Service No]), tblContracts.Customer,
tblContracts.Rate, 'Base Hourly'" & _
"ORDER BY
IIf(tblService_Date_and_Times!Sleepover=True,'Sleepover',tblContracts![Service Type]);"
Q.SQL = strSQL
DoCmd.OpenQuery "MYOB_activityslip"
Exit_Handler:
Exit Sub
Err_Handler:
If Err.Number <> 2501 Then 'Ignore "Report cancelled" error.
MsgBox "Error " & Err.Number & " - " & Err.Description, ,
"cmdPreview_Click"
End If
Resume Exit_Handler
End Sub