Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Access Database thru Excel VBA

Posted on 2014-03-13
7
Medium Priority
?
1,610 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 1500 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 1500 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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

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!
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

730 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