Solved

Access Database thru Excel VBA

Posted on 2014-03-13
7
1,312 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

895 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