Results 1 to 3 of 3
  1. #1
    Join Date
    Jun 2004

    Red face Unanswered: Connect to Access with VB6

    I'm new to this and I found this code on how to connect to the database. But everytime I run it, it'll have a compile error. Did I miss out some steps or something?

    Dim MyConn As ADODB.Connection

    Set MyConn = New ADODB.Connection
    MyConn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\MyAccessDB.mdb;"

  2. #2
    Join Date
    Feb 2004
    In front of the computer
    Provided Answers: 54
    Maybe. What is the exact message that the compiler gives (use cut-and-paste if you can)?


  3. #3
    Join Date
    Jul 2001
    without seeing what the error is, its difficult to help you, but maybe these steps will help you.

    1. make sure you add to your references:
    Microsoft ActiveX Data Objects 2.5 Library (or higher version)

    2. Add the following 3 subrouties to the .bas file to make connection work easier. make all variables PUBLIC
    Sub CONXMAKE()
    Set Xconx = New ADODB.Connection
    Set Xcmd = New ADODB.Command
    Set Xrs = New ADODB.Recordset
    Set Xconx = CreateObject("ADODB.Connection")
    Xconx.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    "Persist Security Info=False;" & _
    "Data Source=" & m_DRV & ":\" & m_DIR & "\" & m_MDB
    Set Xrs = CreateObject("ADODB.Recordset")
    Xrs.CursorLocation = adUseServer
    End Sub

    Sub CONXDROP()
    Set Xconx = Nothing
    Set Xcmd = Nothing
    Set Xrs = Nothing
    End Sub

    Sub CHECKRS()
    If Xrs.State = adStateOpen Then Xrs.Close
    End Sub

    3. generate the sql statement in the form, make the connection, if is is not made already (then use checkrs, for when you make a series of cmds, like updating a table then writing to a log) and open the recordset.
    m_strSql = "SELECT * FROM ......"
    Xrs.Open m_strSQL, Xconx, adOpenStatic, adLockReadOnly, -1

    4. drop the connection with CONXDROP

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts