ADODB错误处理变量未设置 [英] ADODB error handling variable not setting
问题描述
我建立了一个ADODB错误陷阱,但是由于某种原因,通过errSQL.Number和errSQL.Description都给了我一个对象变量或未设置Block变量。错误....到目前为止,这是我的代码...我启用了活动的x对象,并且我认为.number和.description是正确的...任何帮助都将非常棒!我也正在运行的查询会故意发送错误。
I building an ADODB error trap but for some reason, by errSQL.Number and errSQL.Description both give me a "Object variable or With Block variable not set." error....here is my code so far...I have the active x object enabled and I thought that .number and .description are correct...any help would be awesome! The query I'm running also purposely will send an error.
当我注释掉错误陷阱时,我确实收到一个带有SQL语法错误的消息框,但似乎无法像下面那样将其陷阱...
When I comment the error trap out, I do get a message box with a SQL syntax error but can't seem to trap it like below...
Public errSQL As ADODB.Error
Public strErrODBC As String
Private Sub verifySQL()
Dim strSQL2 As String
Dim cn As New ADODB.Connection
Dim cdTxt As String
Dim rs As New ADODB.Recordset
Dim intVerify As Integer
On Error GoTo ODBCErrorHandler
cn.ConnectionString = "DSN=source;"
cn.Open
If cn.State = adStateOpen Then
rs.Open "SELECT CASE WHEN MAX((CASE WHEN " & Forms!dlgSplitName.lstbxFlds.Column(0) & " " & cdTxt & " THEN 1 ELSE 0 END)) =1 THEN 1 ELSE 0 END FROM table;", cn
Else
End If
intVerify = rs.Fields(0).Value
If intVerify = 1 Then
insrt_Test
ElseIf intVerify = 0 Then
MsgBox "No records were found with the code text logic.", vbExclamation + vbOKOnly, "Spliy by Notification"
End If
ODBCErrorHandler:
Debug.Print errSQL.Number
Debug.Print errSQL.Description
strErrODBC = "A SQL error was encountered with the code text logic." & vbCrLf
strErrODBC = strErrODBC & "Error " & errSQL.Number & vbCrLf
strErrODBC = strErrODBC & " " & errSQL.Description
MsgBox strErrODBC & vbCrLf & "Please try again.", vbCritical, "Split by field code text error."
cn.Close
End Sub
推荐答案
问题是errSQL ADODB Error对象从未设置为任何对象。 Connection对象具有一个错误集合,您需要使用该错误集合来显示错误。尝试以下操作:
The problem is that the errSQL ADODB Error object is never set to anything. the Connection object has an error collection that you need to use to display the errors. Try this:
ODBCErrorHandler:
Dim ErrorCount As Double
Dim strError As String
ErrorCount = cn.Errors.Count
strErrODBC = "A SQL error was encountered with the code text logic." & vbCrLf
If ErrorCount > 0 Then
For index = 0 To (ErrorCount - 1)
Set errSQL = cn.Errors.Item(index)
strErrODBC = strErrODBC & "Error " & errSQL.Number & vbCrLf
strErrODBC = strErrODBC & " " & errSQL.Description & vbCrLf
Next index
End If
MsgBox strErrODBC & vbCrLf & "Please try again.", vbCritical, "Split by field code text error."
cn.Close
希望这会有所帮助。
这篇关于ADODB错误处理变量未设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!