-
Mar 8th, 2018, 04:21 PM
#1
Thread Starter
New Member
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 04:47 PM.
-
Mar 8th, 2018, 07:29 PM
#2
Re: VBA opendatabase issue
What does fname contain after this line?
Code:
fname = Dir("C:\MAPS\Claim Temp\*.accdb")
-
Mar 8th, 2018, 07:52 PM
#3
Thread Starter
New Member
Re: VBA opendatabase issue
Originally Posted by jdc2000
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 08:00 PM.
-
Mar 8th, 2018, 07:59 PM
#4
Re: VBA opendatabase issue
-
Mar 8th, 2018, 08:16 PM
#5
Re: VBA opendatabase issue
Originally Posted by brandoomg
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)
-
Mar 9th, 2018, 01:55 AM
#6
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
-
Mar 9th, 2018, 11:55 AM
#7
Thread Starter
New Member
Re: VBA opendatabase issue
Originally Posted by OptionBase1
What happens if you do this instead:
Code:
fname = Dir("C:\MAPS\Claim Temp\*.accdb")
Set dbs = OpenDatabase("C:\MAPS\Claim Temp\" & fname)
Originally Posted by westconn1
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|