B
Bill Murphy
I'm storing the SQL code for certain queries in a memo field in a table and
executing this code in VBA. This code creates a query with CreateQueryDef
using the SQL code stored in tblAdminLANQueries. If it's a Select query it
opens it, else it executes the action query.
The action and select queries work fine if there are no parameters in the
query. But if TempLANQuery includes parameters such as [Begin Date] I get
an error 2482 - "Microsoft Access can't find the name 'Begin Date' you
entered in the expression." The query TempLANQuery runs with no problems
outside the application, and asks the user to input the required parameter
[Begin Date] as expected.
The code is shown below. Any thoughts on this error will be appreciated.
Bill
__________________________________________________
Private Sub cmdExecute_Click()
' execute the selected action query or open a Select query
Dim db As Database
Set db = CurrentDb
Dim qdf As QueryDef
Dim prm As Parameter
' delete the query if it already exists in the mdb
On Error Resume Next
DoCmd.DeleteObject acQuery, "TempLANQuery"
On Error GoTo Error_Handler
Set qdf = db.CreateQueryDef("TempLANQuery")
qdf.SQL = Me!QueryCode ' stored in tblAdminLANQueries
For Each prm In qdf.Parameters
prm.Value = Eval(prm.Name)
Next prm
If Left(Me!QueryCode, 6) = "select" Then
DoCmd.OpenQuery ("TempLANQuery")
Else
qdf.Execute
End If
MsgBox "Query executed successfully."
Exit_ExecuteQuery:
Set db = Nothing
Set qdf = Nothing
Exit Sub
Error_Handler:
MsgBox "An error occurred when attempting to execute " & Me!Description
_
& vbCrLf & vbCrLf _
& "Error number: " & Str(Err) _
& vbCrLf _
& "Description: " & Err.Description
Resume Exit_ExecuteQuery
End Sub
executing this code in VBA. This code creates a query with CreateQueryDef
using the SQL code stored in tblAdminLANQueries. If it's a Select query it
opens it, else it executes the action query.
The action and select queries work fine if there are no parameters in the
query. But if TempLANQuery includes parameters such as [Begin Date] I get
an error 2482 - "Microsoft Access can't find the name 'Begin Date' you
entered in the expression." The query TempLANQuery runs with no problems
outside the application, and asks the user to input the required parameter
[Begin Date] as expected.
The code is shown below. Any thoughts on this error will be appreciated.
Bill
__________________________________________________
Private Sub cmdExecute_Click()
' execute the selected action query or open a Select query
Dim db As Database
Set db = CurrentDb
Dim qdf As QueryDef
Dim prm As Parameter
' delete the query if it already exists in the mdb
On Error Resume Next
DoCmd.DeleteObject acQuery, "TempLANQuery"
On Error GoTo Error_Handler
Set qdf = db.CreateQueryDef("TempLANQuery")
qdf.SQL = Me!QueryCode ' stored in tblAdminLANQueries
For Each prm In qdf.Parameters
prm.Value = Eval(prm.Name)
Next prm
If Left(Me!QueryCode, 6) = "select" Then
DoCmd.OpenQuery ("TempLANQuery")
Else
qdf.Execute
End If
MsgBox "Query executed successfully."
Exit_ExecuteQuery:
Set db = Nothing
Set qdf = Nothing
Exit Sub
Error_Handler:
MsgBox "An error occurred when attempting to execute " & Me!Description
_
& vbCrLf & vbCrLf _
& "Error number: " & Str(Err) _
& vbCrLf _
& "Description: " & Err.Description
Resume Exit_ExecuteQuery
End Sub