+ Reply to Thread
Results 1 to 5 of 5

VBA in Excel to import Access Database

Hybrid View

jmzis4me VBA in Excel to import Access... 09-26-2014, 04:04 PM
ranman256 Re: VBA in Excel to import... 09-26-2014, 04:13 PM
jmzis4me Re: VBA in Excel to import... 09-26-2014, 04:25 PM
jmzis4me Re: VBA in Excel to import... 09-26-2014, 04:32 PM
jmzis4me Re: VBA in Excel to import... 09-26-2014, 04:39 PM
  1. #1
    Registered User
    Join Date
    04-10-2013
    Location
    Los Angeles
    MS-Off Ver
    Excel 2010
    Posts
    32

    VBA in Excel to import Access Database

    Hello Lifesavers,


    I'm hoping this is simple, as I believe it is common....

    I have an Access Query, we'll just call it QueryA.

    All I want to do is have VBA code on an ActiveX button so when it presses, it runs QueryA from Access, then pastes the results on an excel sheet. We'll say I want it to paste it onto Sheet1 in Book1.

    I have the QueryA SQL code (from what I read that might be needed), and the path to the file on hand. But outside of that, I have no clue where to start with the VBA code.

    PLEASE HELP!

    Thank you!!

  2. #2
    Valued Forum Contributor ranman256's Avatar
    Join Date
    07-29-2012
    Location
    Kentucky
    MS-Off Ver
    Excel 2003
    Posts
    1,192

    Re: VBA in Excel to import Access Database

    You have to have the ADO objects checked via the VBE menu, tools, references.

    Public Sub CopyRST()
    Dim con As ADODB.Connection
    Dim rs As ADODB.Recordset
    Dim DB
    Dim vProvid
    
    Set con = New ADODB.Connection
    Set rs = New ADODB.Recordset
     
     uid = "HomerJSimpson"
     pwd = ""
     DB = "\\myserver\production\Generic104J.mdb"
     vProvid = "Microsoft.Jet.OLEDB.4.0"     '  "SQLOLEDB"
     
      
    With con
        .Provider = "Microsoft.Jet.OLEDB.4.0"
        .Properties("User ID").Value = uid
        .Properties("Password").Value = pwd
        
        .Open "Data Source=" & DB 
    End With
     
    Set rs = con.Execute("QueryA")
    ActiveWorkbook.Worksheets("Sheet1").Range("A1").CopyFromRecordset rs
    
    rs.Close
    con.Close
    
    set rs = nothing
    set con = nothing
    End Sub

  3. #3
    Registered User
    Join Date
    04-10-2013
    Location
    Los Angeles
    MS-Off Ver
    Excel 2010
    Posts
    32

    Re: VBA in Excel to import Access Database

    Thank you, I put that in and checked off the box to allow it. For these:

    .Provider = "Microsoft.Jet.OLEDB.4.0"
    .Properties("User ID").Value = uid
    .Properties("Password").Value = pwd

    .Open "Data Source=" & DB

    Am I supposed to overwrite with my information in any of the quotations here? I was unsure and when I tried to run it it stopped me at the .Open giving me a runtime error "The workgroup information file is missing". I am sure its a simple obvious thing, but I haven't seen this code before so I'm intuitive things, are probably not for me.
    Thank you again for your help.

  4. #4
    Registered User
    Join Date
    04-10-2013
    Location
    Los Angeles
    MS-Off Ver
    Excel 2010
    Posts
    32

    Re: VBA in Excel to import Access Database

    Also, just to verify, you don't Dim DB or vProvid as anything?

  5. #5
    Registered User
    Join Date
    04-10-2013
    Location
    Los Angeles
    MS-Off Ver
    Excel 2010
    Posts
    32

    Re: VBA in Excel to import Access Database

    Nevermind. Got it. I didn't need a userid or password so that was messing it up. Perfect. Thanks!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Import relational Access database into Excel 2007?
    By OldManExcellor in forum Excel General
    Replies: 0
    Last Post: 09-01-2014, 08:58 AM
  2. Import Excel to Relational Access Database
    By jschen77 in forum Access Tables & Databases
    Replies: 2
    Last Post: 03-05-2013, 02:10 PM
  3. Import many excel files to many tables in one access database
    By boss1982 in forum Access Tables & Databases
    Replies: 3
    Last Post: 12-26-2012, 04:05 PM
  4. Is it possible to import data to excel from an access database online?
    By Rob* in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-16-2012, 04:22 AM
  5. Import Access Database info to Excel???
    By jwr in forum Excel General
    Replies: 4
    Last Post: 08-16-2005, 01:17 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1