在外部数据库之间传输数据

时间:2017-05-22 21:50:11

标签: vba ms-access

我有两个Access数据库[source(TBL_PAINEL)]具有相同的主表结构,但我需要将数据传输到[target(TBL_PAINEL2)]数据库,但我不能帮助我。请遵守代码:

Dim strSQL As String
Dim db As DAO.Database
Dim db1 As DAO.Database
Dim wrk As DAO.Workspace
Dim rs As DAO.Recordset
Dim dbname As String

Set wrk = DBEngine(0)
Set db = CurrentDb()
Set db1 = wrk.OpenDatabase("\\fswcorp\cat\dsie\soas\gocpf\UGO\5. QA\2. DB\History_CD.accdb")

dbname = "\\fswcorp\cat\dsie\soas\gocpf\UGO\5. QA\2. DB\History_CD.accdb"

strSQL = "INSERT INTO TBL_PAINEL2 (CHAVEPAINEL, NOMECLIENTE, AGENCIA, CONTA, CONTRATO, MODALIDADE, ID_SEGMENTO, ID_PRODUTO) " _
& "SELECT TBL_PAINEL .CHAVEPAINEL, TBL_PAINEL.NOMECLIENTE, TBL_PAINEL.AGENCIA, TBL_PAINEL.CONTA, TBL_PAINEL.CONTRATO, TBL_PAINEL.MODALIDADE, TBL_PAINEL.ID_SEGMENTO, TBL_PAINEL.ID_PRODUTO " _
& "FROM TBL_PAINEL " _
& "IN '" & dbname & "';"

db1.Execute (strSQL)

wrk.Close
Set db = Nothing
Set db1 = Nothing
Set wrk = Nothing

谢谢!

1 个答案:

答案 0 :(得分:1)

这里真的不需要vba,只是一个简单的查询。如果必须,保存查询并从vba运行。

如果从3&rdd数据库运行,那么既不是源也不是目标:

INSERT INTO target_table IN '\\serverpath\target.accdb'
SELECT * FROM source_table IN '\\serverpath\source.accdb'

如果从源数据库内部运行,只需省略路径,反之亦然,如果从目标数据库内部运行。

INSERT INTO target_table IN '\\serverpath\target.accdb'
SELECT * FROM source_table