简体   繁体   中英

vba ActiveX can't create odbc.connection object

I'm trying to create ODBC connection in order to be able to perform a simple sql query on one of the tables from my SQL DB.

Below is my code and I'm not sure what I'm doing wrong but I keep getting ActiveX can't create an object error and the following line is highlighted: Set con = CreateObject("ODBC.Connection") .

Private Sub findBtn_Click()

    Dim s
    Dim con As Object 'OdbcConnection
    Dim strCon
    Dim rsSearch
    Dim strSql
    Dim mystring As String
    Dim cmd 'As OdbcCommand

    s = Me.findTxt

    mystring = "Select * from CUSTOMER where CUSTOMER.FORENAME1 like '%" & s &   "%';"

    Set con = CreateObject("ODBC.Connection")

    con.Open "ODBC;Driver={SQL Server};" & _
    "Server=localhost;" & _
    "Database=Customers23;" & _
    "UID=admin;" & _
    "PWD=admin;"

    Set cmd = CreateObject("ODBC.Command")
    cmd = mystring
    Me.resTxt = "Connected!"
    MsgBox ("Connected")

End Sub

Try to replace ODBC.Connection with ADODB.Connection.

Same with ODBC.Command

You can find an example here How do I setup an ADODB connection to SQL Server 2008 in Microsoft Access 2010?

Also see that link on how to use the Command object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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