View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
Mike Mike is offline
external usenet poster
 
Posts: 3,101
Default Code for getting Access recordset into Excel

Try this
Sub GetEmployeeName()

Dim cnn As ADODB.Connection
Dim rs1 As ADODB.Recordset
Dim strSQL1 As String, strConn
Dim i As Integer
Dim employeeID As String

i = 2
'used to get employee ID Number
employeeID = Range("A1").Value

'you could also use this just remove the '
'employeeID = InputBox("Select employee number", "Get Employee Number",
"<Employee Number")

'Change data source path to the path of you database
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" _
& "Data Source=C:\PathTO\MyDataBase\" _
& "Employee.mdb;Persist Security Info=False"

'You will need to modify this section the Select , From and Where
'you do not need to modify =" & employeeID & ")); " part of code
'if i knew the table and fieds i could help a little more

strSQL1 = "SELECT Employee.NAME, tblEmployee .EMPLOYEE_ID " _
& "FROM tblEmployee " _
& "WHERE (((tblEmployee .EMPLOYEE_ID)=" & employeeID & ")); "


Set cnn = New ADODB.Connection
Set rs1 = New ADODB.Recordset
cnn.Open strConn
rs1.Open strSQL1, cnn, adOpenForwardOnly, adLockReadOnly
Do While rs1.EOF = False
Range("B" & i) = rs1!Name

i = i + 1
rs1.MoveNext
Loop
rs1.Close
cnn.Close
End Sub

"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