When a query fails in a program, the error info Access gives is not enough. I have to ask the user questions to zero in on what form or report was in use and what was being done when the failure happened, before I even know which query we are talking about. Below is my attempt to deal with this. I have not had an opportunity to test this on a machine with only run-time Access. I would be grateful for your comments on how I am going about this. Thank you.
Put these functions into a module:
Public Function Run_SQL(strsql As String) As Boolean
'---------------------------------------------------------------------------
On Error GoTo SQLDidNotRun
DoCmd.SetWarnings False
DoCmd.RunSQL strsql
DoCmd.SetWarnings True
Run_SQL = True
EXIT_PROC:
Exit Function
SQLDidNotRun:
DoCmd.SetWarnings True
Run_SQL = False
GoTo EXIT_PROC
End Function
Public Function Open_Qry(qryName As String) As Boolean
'---------------------------------------------------------------------------
On Error GoTo QryDidNotRun
DoCmd.SetWarnings False
DoCmd.OpenQuery qryName
DoCmd.SetWarnings True
Open_Qry = True
EXIT_PROC:
Exit Function
QryDidNotRun:
DoCmd.SetWarnings True
Open_Qry = False
GoTo EXIT_PROC
End Function
Example of use:
At the beginning of the code for the particular event, such as Form_Load for a form named frmOpening, add, for example:
Const CalledFrom = "frmOpening" ' Name of form or report.
Const ForEvent = "Form_Load" ' Name of event.
Dim strsql As String ' For in-line queries.
Dim tgtqry As String ' For existing queries.
Dim ErrTxt As String
Here is an example of running an in-line query. (The first table mentioned is deliberately non-existent.)
'---------------------------------------------------------------------------
strsql = "INSERT INTO tblInboundHistoryXXX SELECT tblInbound.* FROM " & _ "tblInbound WHERE LeftUSA<(Date()-2)"
If Run_SQL(strsql) = False Then
GoSub ERROR_MSG_SQL
End If
'---------------------------------------------------------------------------
Here is an example of running an existing query:
'---------------------------------------------------------------------------
tgtqry = "qryDeleteArchived_tblOther"
If Open_Qry(tgtqry) = False Then
GoSub ERROR_MSG_QRY
End If
'---------------------------------------------------------------------------
After all other code has been entered for this event, insert the remaining lines shown here:
EXIT_PROC:
Exit Sub
ERROR_MSG_SQL:
ErrTxt = "An error occurred during an attempt to run this in-line query:" & vbCrLf & vbCrLf & strsql
GoSub GatherFacts
ShowMsg ErrTxt, "ERROR", "Courier New", 12
GoTo EXIT_PROC
ERROR_MSG_QRY:
ErrTxt = "An error occurred during an attempt to run the query:" & vbCrLf & vbCrLf & tgtqry
GoSub GatherFacts
ShowMsg ErrTxt, "ERROR", "Courier New", 12
GoTo EXIT_PROC
GatherFacts:
ErrTxt = ErrTxt & vbCrLf & vbCrLf & _
"From: " & CalledFrom & vbCrLf & _
"Event: " & ForEvent & vbCrLf & vbCrLf & _
"Computer: " & fOSMachineName & vbCrLf & _
"User: " & fOSUserName & vbCrLf & vbCrLf & _
"Date: " & Format(Date, "ddd, mmm d, yyyy") & vbCrLf & _
"Time: " & Format(Time, "h:nn AM/PM") & vbCrLf & vbCrLf & _
"Please notify IT."
Return
End Sub
Source for fosMachineName and fosUserName: (Dev Ashish)
Example of output for in-line query.
An error occurred during an attempt to run this in-line query:
INSERT INTO tblInboundHistoryXXX SELECT tblInbound.* FROM tblInbound WHERE LeftUSA<(Date()-2)
From: frmOpening
Event: Form_Load
Computer: DON_THINK
User: Don
Date: Fri, Dec 28, 2018
Time: 2:24 PM
Please notify IT.
Example of output for an existing query.
An error occurred during an attempt to run the query:
qryDeleteArchived_tblOther
From: frmOpening
Event: Form_Load
Computer: DON-THINK
User: Don
Date: Fri, Dec 28, 2018
Time: 3:12 PM
Please notify IT.
Put these functions into a module:
Public Function Run_SQL(strsql As String) As Boolean
'---------------------------------------------------------------------------
On Error GoTo SQLDidNotRun
DoCmd.SetWarnings False
DoCmd.RunSQL strsql
DoCmd.SetWarnings True
Run_SQL = True
EXIT_PROC:
Exit Function
SQLDidNotRun:
DoCmd.SetWarnings True
Run_SQL = False
GoTo EXIT_PROC
End Function
Public Function Open_Qry(qryName As String) As Boolean
'---------------------------------------------------------------------------
On Error GoTo QryDidNotRun
DoCmd.SetWarnings False
DoCmd.OpenQuery qryName
DoCmd.SetWarnings True
Open_Qry = True
EXIT_PROC:
Exit Function
QryDidNotRun:
DoCmd.SetWarnings True
Open_Qry = False
GoTo EXIT_PROC
End Function
Example of use:
At the beginning of the code for the particular event, such as Form_Load for a form named frmOpening, add, for example:
Const CalledFrom = "frmOpening" ' Name of form or report.
Const ForEvent = "Form_Load" ' Name of event.
Dim strsql As String ' For in-line queries.
Dim tgtqry As String ' For existing queries.
Dim ErrTxt As String
Here is an example of running an in-line query. (The first table mentioned is deliberately non-existent.)
'---------------------------------------------------------------------------
strsql = "INSERT INTO tblInboundHistoryXXX SELECT tblInbound.* FROM " & _ "tblInbound WHERE LeftUSA<(Date()-2)"
If Run_SQL(strsql) = False Then
GoSub ERROR_MSG_SQL
End If
'---------------------------------------------------------------------------
Here is an example of running an existing query:
'---------------------------------------------------------------------------
tgtqry = "qryDeleteArchived_tblOther"
If Open_Qry(tgtqry) = False Then
GoSub ERROR_MSG_QRY
End If
'---------------------------------------------------------------------------
After all other code has been entered for this event, insert the remaining lines shown here:
EXIT_PROC:
Exit Sub
ERROR_MSG_SQL:
ErrTxt = "An error occurred during an attempt to run this in-line query:" & vbCrLf & vbCrLf & strsql
GoSub GatherFacts
ShowMsg ErrTxt, "ERROR", "Courier New", 12
GoTo EXIT_PROC
ERROR_MSG_QRY:
ErrTxt = "An error occurred during an attempt to run the query:" & vbCrLf & vbCrLf & tgtqry
GoSub GatherFacts
ShowMsg ErrTxt, "ERROR", "Courier New", 12
GoTo EXIT_PROC
GatherFacts:
ErrTxt = ErrTxt & vbCrLf & vbCrLf & _
"From: " & CalledFrom & vbCrLf & _
"Event: " & ForEvent & vbCrLf & vbCrLf & _
"Computer: " & fOSMachineName & vbCrLf & _
"User: " & fOSUserName & vbCrLf & vbCrLf & _
"Date: " & Format(Date, "ddd, mmm d, yyyy") & vbCrLf & _
"Time: " & Format(Time, "h:nn AM/PM") & vbCrLf & vbCrLf & _
"Please notify IT."
Return
End Sub
Source for fosMachineName and fosUserName: (Dev Ashish)
Example of output for in-line query.
An error occurred during an attempt to run this in-line query:
INSERT INTO tblInboundHistoryXXX SELECT tblInbound.* FROM tblInbound WHERE LeftUSA<(Date()-2)
From: frmOpening
Event: Form_Load
Computer: DON_THINK
User: Don
Date: Fri, Dec 28, 2018
Time: 2:24 PM
Please notify IT.
Example of output for an existing query.
An error occurred during an attempt to run the query:
qryDeleteArchived_tblOther
From: frmOpening
Event: Form_Load
Computer: DON-THINK
User: Don
Date: Fri, Dec 28, 2018
Time: 3:12 PM
Please notify IT.