简体   繁体   English

从Excel打开ADODB连接-错误424

[英]Opening ADODB Connection from Excel - Error 424

Am trying to create a generic function to return an open connection. 我试图创建一个泛型函数以返回打开的连接。 However, code bugs out at the last line when i set the function to the connection object. 但是,当我将功能设置为连接对象时,代码会在最后一行出错误。 Need some directions to get this going. 需要一些指导来实现这一目标。 Thank you! 谢谢!

''
' Function to open an ADODB connection and return the connection object
' @param    strDBPath string containing full path to database of interest
' @param    strUserID optional string containing user name to use when establishing the connection (default: vbNullString)
' @param    strPassword optional string containing password to use when establishing the connection (default: vbNullString)
' @param    intOptionsEnum optional integer to open the connection synchronously (-1) (defualt) - 16 to open connection asynchronously
' @return   Object containing opened ADODB connection
' @remarks  Have only tested this on .mdb database (not .accdb)
Public Function vfnc_StartConnection( _
    strDBPath As String, _
    Optional strUserID As String = vbNullString, _
    Optional strPassword As String = vbNullString, _
    Optional intOptionsEnum As Integer = -1 _
    ) As Object

    Dim objConn As Object: Set objConn = CreateObject("ADODB.connection")
    Dim strDataSource As String: strDataSource = "Data Source=" & strDBPath & ";"
    '#If VB7 And Win64 Then
        strProvider = "Provider=Microsoft.ACE.OLEDB.12.0; "
    '#Else
        'strProvider = "Provider=Microsoft.Jet.OLEDB.4.0; "
    '#End If
    Set vfnc_StartConnection = objConn.Open(strProvider & strDataSource, strUserID, strPassword, intOptionsEnum) 'Error occurs here
End Function

The Open method of the Connection object does not return an object. Connection对象的Open方法不返回对象。 Therefore it isn't appropriate to set something to objConn.Open . 因此,将某些内容设置为objConn.Open是不合适的。

You would probably just want to return the opened objConn object itself, ie 您可能只想返回打开的objConn对象本身,即

    'Open the connection
    objConn.Open strProvider & strDataSource, strUserID, strPassword, intOptionsEnum

    'Now that the connection has been opened, return the connection to the calling routine
    Set vfnc_StartConnection = objConn

Here is my take on that, using ConnectionString properity, partially based on answer by YowE3K. 这是我的看法,使用ConnectionString属性,部分基于YowE3K的回答。

Public Function vfnc_StartConnection(strDBPath As String, Optional strUserID As String = "admin", Optional strPassword As String = vbNullString, Optional intOptionsEnum As Integer = -1) As ADODB.Connection

    Dim objConn As ADODB.Connection
    Set objConn = New ADODB.Connection

    Dim strDataSource As String
    Dim strprovider As String
    strDataSource = "Data Source=" & strDBPath & ";"
    strprovider = "Provider=Microsoft.ACE.OLEDB.12.0; "

    objConn.ConnectionString = strprovider & strDataSource & ";User ID=" & strUserID & ";Password=" & strPassword & ";"
    objConn.Open

    Set vfnc_StartConnection = objConn
End Function

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM