Neil,
Something like the following code usually works for me. You would need an
appropriate provider to be installed on your machine (I believe they come
with MDAKs), and Microsoft ActiveX Data Objects to be included in references
to your project (Tools-References, when in Module Design view)
Sub ADOtest()
Dim con As ADODB.Connection ' or you can use "New" here, then you would not
need to Set
Dim rs As ADODB.Recordset ' same for this one
Set con = New ADODB.Connection
con.ConnectionString = "Provider=SQLOLEDB;Password=password;User
ID=userlogin;" & _
"Persist Security Info=True;Data
Source=servername;Initial Catalog=dbname"
' SQLOLEDB is provider for SQL server, and other providers are:
'
' MSDASQL for ODBC, in this case string looks like this:
' "Provider=MSDASQL;Driver={SQL Server};SERVER=server;" & _
' "DATABASE=database;UID=userlogin;PWD=password;"
'
' Microsoft.Jet.OLEDB.4.0 (for Jet 4.0), string like this:
' Provider=Microsoft.Jet.OLEDB.4.0;" & _
' "Data Source=database path and name;" & _
' "Jet OLEDB:Database Password=password;"
'
'
con.Open
Set rs = New ADODB.Recordset
rs.Open "table name/SQL statement", con, adOpenForwardOnly, adLockReadOnly '
or other settings
With rs
Do While Not .EOF
Debug.Print ![field name] ' or whatever you want to do with a record
.MoveNext
Loop
.Close
End With
Set rs = Nothing
con.Close
Set con = Nothing
End Sub
Hope this helps.
Alex
Quote:
> I need to be able to use ADO within a Access 97 module, but i don't know
who
> to do this. Can anybody help? All I want is to open a connection, and then
> do a select. Can anybody supply some sample code?
> I thought somthing like the following would be correct. But obviously not.
> Dim ex1 as Object, ex2 as Object
> Set Exl = CreateObject("ADODB.Connection")
> Set ex2 = CreateObject("ADODB.RecordSet")
> ex1.Open "DSN=dsname;UID=username;DATABASE=dbname;Trusted_Connection=Yes"
> ex2= ex1.Execute("select * from tbldevDocuments")
> ex2.MoveNext
> ex1.Close