[sample code fragment using the procedure]
Dim dbs As DAO.Database
Dim lngCount As Long
Dim lngID As Long
Dim rpt As Access.Report
Dim rst As DAO.Recordset
Dim strPrompt As String
Dim strQuery As String
Dim strRecordSource As String
Dim strReport As String
Dim strSQL As String
Dim strTitle As String
strRecordSource = "tblInventoryItemsComponents"
strQuery = "qryTemp"
Set dbs = CurrentDb
'Numeric filter
lngID = Nz(Me![ID])
If lntID <> 0 Then
strSQL = "SELECT * FROM " & strRecordSource & " WHERE " _
& "[ID] = " & lngID & ";"
End If
'String filter
strInventoryCode = Nz(Me![InventoryCode])
If strInventoryCode <> "" Then
strSQL = "SELECT * FROM " & strRecordSource & " WHERE " _
& "[InventoryCode] = " & Chr$(39) & strInventoryCode & Chr$(39) & ";"
End If
'Date range filter from custom database properties
dteFromDate = CDate(GetProperty("FromDate", ""))
dteToDate = CDate(GetProperty("ToDate", ""))
strSQL = "SELECT * FROM " & strRecordSource & " WHERE " _
& "[dteDateReceived] Between " & Chr(35) & dteFromDate _
& Chr(35) & " And " & Chr(35) & dteToDate & Chr(35) & ";"
'Date range filter from controls
If IsDate(Me![txtFromDate].Value) = True Then
dteFromDate = CDate(Me![txtFromDate].Value)
End If
If IsDate(Me![txtToDate].Value) = True Then
dteToDate = CDate(Me![txtToDate].Value)
End If
strSQL = "SELECT * FROM " & strRecordSource & " WHERE " _
& "[dteDateReceived] Between " & Chr(35) & dteFromDate _
& Chr(35) & " And " & Chr(35) & dteToDate & Chr(35) & ";"
Debug.Print "SQL for " & strQuery & ": " & strSQL
lngCount = CreateAndTestQuery(strQuery, strSQL)
Debug.Print "No. of items found: " & lngCount
If lngCount = 0 Then
strPrompt = "No records found; canceling"
strTitle = "Canceling"
MsgBox strPrompt, vbOKOnly + vbCritical, strTitle
GoTo ErrorHandlerExit
Else
'Use this line if you need a recordset
Set rst = dbs.OpenRecordset(strQuery)
End If
'Use SQL string as the record source of a form
strFormName = "fpriLoadSoldPackingSlip"
DoCmd.OpenForm FormName:=strFormName, _
view:=acDesign
Set frm = Forms(strFormName)
frm.RecordSource = strSQL
DoCmd.OpenForm FormName:=strFormName, _
view:=acNormal
'Use SQL string as the record source of a report
strReport = "rptLoadSold"
DoCmd.OpenReport ReportName:=strReport, _
view:=acViewDesign, _
windowmode:=acHidden
Set rpt = Reports(strReport)
rpt.RecordSource = strSQL
DoCmd.OpenReport ReportName:=strReport, _
view:=acViewNormal, _
windowmode:=acWindowNormal
'DoCmd.Save objecttype:=acReport, objectname:=strReport
'DoCmd.Close objecttype:=acReport, _
objectname:=strReport
=========================
Public Function CreateAndTestQuery(strTestQuery As String, _
strTestSQL As String) As Long
'Created by Helen Feddema 28-Jul-2002
'Last modified 6-Dec-2009
On Error Resume Next
Dim qdf As DAO.QueryDef
'Delete old query
Set dbs = CurrentDb
dbs.QueryDefs.Delete strTestQuery
On Error GoTo ErrorHandler
'Create new query
Set qdf = dbs.CreateQueryDef(strTestQuery, strTestSQL)
'Test whether there are any records
Set rst = dbs.OpenRecordset(strTestQuery)
With rst
.MoveFirst
.MoveLast
CreateAndTestQuery = .RecordCount
End With
ErrorHandlerExit:
Exit Function
ErrorHandler:
If Err.Number = 3021 Then
CreateAndTestQuery = 0
Resume ErrorHandlerExit
Else
MsgBox "Error No: " & Err.Number _
& " in CreateAndTestQuery procedure; " _
& "Description: " & Err.Description
End If
End Function
|