Access.2007.VBA.Bibl..

(John Hannent) #1
ErrorHandler:
‘Excel is not running; open Excel with CreateObject
If Err.Number = 429 Then
Set appExcel = CreateObject(“Excel.Application”)
Resume Next
Else
MsgBox “Error No: “ & Err.Number & “; Description: “
Resume ErrorHandlerExit
End If

End Sub

Public Function CreateAndTestQuery(strTestQuery As String, _
strTestSQL As String) As Long

This function is called from other procedures to create a filtered query, using a SQL string in its
strTestSQLargument:

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 & “; Description: “ &
Err.Description
Resume ErrorHandlerExit
End If

Part I The Office Components and What They Do Best

Free download pdf