Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2480
  • Last Modified:

Import multiple tables using: DoCmd.TransferDatabase acImport

dexweaver wrote the following code, I am using it and it works like a charm:

Okay, make a table (tblTableNames) with one field (TableName), put the names of the tables in one per line, then use some code like this:

'*******************************************
Dim r as Recordset
Dim CurrentTableName as String

Set r = CurrentDb.OpenRecordset("tblTableNames")

r.MoveFirst
Do Until r.EOF
  CurrentTableName = r!TableName
  If IsTable(CurrentTableName Then
    DoCmd.DeleteObject acTable, CurrentTableName
  End If
Loop
'*******************************************
What I would like now is to import the same tables that have been deleted using DoCmd.TransferDatabase acImport without having to type all the tables. In other words, using the same type of script.
Thanks,
Gabe

0
gablais
Asked:
gablais
  • 4
  • 3
1 Solution
 
jadedataMS Access Systems CreatorCommented:
Hey gablais!

  using your same tblTableNames, add a field for the Make-Table query name that is built to re-build the table.
  Be aware that the Query is going to have certain requirements that you will have to deal with that I have no idea about

Dim r as Recordset
Dim CurrentQueryName as String

Set r = CurrentDb.OpenRecordset("tblTableNames")

r.MoveFirst
Do Until r.EOF
  CurrentQueryName = r!QueryName
  If Not IsTable(r!TableName) Then
    docmd.runsql CurrentQueryName
  End If
Loop



regards
Jack
0
 
gablaisAuthor Commented:
Jack, I am not sure we are on the same page. The task I am trying to accomplish is to import tables from another database. The name of the tables will be the same on both databases.
If I had only one table to import it would be something like this:
DoCmd.TransferDatabase acImport, "Microsoft Access", "S:\HES\Safety & IH Databases\Checklist.mdb", acTable, "Air_Quality", "Air_Quality"

But I have nearly 80 of them.

Please let me know If I am being clear enough.
Gabe
0
 
jadedataMS Access Systems CreatorCommented:
In that case make the new field the path to the database you are importing from (see r!DatabaseName below)

Dim r as Recordset
Set r = CurrentDb.OpenRecordset("tblTableNames")
r.MoveFirst
Do Until r.EOF
  If Not IsTable(r!TableName) Then
    DoCmd.TransferDatabase acImport, "Microsoft Access", r!DatabaseName, acTable, r!TableName, r!TableName
  End If
Loop
0
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
gablaisAuthor Commented:
Ok Jack, We're getting there.
The loop is getting carried away and keeps reimporting the first table until I kill the application.
Also, I don't think I need the following lines in that script:
If Not IsTable(r!TableName) Then
End If
If I do use them, the table does not import more than once, but the loop does not stop running.
Gabe
0
 
jadedataMS Access Systems CreatorCommented:
Dim r as Recordset
Set r = CurrentDb.OpenRecordset("tblTableNames")
r.MoveFirst
Do Until r.EOF
  If Not IsTable(r!TableName) Then
    DoCmd.TransferDatabase acImport, "Microsoft Access", r!DatabaseName, acTable, r!TableName, r!TableName
  End If
  r.movenext
Loop

Now remeber I started this copy with your first code block.
Added the r.movenext to get it to move to the next record.  Can't get to the EOF unless you traverse records!
0
 
gablaisAuthor Commented:
You got it pal. Thank you and take care.
Gabe
0
 
jadedataMS Access Systems CreatorCommented:
and thank you for the question!
0

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now