How can I test an sql statement before applying it to a query? How can I test an sql statement before applying it to a query:
The reason I am asking this question is the following:
I am trying to change the query “QryOGCGeneric” programmatically.
If I send in a correct query (txtSQL = “Select * from tblAGENCY”)
Everything is ok.
The moment I send in wrong query (txtSQL = “Select * from tblAGENCIES”)
I get an error: “Item cannot be found in the collection corresponding to the requested name or ordinal”
This error will still come regardless of whether I send in a correct
SQL statement thereafter or not.
Using:
Private Function IsQueryGood() As Boolean
Does not work either.
Private Function PopulateQry(boolDisplayQry As Boolean)
Dim strSQL As String
Dim strSQL1 As String
'Set a reference for Microsoft ADO Ext. 2.1
'for DDL and security.
Dim cat As ADOX.Catalog
Dim cmd As ADODB.Command
'Dim vwu As ADOX.View
On Error GoTo PopulateQryErr
Set cat = New ADOX.Catalog
cat.ActiveConnection = CurrentProject.Connection
'If TypeOfQuery("QryOGCGeneric") = "View" Then
'txtSQL has already been validated
Set cmd = cat.Views("QryOGCGeneric").Command
cmd.CommandText = txtSQL
Set cat.Views("QryOGCGeneric").Command = cmd
cat.Views.Refresh
Set cat = Nothing
Set cmd = Nothing
Exit_This:
On Error GoTo 0
Exit Function
PopulateQryErr:
MsgBox Err.Description
MsgBox "Error " & Err.Number & " " & Err.Description & vbCrLf & _
" Most likely a wrong query", vbCritical, "OGC"
Set cat = Nothing
Set cmd = Nothing
Resume Exit_This
End Function
Private Function IsQueryGood() As Boolean
On Error GoTo HandleErr
'DoCmd.OpenQuery strQueryName, acViewNormal
IsQueryGood = False
' Test the sql statement first
DoCmd.RunSQL txtSQL
IsQueryGood = True
ExitHere:
Exit Function
' Error handling block added by Error Handler Add-In. DO NOT EDIT this block of code.
' Automatic error handler last updated at 10-02-2001 11:50:10 'ErrorHandler:$$D=10-02-2001 'ErrorHandler:$$T=11:50:10
HandleErr:
Select Case Err.Number
Case Else
MsgBox "Error " & Err.Number & ": " & Err.Description, vbCritical, "UtilityFunctions.OpenQueryAndWait" 'ErrorHandler:$$N=UtilityFunctions.OpenQueryAndWait
End Select
' End Error handling block.
End Function
__________________ Wango |