View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Dave Patrick Dave Patrick is offline
external usenet poster
 
Posts: 249
Default Code for getting Access recordset into Excel

'Give this a go. Don't forget to; Tools|References and check the box for
'Microsoft ActiveX Data Objects 2.x Library'

Use the highest version that will still support your clients.


Sub ReadAccess()

Const adLockReadOnly = 1
Const adOpenForwardOnly = 0

Dim strConnect As String, strSQL As String, i As Integer

strConnect = "Provider=Microsoft.Jet.OLEDB.4.0;Data " _
& "Source=C:\Program Files\Microsoft " _
& "Office\Office12\SAMPLES\Northwind.mdb;" _
& "Persist Security Info=False"

Sheets(1).Range("B1") = "eastc"
strSQL = "SELECT Customers.ContactName " _
& "FROM Customers " _
& "WHERE (((Customers.CustomerID)='" & Sheets(1).Range("b1") & "')); "

Set objConnection = CreateObject("ADODB.Connection")
Set objRecordset = CreateObject("ADODB.Recordset")

i = 1
objConnection.Open strConnect
objRecordset.Open strSQL, objConnection, _
adOpenForwardOnly, adLockReadOnly
Do While objRecordset.EOF = False
Sheets(1).Range("A" & i) = objRecordset!ContactName
i = i + 1
objRecordset.MoveNext
Loop
objRecordset.Close
objConnection.Close
End Sub


--

Regards,

Dave Patrick ....Please no email replies - reply in newsgroup.
Microsoft Certified Professional
Microsoft MVP [Windows]
http://www.microsoft.com/protect

"Matt" wrote:
I am looking to see if some one can give me some basic code, using
ADO, that will query an Access database using a cell value as a
variable and then returning a recordset value to a cell. i.e.

cell A1= employeeNumber
using employee number, query the tblEmployee and return the Employee
name in B2

any help is appreciated. If anyone know of a good tutorial site for
doing this type of thing i would appreciate it.

thanks