Solved

Access Database thru Excel VBA

Posted on 2014-03-13
7
1,427 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Suggested Solutions

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
This article describes a serious pitfall that can happen when deleting shapes using VBA.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

751 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