Tuesday, May 6, 2008

How to use SQL from Excel to Access

Sub Button1_Click()
Dim cn As New ADODB.Connection
cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=C:\Documents and Settings\jwaxman\My Documents\db1.mdb;" & _
"User Id=;" & _
"Password="
cn.Open

Dim cmd As New ADODB.Command
Dim rs As ADODB.Recordset
cmd.ActiveConnection = cn
cmd.CommandText = "Select * from Cust"
Set rs = cmd.Execute

'$#,##0_);[Red]($#,##0);[blue]General

Dim I As Integer
I = 1
While Not rs.EOF
Range("A" & I).Value = rs("Name") & " " & rs("Age")
rs.MoveNext
I = I + 1
Wend
End Sub


Homework with a scan will be up shortly

No comments: