PC Review


Reply
Thread Tools Rate Thread

Code for getting Access recordset into Excel

 
 
Matt
Guest
Posts: n/a
 
      24th Jun 2007
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

 
Reply With Quote
 
 
 
 
Dave Patrick
Guest
Posts: n/a
 
      24th Jun 2007
'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
>


 
Reply With Quote
 
Matt
Guest
Posts: n/a
 
      24th Jun 2007
On Jun 23, 6:49 pm, "Dave Patrick" <DSPatr...@nospam.gmail.com> wrote:
> '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- Hide quoted text -

>
> - Show quoted text -


I will it give it a try! Thanks!

 
Reply With Quote
 
Dave Patrick
Guest
Posts: n/a
 
      24th Jun 2007
You're welcome.

--

Regards,

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

"Matt" wrote:
> I will it give it a try! Thanks!
>

 
Reply With Quote
 
=?Utf-8?B?TWlrZQ==?=
Guest
Posts: n/a
 
      24th Jun 2007
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
>
>

 
Reply With Quote
 
Matt
Guest
Posts: n/a
 
      24th Jun 2007
On Jun 23, 7:35 pm, Mike <M...@discussions.microsoft.com> wrote:
> 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- Hide quoted text -

>
> - Show quoted text -


Thanks! I am just trying to understand what i=i+1 is about. Is that
there for the loop in case I am trying to pull more than the Name?
ie, if I wanted ss# then it would be placed in b3...etc??

 
Reply With Quote
 
=?Utf-8?B?TWlrZQ==?=
Guest
Posts: n/a
 
      24th Jun 2007
the i = i +1 is a loop to get next row in case you were pulling more then
one employee at a time
If you look at the top of the code you will see i = 2 meaning row 2
If your wanting to add something else you would use
Range("C" & i) = rs1!SocialNumber
Range("D" & i) = rs1!DateOfHire
and so on
"Matt" wrote:

> On Jun 23, 7:35 pm, Mike <M...@discussions.microsoft.com> wrote:
> > 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- Hide quoted text -

> >
> > - Show quoted text -

>
> Thanks! I am just trying to understand what i=i+1 is about. Is that
> there for the loop in case I am trying to pull more than the Name?
> ie, if I wanted ss# then it would be placed in b3...etc??
>
>

 
Reply With Quote
 
Matt
Guest
Posts: n/a
 
      24th Jun 2007
On Jun 23, 8:25 pm, Mike <M...@discussions.microsoft.com> wrote:
> the i = i +1 is a loop to get next row in case you were pulling more then
> one employee at a time
> If you look at the top of the code you will see i = 2 meaning row 2
> If your wanting to add something else you would use
> Range("C" & i) = rs1!SocialNumber
> Range("D" & i) = rs1!DateOfHire
> and so on
>
>
>
> "Matt" wrote:
> > On Jun 23, 7:35 pm, Mike <M...@discussions.microsoft.com> wrote:
> > > 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- Hide quoted text -

>
> > > - Show quoted text -

>
> > Thanks! I am just trying to understand what i=i+1 is about. Is that
> > there for the loop in case I am trying to pull more than the Name?
> > ie, if I wanted ss# then it would be placed in b3...etc??- Hide quoted text -

>
> - Show quoted text -


thanks again for the explanation and your time!

 
Reply With Quote
 
Matt
Guest
Posts: n/a
 
      25th Jun 2007
On Jun 23, 9:19 pm, Matt <mwagn...@gmail.com> wrote:
> On Jun 23, 8:25 pm, Mike <M...@discussions.microsoft.com> wrote:
>
>
>
>
>
> > the i = i +1 is a loop to get next row in case you were pulling more then
> > one employee at a time
> > If you look at the top of the code you will see i = 2 meaning row 2
> > If your wanting to add something else you would use
> > Range("C" & i) = rs1!SocialNumber
> > Range("D" & i) = rs1!DateOfHire
> > and so on

>
> > "Matt" wrote:
> > > On Jun 23, 7:35 pm, Mike <M...@discussions.microsoft.com> wrote:
> > > > 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- Hide quoted text -

>
> > > > - Show quoted text -

>
> > > Thanks! I am just trying to understand what i=i+1 is about. Is that
> > > there for the loop in case I am trying to pull more than the Name?
> > > ie, if I wanted ss# then it would be placed in b3...etc??- Hide quoted text -

>
> > - Show quoted text -

>
> thanks again for the explanation and your time!- Hide quoted text -
>
> - Show quoted text -


Everything worked great! Now I have kinda of taken this up a notch
into a differenent scenario. I want to get values from an Access
table into a list box or combo box. I have a jobNumber that a user
would input and that would need to return all of the possible JobAreas
it has into a Combo Box on my worksheet. Ideas?

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Set Access 2003 Listbox recordset via code? kiln Microsoft Access Form Coding 9 5th Jun 2005 08:14 PM
Access Recordset Rows to Excel Geoff Microsoft Excel Programming 1 25th May 2005 05:41 PM
open access recordset as an excel spreadsheet =?Utf-8?B?dG1vcnQ=?= Microsoft Access VBA Modules 21 8th Apr 2005 10:01 PM
can't access recordset from report code module? Randall Arnold Microsoft Access Reports 6 19th Apr 2004 01:05 PM
How do I open a recordset and step through it in Access 2000 code =?Utf-8?B?UmljaCBNYXp6YQ==?= Microsoft Access VBA Modules 7 24th Feb 2004 11:18 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:03 PM.