Question : Using ADO to connect to a database: Factors preventing connection

Dear Experts,

What could be causing this textbook example form from connecting to the Northwind database whose file path is correct?

I've just checked the reference library selections, the file type of both Databases and the code. I get a 424 error which says it cannot connect tot the database.

Line 5 is highlighted as the problem.

Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
Option Compare Database

Private Sub Form_Load()

Dim remoteConnection As New ADODB.Connection
Dim rsProducts As New ADODB.Recordset
    Connect
    SetRecordset
    
End Sub

Private Sub Form_Unload(Cancel As Integer)

    Disconnect
    
End Sub

Public Sub Disconnect()

    On Error GoTo ConnectionError
    
    rsProducts.Close
    remoteConnection.Close
    
Exit Sub

ConnectionError:

MsgBox "There was an error closing the databse." & _
    Err.Number & ", " & Err.Description
    
End Sub

Private Sub Connect()

    On Error GoTo ConnectionError
    
    With remoteConnection
        .Provider = "Microsoft.ACE.OLEDB.12.0"
        .Open "C:\Users\S B\Documents\Databases\Northwind.accdb"
        
    End With
    Exit Sub
    
ConnectionError:
    
    MsgBox "There was an error connecting to the database. " & _
        Chr(13) & Err.Number & ", " & Err.Description
        
End Sub

Public Sub SetRecordset()

Dim sql As String

On Error GoTo DbError

sql = "select * from Products"

rsProducts.CursorType = adOpenKeyset
rsProducts.LockType = adLockReadOnly


rsProducts.Open sql, remoteConnection, _
    , , adCmdText
    
    If rsProducts.EOF = False Then
    'Using three different techniques to access items in a recordset
    Me.txtProductID = rsProducts!ID
    Me.txtProductCode = rsProducts.Fields.Item("Product Code")
    Me.txtProductName = rsProducts.Fields.Item(3)
    
End If

Exit Sub

DbError:

MsgBox "There was an error retrieving information " & _
       "From the database." _
       & Err.Number & ", " & Err.Description
        
End Sub

Answer : Using ADO to connect to a database: Factors preventing connection

Hi,

Check to see that ADO is referenced for this statement.
Dim remoteConnection As New ADODB.Connection

The code below does not appear correct.  The Connect and SetRecordset are "hanging".  
Dim rsProducts As New ADODB.Recordset
    Connect
    SetRecordset

Bill
Random Solutions  
 
programming4us programming4us