Office中国论坛/Access中国论坛

标题: 两个ACCESS库间表的复制 [打印本页]

作者: yxcsy    时间: 2012-5-19 09:23
标题: 两个ACCESS库间表的复制

用VB操作两个ACCESS库的表,把库1的表1数据复制到库2的表2,他们的数据库结构一样,请大家帮忙,谢谢
[attach]49221[/attach]


以下是代码贴出来,请帮助
Dim cn1 As New ADODB.Connection    '连接源数据库
Dim rs1 As New ADODB.Recordset        '源数据集
Dim cn2 As New ADODB.Connection   '连接目标数据库
Dim rs2 As New ADODB.Recordset      '目标记录集
Dim rstemp As ADODB.Recordset         '临时记录集
Dim strCNN As String, strSQL As String  '定义字符串
Dim record1, record2, record3                 '定义两个记录
Private Sub Command1_Click()
'cn2.Open
cn1.Execute "delete * from 表A"
cn2.Close
End Sub

Private Sub Command2_Click() '要求复制新.mde内表A到目标.mde 表A,目前都还没完成!
'cn2.Open
'select * into [e:\复制\新.mdb].表A from 表A
'Set rs1 = cn1.OpenRecordses("Select * FROM 表A")
'n1.Execute "select * into [e:\复制\新.mdb].表A from 表A"

'Insert into [e:\复制\目标.mdb].[表A] Select * From 表A in "E:\新.mdb"
'执行select * into X in [b库路径] from X


'cn1.Execute "insert into 表A SELECT * form cn2.表A"

'insert into 表A(姓名) select 姓名 from 表A in 'e:\复制\新.mdb'

End Sub

Private Sub Form_Load()
  'Set cn1 = New ADODB.Connection
  'Set cn2 = New ADODB.Connection
  cn1.ConnectionString = "rovider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\新.mdbersist Security Info=False"
  cn2.ConnectionString = "rovider=Microsoft.Jet.OLEDB.4.0;Data Source=e:\复制\目标.mdb;Persist Security Info=False" 'Jet OLEDBatabase Password=123"
cn1.Open
cn2.Open

End Sub



作者: roych    时间: 2012-5-19 12:26
本帖最后由 roych 于 2012-5-19 12:35 编辑

能引用Access库么?如果可以引用,代码将相当简单:

  1. '窗体模块定义。创建Access组件和定义数据库
  2. Dim acc As New Access.Application                              
  3. Dim db As DAO.Database
  4. Private Sub Command2_Click()
  5. Set db = DAO.OpenDatabase(App.Path & "\目标.mdb") '删除旧表(如果不删除将产生:“表A1”、“表A2”等等)
  6. db.Execute "drop Table 表A"
  7. acc.OpenCurrentDatabase App.Path & "\目标.mdb"        '导入数据
  8. acc.DoCmd.TransferDatabase acImport, "Microsoft Access",App.Path & "\新.mdb", acTable, "表A","表A"
  9. End Sub
复制代码
[attach]49223[/attach]




欢迎光临 Office中国论坛/Access中国论坛 (http://www.office-cn.net/) Powered by Discuz! X3.3