使用VBA建立新的数据库连接

使用VBA建立新的数据库连接,vba,connection,Vba,Connection,子数据库连接() Dim C作为整数 朦胧的氛围 将用户ID设置为字符串 将密码设置为字符串 将查询设置为字符串 Dim Newsht As工作表 Dim Conn作为ADODB连接 作为ADODB.Recordset的Dim Rcrdst Set Newsht=ActiveWorkbook.Sheets(“sheet1”) userid=InputBox(“请插入CSDG4环境的用户ID。”,“测试”) password=InputBox(“请插入与“&userid&“user.”、“Test

子数据库连接()

Dim C作为整数
朦胧的氛围
将用户ID设置为字符串
将密码设置为字符串
将查询设置为字符串
Dim Newsht As工作表
Dim Conn作为ADODB连接
作为ADODB.Recordset的Dim Rcrdst
Set Newsht=ActiveWorkbook.Sheets(“sheet1”)
userid=InputBox(“请插入CSDG4环境的用户ID。”,“测试”)
password=InputBox(“请插入与“&userid&“user.”、“Test”相关的密码”)
ambiente=“CSDG4”
如果用户名为“”,密码为“”,则
Set Conn=新的ADODB.连接
Conn.ConnectionString=“Provider=MSDAORA;Password=“&Password&”User ID=“&userid&”Data Source=“&ambiente&”Persist Security Info=True”
康涅狄格州公开赛
Query=“从via.prenotazione中选择seq_prenotazione、cod_reporto、stato_pren,其中seq_prenotazione位于(700016298527、700016761977);”
Set Rcrdst=New ADODB.Recordset
Rcrdst.CursorLocation=adUseClient
Rcrdst.CursorType=adOpenStatic
Rcrdst.LockType=adlockBatch
Rcrdst.Source=查询
Rcrdst.ActiveConnection=Conn
Rcrdst.打开
我试图使用VBA打开一个新连接,但语句“Rcrdst.open”给出如下错误


答案并非如此,但您正在使用的对象、连接和记录集都有事件发生,这些事件发生在连接/数据操作等过程中。因此,就我个人而言,在学习此类新知识时,我会将它们封装在我自己的类中,以帮助捕获发生错误的地方。例如,这并没有经过测试,但对于ADO,我会使用类似这样的类clsADOTest来编写代码

Option Explicit

Private WithEvents CONN As ADODB.Connection         '   Allow us to couple code we write to the events of an object
Private WithEvents rst As ADODB.Recordset
Private strUserID As String
Private strPwd As String

Public Property Let UserName(strUserName As String)
    strUserID = strUserName
End Property
Public Property Let Password(strPassword As String)
    strPwd = strPassword
End Property

Public Property Let ConnectionString(strConnectionString As String)
    CONN.ConnectionString = strConnectionString
End Property

Public Sub class_initialize()
    Set CONN = New ADODB.Connection
End Sub

Private Sub Class_Terminate()
    If Not CONN Is Nothing Then
        If CONN.State <> adStateClosed Then
            CONN.Close
        End If
        Set CONN = Nothing
    End If
End Sub

'   Simulated ADO Methods
'   Wrappers round existing ADO Methods used in SO question

Public Function OPEN_CONNECTION() As Boolean
On Error GoTo eHandle
If Not CONN Is Nothing Then
    CONN.Open
End If
OPEN_CONNECTION = True
Exit Function
eHandle:
    OPEN_CONNECTION = False
End Function

Public Function EXECUTE_SQL(strSQL As String) As ADODB.Recordset
    Set EXECUTE_SQL = CONN.Execute(strSQL)
End Function

Public Function CLOSE_CONNECTION() As Boolean
If Not CONN Is Nothing And CONN.State <> adStateClosed Then
    CONN.Close
End If
End Function

'   ADO Events
Private Sub CONN_ConnectComplete(ByVal pError As ADODB.Error, adStatus As ADODB.EventStatusEnum, ByVal pConnection As ADODB.Connection)

If Not pError Is Nothing Then
    MsgBox "Error in connection"
Else
    Debug.Print "Connected to " & pConnection.ConnectionString
End If

End Sub

Private Sub CONN_ExecuteComplete(ByVal RecordsAffected As Long, ByVal pError As ADODB.Error, _
                            adStatus As ADODB.EventStatusEnum, ByVal pCommand As ADODB.Command, _
                            ByVal pRecordset As ADODB.Recordset, ByVal pConnection As ADODB.Connection)

If Not pError Is Nothing Then
    MsgBox "Error in SQL"
Else
    If Not pRecordset Is Nothing Then
        Debug.Print pRecordset.RecordCount & " records returned from " & pCommand.CommandText
    Else
        Debug.Print "Execute complete"
    End If
End If

End Sub

不是这样的答案,但是您正在使用的对象,您的连接和记录集,都有在连接/数据操作等过程中发生的事件。因此,就我个人而言,在学习类似这样的新事物时,我会将它们打包到我自己的类中,以帮助捕获发生错误的地方。例如,这并没有经过测试,但对于ADO,我会使用类似这样的类clsADOTest来编写代码

Option Explicit

Private WithEvents CONN As ADODB.Connection         '   Allow us to couple code we write to the events of an object
Private WithEvents rst As ADODB.Recordset
Private strUserID As String
Private strPwd As String

Public Property Let UserName(strUserName As String)
    strUserID = strUserName
End Property
Public Property Let Password(strPassword As String)
    strPwd = strPassword
End Property

Public Property Let ConnectionString(strConnectionString As String)
    CONN.ConnectionString = strConnectionString
End Property

Public Sub class_initialize()
    Set CONN = New ADODB.Connection
End Sub

Private Sub Class_Terminate()
    If Not CONN Is Nothing Then
        If CONN.State <> adStateClosed Then
            CONN.Close
        End If
        Set CONN = Nothing
    End If
End Sub

'   Simulated ADO Methods
'   Wrappers round existing ADO Methods used in SO question

Public Function OPEN_CONNECTION() As Boolean
On Error GoTo eHandle
If Not CONN Is Nothing Then
    CONN.Open
End If
OPEN_CONNECTION = True
Exit Function
eHandle:
    OPEN_CONNECTION = False
End Function

Public Function EXECUTE_SQL(strSQL As String) As ADODB.Recordset
    Set EXECUTE_SQL = CONN.Execute(strSQL)
End Function

Public Function CLOSE_CONNECTION() As Boolean
If Not CONN Is Nothing And CONN.State <> adStateClosed Then
    CONN.Close
End If
End Function

'   ADO Events
Private Sub CONN_ConnectComplete(ByVal pError As ADODB.Error, adStatus As ADODB.EventStatusEnum, ByVal pConnection As ADODB.Connection)

If Not pError Is Nothing Then
    MsgBox "Error in connection"
Else
    Debug.Print "Connected to " & pConnection.ConnectionString
End If

End Sub

Private Sub CONN_ExecuteComplete(ByVal RecordsAffected As Long, ByVal pError As ADODB.Error, _
                            adStatus As ADODB.EventStatusEnum, ByVal pCommand As ADODB.Command, _
                            ByVal pRecordset As ADODB.Recordset, ByVal pConnection As ADODB.Connection)

If Not pError Is Nothing Then
    MsgBox "Error in SQL"
Else
    If Not pRecordset Is Nothing Then
        Debug.Print pRecordset.RecordCount & " records returned from " & pCommand.CommandText
    Else
        Debug.Print "Execute complete"
    End If
End If

End Sub

这是一个SQL问题,那么我相信它来自记录集打开,连接是否正常。打开行工作吗?是的连接。打开绝对正常。这是一个SQL问题,那么我相信它来自记录集打开,连接是否正常。打开行工作吗?是的连接。打开绝对正常
Sub testing()

Dim ADOClass As New clsADOTest
Dim rst As ADODB.Recordset

With ADOClass

    .UserName = InputBox("Please insert your USER ID for CSDG4 environment.", "Test")
    .Password = InputBox("Please insert the PASSWORD related to " & .UserName & " user.", "Test")
    .ConnectionString = ""

    If .OPEN_CONNECTION Then
        Set rst = ADOClass.EXECUTE_SQL("select seq_prenotazione, cod_rapporto,stato_pren from.....")
    Else

    End If

End With

Set ADOClass = Nothing

End Sub