How to open two tables in single connection [one in currentdb, other in my new connection]

pedie

Well-known Member
Joined
Apr 28, 2010
Messages
3,875
Code:
[/FONT]
[FONT=Courier New]Dim conn As ADODB.Connection
conn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\some folder\somefile.accdb;Persist Security Info=False;"[/FONT]
[FONT=Courier New]

Hi, using the above connection string, how can i open two tables and transfer data from on table to another

dim rst1 as dao.recordset
dim rst2 as adodb.recordset
etc....


Thanks in advance.
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
just like that: create two recordsets, then do what you need to do with them.
 
Upvote 0
Depending on exactly what and how you're transferring data it would normally be quicker to use SQL statements rather than looping through a recordset. For example:

Code:
Dim conn As ADODB.Connection
Dim sSQL As String

conn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\some folder\somefile.accdb;Persist Security Info=False;"

sSQL = "INSERT INTO TABLE2 (field1, field2, field3) SELECT Field1, Field2, Field3 FROM TABLE1"

conn.Execute sSQL

DK
 
Upvote 0
Pedie

How about using DoCmd.TransferDatabase?
Code:
Dim CurDb As DAO.Database
Dim strSQL As String
 
    Set CurDb = CurrentDb
    
    DoCmd.TransferDatabase acLink, "Microsoft Access", CurrentProject.Path & "/" & "Database1.accdb", acTable, "Table1", "TempTable", False
    
    strSQL = "INSERT INTO Table2 SELECT * FROM TempTable"

    CurDb.Execute strSQL

    DoCmd.DeleteObject acTable, "TempTable"
Probably not very efficient and/or liable to cause problems but it worked for me to transfer data from a table in a different database.
 
Upvote 0
Depending on exactly what and how you're transferring data it would normally be quicker to use SQL statements rather than looping through a recordset. For example:

Rich (BB code):
Rich (BB code):
Dim conn As ADODB.Connection
Dim sSQL As String
 
conn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\some folder\somefile.accdb;Persist Security Info=False;"
 
sSQL = "INSERT INTO TABLE2 (field1, field2, field3) SELECT Field1, Field2, Field3 FROM TABLE1"
 
conn.Execute sSQL


DK


Hi everyone.....
Thanks for helping me out here again...
I tried this way and it errors in connection line
"Runtime error 91" Object variable or with block variable not set"
Please advice. My database in not password protected.

Sub TEST1()
Dim conn As ADODB.Connection
Dim sSQL As String
conn.Open "Provider =Microsoft.ACE.OLEDB.12.0; Data Source =C:\myFolder\myAccess2007file.accdb; Persist Security Info =False;"
sSQL = "INSERT INTO BUDGET (Name, Email, ContactNo) SELECT Name, Email, ContactNo FROM BUDGET"
conn.Execute sSQL
End Sub

 
Upvote 0
I want to insert into this database table from different database table is that possible [that was my actual question]:)
I have temp table called "BUDGETxyz" in this database and BUDGET in different db. It says table "BUDGETxyz" NOT found....

Thanks again.
Code:
[/FONT]
[FONT=Courier New]Sub zcbvxcvb()
Dim conn As New ADODB.Connection
Dim sSQL As String
Dim strcon As String[/FONT]
[FONT=Courier New]Set conn = New ADODB.Connection
strcon = "Provider =Microsoft.ACE.OLEDB.12.0; Data Source =E:\Access\Database2.accdb; Persist Security Info =False;"
conn.Open strcon
sSQL = "INSERT INTO BUDGETxyz (MNG, BUDGET, QTR) SELECT MNG, BUDGET, QTR FROM BUDGET"
conn.Execute sSQL
End Sub
 
Upvote 0
A connection is to one database, so your "connection" only knows about the tables in the database it is connected to.

You could use the "IN" clause in a select query to push data to a remote database, as I have shown you how to do before:
http://www.mrexcel.com/forum/showthread.php?t=571587

(or you can just link the table - which is what most people do)

ξ
 
Upvote 0
Pedie

Why don't you just link the table?

That worked fine for me using the code I posted.
 
Upvote 0

Forum statistics

Threads
1,224,524
Messages
6,179,310
Members
452,906
Latest member
phanmemchatdakenhupviral

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top