Question : ADO vs linked dsn

Torrwin wrote on my Access question
"skip the linked tables and go straight in to SQL Server via ADO"
In access how do I go straight to SQL via ADO when populating a list box?

Is it better to fill a value list via ADODB rather then with a linked table or sqlpassthrough?


Answer : ADO vs linked dsn

It's actually pretty easy to do, the first thing you need to do is set up your connection string.  Then, add a couple of functions to send/retreive data to/from the database.  See the attached code.  Once you have the recordset from ADO, simply set your control's recordset to the one returned from ADO.

I don't use linked tables at all.  I find it much more efficient to keep everything in SQL.
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:
'Connection string utilizing a DSN-less connection
Public Function GetConnectString() As String
    GetConnectString = "Driver={SQL Server};Server=MyServer;Database=MyDatabase;Uid=MyUser;Pwd=MyPassword"
End Function
 
'Use the given SQL statement to return a recordset
Public Function GetRecordset(ByVal sql As String) As ADODB.Recordset
    On Error GoTo Error_Handler
 
    Dim myConn As ADODB.Connection
    Set myConn = New ADODB.Connection
    myConn.Open GetConnectString
    
    Dim rsTemp As ADODB.Recordset
    Set rsTemp = New ADODB.Recordset
    
    
    Set GetRecordset = rsTemp.Open sql, m_oConn, adOpenStatic, adLockReadOnly
    
Clean_Up:
    rsTemp.ActiveConnection = Nothing
    myConn.Close
 
    Set rsTemp = Nothing
    
    Exit Function
    
Error_Handler:
    MsgBox "Error Number: " & Err.number & vbCrLf & Err.Description
    
    GoTo Clean_Up
End Function
 
'Execute an INSERT/UPDATE/ETC statement in a transaction
Public Sub RunSQL(ByVal sql As String)
    On Error GoTo Error_Handler:
    
    Dim myConn As ADODB.Connection
    Set myConn = New ADODB.Connection
    myConn.Open GetConnectString
    
    myConn.BeginTrans
    
    myConn.Execute sql
    
Clean_Up:
 
    myConn.CommitTrans
    myConn.Close
 
    Set myConn = Nothing
 
    Exit Sub
    
Error_Handler:
    MsgBox "Error Number: " & Err.number & vbCrLf & Err.Description
    
    myConn.RollbackTrans
    GoTo Clean_Up
End Sub
Random Solutions  
 
programming4us programming4us