Solved

Access Database thru Excel VBA

Posted on 2014-03-13
7
1,287 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
Comment Utility
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
Comment Utility
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
Comment Utility
Dear Nutsch,

I want a VBA code for this.

-San
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 19

Expert Comment

by:regmigrant
Comment Utility
Did you try the vba I posted?
0
 

Author Comment

by:sanjay-gandhi
Comment Utility
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
Comment Utility
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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Drop Down List with Unique/Distinct Values (Part II - ComboBox or ListBox and Data Validation List Bonus!) David Miller (dlmille) Intro This article focuses on delivering unique, sorted lists to list objects (e.g., ComboBox, ListBox) and Dat…
How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
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.

771 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now