CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jun 2003
    Location
    Planet Earth
    Posts
    186

    Establish ADO connection using VB6

    Hi,

    I'm trying to establish a connection to an Access2000 database using ADO. I'm exposing Microsoft ActiveX Data Objects 2.7 Library. What am I doing wrong?

    I keep getting the following error on the Open command shown in bold below:
    Error: -2147467259
    ErrMsg: Automation error
    Unspecified error

    Code:
        Dim dbMyDB As ADODB.Connection
        Dim rsMyRS As ADODB.Recordset    
        Set dbMyDB = CreateAccessConnection("C:\MyDataBase.mdb")
        Set rsMyRS = New ADODB.Recordset
        rsMyRS.CursorType = 0
        rsMyRS.LockType = adLockOptimistic
        rsMyRS.Open "MyTableName", dbMyDB, , , adCmdTable
    
    
    Public Function CreateAccessConnection(ByVal dbNameAndPath As String) As Connection
        Dim Conn As ADODB.Connection
        Dim ConnectString$
        Const MAX_CONN_WAIT As Integer = 90 
        Const MAX_CMD_WAIT As Integer = 300 
        
        ConnectString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & dbNameAndPath & ";Mode=ReadWrite;Persist Security Info=False"
        
        Set Conn = New ADODB.Connection
        ' open connection
        Conn.ConnectionTimeout = MAX_CONN_WAIT
        Conn.CommandTimeout = MAX_CMD_WAIT
        Conn.CursorLocation = adUseClient
        Conn.Open ConnectString
        Set CreateAccessConnection = Conn
    End Function

  2. #2
    Join Date
    May 2004
    Posts
    1
    '**** Form Level Declarations ****

    '*************************************************************************************************
    ' Be sure to add a Reference to Ms ActiveX Data Objects 2.x Library to Project
    '*************************************************************************************************

    Dim DbFile As String 'Name of DataBase
    Dim cn as ADODB.Connection 'Connect to the ADO Data Type
    Dim rs as ADODB.Recordset 'Record Source Name
    Dim SQLstmt as String 'SQL Statement String(s)

    Private Sub Form_Load()
    Open_cn
    End Sub

    Private Sub Open_cn ()
    ' Set the Database Applicable Path
    DbFile = App.Path & "\Your Database Name.mdb"

    ' Establish the Connection
    Set cn= New ADODB.Connection
    cn.CursorLocation = adUseClient
    cn.ConnectionString = _
    "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    "Data Source=" & DbFile & ";" & _
    "Persist Security Info=False"

    ' Open the Connection
    cn.Open

    ' Once this Connection is opened, it can
    ' be used throughout the application

    SQLstmt = "SELECT * FROM [YourTableName]"

    ' Get the Records
    Set rs = New ADODB.Recordset
    rs.Open SQLstmt, cn, adOpenStatic, adLockOptimistic, _
    adCmdText

    End Sub

    Private Sub Close_cn ()
    cn.Close
    Set cn = Nothing
    End Sub

  3. #3
    Join Date
    Jun 2003
    Location
    Planet Earth
    Posts
    186
    Thanks for the response. I got it solved.

    2 problems:
    1 was the initial table I was referencing was apparently corrupted when I first built it.
    2 was one of the table names I used was "Module". This also causes an error simply because of the name itself. I renamed it to "Module1" and got that solved as well.

    Turns out my initial code was fine.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured