Solved

Access Database thru Excel VBA

Posted on 2014-03-13
7
1,367 Views
Last Modified: 2014-03-14
Hi,

I am using Office 2013.

I want to open Access database C:\Test\Employees.accdb using Excel VBA, and run thru records, and then paste these records in Excel sheet. The table is EmployeeMaster.

How to???

San.
0
Comment
Question by:sanjay-gandhi
  • 3
  • 3
7 Comments
 
LVL 39

Expert Comment

by:nutsch
ID: 39928340
Use data / get external data, set up a connection to your access database and follow the prompts. You should be done in no time.
0
 
LVL 19

Assisted Solution

by:regmigrant
regmigrant earned 500 total points
ID: 39928399
if you need to manipulate the records before putting them into the sheet:-
Dim myTable As New Collection
Dim slq_String as String
 

 Sub GetData()

'this is the connection string which specifies db driver, db, and login details if needed
 Const ConnString As String = _
 "Provider=Microsoft.Jet.OLEDB.4.0;Data Source='path and db' For Excel.mdb;Persist Security Info=False"

sql_String  = "SELECT * FROM a_table"

 Dim Con As ADODB.Connection              '  Con is  the connection
 Dim RecordSet As ADODB.RecordSet     ' this is the records we will get

 Set Con1 = New ADODB.Connection     'sets up the connection through ado

 Con1.ConnectionString = ConnString  
 Con1.Open

 Set RecordSet = New ADODB.RecordSet
 
Call RecordSet.Open(SQL, Con1)

 With RecordSet

'check if there are any records

 If .BOF And .EOF Then
     MsgBox "There are no records"
     Exit Sub
 End If

 
 .MoveFirst


 Do While Not .EOF

   .... etc

Open in new window

0
 

Author Comment

by:sanjay-gandhi
ID: 39928471
Dear Nutsch,

I want a VBA code for this.

-San
0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
LVL 19

Expert Comment

by:regmigrant
ID: 39928474
Did you try the vba I posted?
0
 

Author Comment

by:sanjay-gandhi
ID: 39928480
Dear Regmigrant,

I get following error "User-defined type not defined" on statement "Con As ADODB.Connection".

And I have already included "Microsoft ActiveX Data Objects Recordset 6.0 library" thru  Tools > References.

-San
0
 
LVL 19

Accepted Solution

by:
regmigrant earned 500 total points
ID: 39928522
you need to reference "Microsoft ActiveX Data Objects Library" - the recordsets has additional record stuff but not the connection
0
 

Author Closing Comment

by:sanjay-gandhi
ID: 39928616
The following code finally works good - it includes the Ace library instead of Jet.

>Code
Sub GetData()

'this is the connection string which specifies db driver, db, and login details if needed
 Const ConnString As String = "Provider=Microsoft.Ace.OLEDB.12.0;Data Source='C:\Test\Employees.accdb'"
 Sql_string = "SELECT * FROM EmployeeMaster"

 Dim Con As ADODB.Connection          '  Con is  the connection
 Dim RecordSet As ADODB.RecordSet     ' this is the records we will get
 Set Con1 = New ADODB.Connection      'sets up the connection through ado

 Con1.ConnectionString = ConnString
 Con1.Open
 Set RecordSet = New ADODB.RecordSet
 
 Call RecordSet.Open(Sql_string, Con1)

 With RecordSet
     'check if there are any records
     If .BOF And .EOF Then
         MsgBox "There are no records"
         Exit Sub
     End If
   
     .MoveFirst
     NextRow = 2
     Sheets.Add
     Do While Not .EOF
        Cells(NextRow, 1).Value = .Fields(3)
        Cells(NextRow, 2).Value = .Fields(4)
        NextRow = NextRow + 1
        .MoveNext
     Loop
 End With

End Sub
>Code Ends

-San
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

792 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question