Solved

EXCEL ACCESS VBA data transfer

Posted on 2013-05-14
3
405 Views
Last Modified: 2013-05-14
I am looking for any code example which will show me how to connect to an Access database from Excel and copy a field from an Access query and paste it into a field in an Excel worksheet
0
Comment
Question by:Idarac
  • 2
3 Comments
 
LVL 119

Accepted Solution

by:
Rey Obrero earned 500 total points
Comment Utility
place this codes in your excel module and replace the variables to suit your requirements


Sub Open_Query()
    Dim db As Database, rs As Recordset, qd As QueryDef
    Dim ws As Object, i As Integer
    Dim dbPath As String
    dbPath = "<path to your access db>"
    Set db = OpenDatabase(dbPath, ReadOnly:=True)
   
    Set qd = db.QueryDefs("QueryName")
    Set rs = qd.OpenRecordset()
    Set ws = Sheets("sheet1")
    ws.Activate
 
    Range("A1").Select
       
        For i = 0 To rs.Fields.Count - 1
            ws.Cells(1, i + 1).Value = rs.Fields(i).Name
        Next

        ws.Range("A2").CopyFromRecordset rs


       qd.Close
       rs.Close
       db.Close
       
End Sub
0
 
LVL 1

Author Comment

by:Idarac
Comment Utility
Yep works great just pasted in and it worked.
0
 
LVL 1

Author Closing Comment

by:Idarac
Comment Utility
Thank you for your quick response
0

Featured Post

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.

763 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

10 Experts available now in Live!

Get 1:1 Help Now