dcsimg
Results 1 to 7 of 7
  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2018
    Posts
    3

    VBA opendatabase issue

    Hi there, I'm relatively new to VBA so sorry if this is an easy fix, but some searching hasn't turned out helpful.

    I'm trying to basically just append a table from one access db to a table in another, but doing this without directly referencing the name of the db. I am currently just trying to use a wildcard to grab it out of a directory, but it doesn't appear to work with OpenDatabase() as I have it, and apprently using * doesn't work within OpenDatabase() either. All help is really appreciated, thanks!

    Code follows:

    Code:
    Private Sub cmdImportClaim_Click()
       Dim fname As String
       Dim dbs As Database
       
        fname = Dir("C:\MAPS\Claim Temp\*.accdb")
        Set dbs = OpenDatabase(fname)
         
        
        dbs.Execute " INSERT INTO Claim In 'C:\MAPS\MAPS - Production_2018.accdb' " _
            & "SELECT * " _
            & "FROM ReconFormat;"
             
        dbs.Close
    
    End Sub
    I'm receiving an error saying the filename (which is correct) cannot be found.
    Last edited by brandoomg; Mar 8th, 2018 at 03:47 PM.

  2. #2
    Frenzied Member
    Join Date
    Oct 2001
    Location
    Idaho Falls, Idaho USA
    Posts
    1,105

    Re: VBA opendatabase issue

    What does fname contain after this line?

    Code:
    fname = Dir("C:\MAPS\Claim Temp\*.accdb")

  3. #3

    Thread Starter
    New Member
    Join Date
    Mar 2018
    Posts
    3

    Re: VBA opendatabase issue

    Quote Originally Posted by jdc2000 View Post
    What does fname contain after this line?

    Code:
    fname = Dir("C:\MAPS\Claim Temp\*.accdb")
    a string which is the direct path to the file, i.e. C:\Maps\Claim Temp\mydb.accdb

    I should note when I enter it directly, i.e.
    Code:
     Set dbs = OpenDatabase("C:\Maps\Claim Temp\mydb.accdb")
    it works as intended.
    Last edited by brandoomg; Mar 8th, 2018 at 07:00 PM.

  4. #4
    Frenzied Member
    Join Date
    Oct 2001
    Location
    Idaho Falls, Idaho USA
    Posts
    1,105

    Re: VBA opendatabase issue


  5. #5
    Hyperactive Member
    Join Date
    Nov 2017
    Posts
    337

    Re: VBA opendatabase issue

    Quote Originally Posted by brandoomg View Post
    a string which is the direct path to the file, i.e. C:\Maps\Claim Temp\mydb.accdb

    I should note when I enter it directly, i.e.
    Code:
     Set dbs = OpenDatabase("C:\Maps\Claim Temp\mydb.accdb")
    it works as intended.
    What happens if you do this instead:

    Code:
        fname = Dir("C:\MAPS\Claim Temp\*.accdb")
        Set dbs = OpenDatabase("C:\MAPS\Claim Temp\" & fname)

  6. #6
    PowerPoster
    Join Date
    Dec 2004
    Posts
    23,548

    Re: VBA opendatabase issue

    as posted above, but not explicitly expressed, dir only returns the file name, not the path
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  7. #7

    Thread Starter
    New Member
    Join Date
    Mar 2018
    Posts
    3

    Re: VBA opendatabase issue

    Quote Originally Posted by OptionBase1 View Post
    What happens if you do this instead:

    Code:
        fname = Dir("C:\MAPS\Claim Temp\*.accdb")
        Set dbs = OpenDatabase("C:\MAPS\Claim Temp\" & fname)

    Quote Originally Posted by westconn1 View Post
    as posted above, but not explicitly expressed, dir only returns the file name, not the path
    Thank you both! I had wrongfully assumed the path was returned, as the output of the error message included the path.

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width


×
We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.