+ Reply to Thread
Results 1 to 8 of 8

VBA to Access Database connection

  1. #1
    Registered User
    Join Date
    12-12-2019
    Location
    Canada
    MS-Off Ver
    Office 365
    Posts
    14

    VBA to Access Database connection

    Hello everyone,
    I am trying to teach myself how to connect to an access database via excel VBA. Can someone explain to me why I am getting an error in this code.
    The first sub works no problem.
    The second sub will work if I uncomment out the first query and comment out the second. But I want to put the database path in a variable because not all computers in our office are mapped the same, so the "C" drive maybe a different letter on another computer. (Take the 'C" drive as a representation of our server drive).

    When I create the variable 'DBPath' and run it, I get an run-time error 1004 when it hits the last line of the with statement (.Refresh BackgroundQuery:=False) and I do not know why.

    Please Login or Register  to view this content.
    I tried to upload a copy of the database (its only two items so I know its not overly large), but the forum would not allow it.

    Thanks in advance
    Ken

  2. #2
    Forum Expert
    Join Date
    07-23-2018
    Location
    UK
    MS-Off Ver
    O365 32bit (Windows)
    Posts
    1,980

    Re: VBA to Access Database connection

    Try it like this. The way you had it was passing the literal text "DBPath" rather than the variable.

    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    12-12-2019
    Location
    Canada
    MS-Off Ver
    Office 365
    Posts
    14

    Re: VBA to Access Database connection

    Thanks ByteMarks, I tried your suggestion but still get the run time error at the same location.

  4. #4
    Forum Expert
    Join Date
    07-23-2018
    Location
    UK
    MS-Off Ver
    O365 32bit (Windows)
    Posts
    1,980

    Re: VBA to Access Database connection

    Maybe

    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    12-12-2019
    Location
    Canada
    MS-Off Ver
    Office 365
    Posts
    14

    Re: VBA to Access Database connection

    Thanks ByteMarks, that worked perfectly.
    If you can, can you explain why the variable has to be done that way. Like I said, I am trying to learn this so understanding the process will help alot.

  6. #6
    Forum Expert
    Join Date
    07-23-2018
    Location
    UK
    MS-Off Ver
    O365 32bit (Windows)
    Posts
    1,980

    Re: VBA to Access Database connection

    Great.
    You need to separate the variable DBPath from the rest of the string so that it's value (the path) is used and not the text "DBPath"

    In the source the path has to be encased by ", so you need to add these before and after the variable, hence the """

    It's might be easier to demonstrate with a simpler example:
    Only 1 and 4 are correct.
    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    12-12-2019
    Location
    Canada
    MS-Off Ver
    Office 365
    Posts
    14

    Re: VBA to Access Database connection

    Thanks for that ByteMarks, much appreciated.

  8. #8
    Forum Expert
    Join Date
    07-23-2018
    Location
    UK
    MS-Off Ver
    O365 32bit (Windows)
    Posts
    1,980

    Re: VBA to Access Database connection

    You're welcome.

+ 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. Excel Connection with Access database
    By ymcontractor in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-10-2017, 12:54 PM
  2. [SOLVED] Access database connection with excel 2003
    By Megatronixs in forum Excel Programming / VBA / Macros
    Replies: 20
    Last Post: 11-20-2014, 07:28 AM
  3. Replies: 5
    Last Post: 07-16-2013, 12:02 PM
  4. Replies: 2
    Last Post: 07-05-2013, 09:37 AM
  5. Cannot Use ADODB.Connection to connect to an access database
    By Cardinalbags in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-18-2012, 07:09 PM
  6. [SOLVED] ADO connection with workgroup Access database
    By ca1358 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-21-2006, 01:15 AM
  7. [SOLVED] How do make it a connection of Access Database from Excel?
    By Dennis in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-28-2005, 10:06 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