database connection question

  • Thread starter Thread starter Charles Law
  • Start date Start date
C

Charles Law

how to connect to and get data from sql server express 2008 using VBA?

Thanks
Charles
 
this will get you started. Open the dev environment (ALT+F11) then set a
reference (Tools / References) to the Microsoft Active Data Objects 2.6
Library

paste this into a new sub:
SUB Demo()
Dim rst As ADODB.Recordset
Dim db As Connection
Dim SQL As String
Set db = New Connection

With db
.CursorLocation = adUseClient
.Open "PROVIDER=MSDASQL;driver={SQL
Server};server=Patrick-PC;uid=;pwd=;database=MyDatabase;"
End With

Set rst = New Recordset
SQL = "select * from products"
rst.Open SQL, db, adOpenStatic, adLockOptimistic
range("A1").CopyFromRecordset rst

rst.Close
db.Close
set rst = Nothing
set db = Nothing
End Sub
 
Back
Top