|
有两ACCESS数据库,库结构完全一致(注意关键字不能设为重复,比如用数字序列作关键字不行),分别为:D:/DB1.MDB及D:/DB2,打开DB1插入模块,编写如下代码:
Public Sub ImportData()
Dim strPath As String
strPath = "D:\db2.MDB"
Dim strSQL As String
Dim Cnn As New ADODB.Connection
Dim Rst As New ADODB.Recordset
Set Cnn = CurrentProject.Connection
strSQL = "SELECT * FROM MSysObjects WHERE Flags=0 AND Type=1"
Rst.Open strSQL, Cnn, adOpenKeyset, adLockReadOnly
If Not Rst.EOF Then
Rst.MoveFirst
Do While Not Rst.EOF
strSQL = "insert into " & Rst!Name & " select * from " & Rst!Name & " in '" & strPath & "'"
Cnn.Execute strSQL
Debug.Print strSQL
Debug.Print Rst!Name
Rst.MoveNext
Loop
End If
MsgBox "OK"
End Sub
运行代码后,db2.mdb合并到DB1.mdb中了,以上代码,小弟想用EXCEL的VBA代码改码,运行VBA代码后实现两数据库的自动合并,请教各路高手,该如何实现,谢谢!
[ 本帖最后由 wwwbjwww 于 2010-1-31 13:42 编辑 ] |
|