怎么把ACCESS不同的数据库合
用VB6的ADO控件,使用SQL语句可实现两个表格式一样数据不同的数据合并,且能把源数据表和目标数据表主关键词相同的数据更新,并可在目标数据表中添加新的数据。提问者应依据所涉及数据库情况对代码改动。 具体代码如下:
第一部分,定义变量
Option Explicit
Dim zh_bm(11, 1500)
Dim zh_bm1(11, 1500)
Dim zsl As Integer
Dim zsl1 As Integer
Dim i As Integer
Dim j As Integer
Dim ii As Integer
Dim jj As Integer
第二部分,将源数据表数据传...全部
用VB6的ADO控件,使用SQL语句可实现两个表格式一样数据不同的数据合并,且能把源数据表和目标数据表主关键词相同的数据更新,并可在目标数据表中添加新的数据。提问者应依据所涉及数据库情况对代码改动。
具体代码如下:
第一部分,定义变量
Option Explicit
Dim zh_bm(11, 1500)
Dim zh_bm1(11, 1500)
Dim zsl As Integer
Dim zsl1 As Integer
Dim i As Integer
Dim j As Integer
Dim ii As Integer
Dim jj As Integer
第二部分,将源数据表数据传递给变量
Adodc1。
ConnectionString = "Provider=Microsoft。Jet。OLEDB。4。0;Data Source=D:\DATA_REF\ b;Persist Security Info=False"
Adodc1。
RecordSource = "SELECT * FROM mdlk_sj where 销售='N' order by 码单号"
Adodc1。Refresh
DataGrid1。
Refresh
zsl = Adodc1。Recordset。RecordCount
Adodc1。Recordset。MoveFirst
For i = 0 To zsl - 1
For j = 0 To 11
zh_bm(j, i) = Adodc1。
Recordset(j)
Next j
Adodc1。Recordset。MoveNext
Next i
Adodc1。Recordset。MoveFirst
第三部分,添加与更新目标数据表
Adodc2。
ConnectionString = "Provider=Microsoft。Jet。OLEDB。4。0;Data Source=D:\DATA_REF\ b;Persist Security Info=False"
Adodc2。
RecordSource = "SELECT * FROM MDLK_SJ1 where 码单号='" & "" & "'" ' order by 码单号"
Adodc2。Refresh
DataGrid2。
Refresh
For i = 0 To zsl - 1
Adodc2。ConnectionString = "Provider=Microsoft。Jet。OLEDB。4。0;Data Source=D:\DATA_REF\ b;Persist Security Info=False"
Adodc2。
RecordSource = "SELECT * FROM MDLK_SJ1 where 码单号='" & zh_bm(4, i) & "'" ' order by 码单号"
Adodc2。
Refresh
DataGrid2。Refresh
zsl1 = Adodc2。Recordset。RecordCount
Text2 = zsl1
If zsl1 = 0 Then
Label2。
BackColor = vbGreen
Adodc2。Recordset。AddNew
For j = 0 To 11
Adodc2。Recordset(j) = zh_bm(j, i)
Next j
Adodc2。
Recordset。Update
Adodc2。ConnectionString = "Provider=Microsoft。Jet。OLEDB。4。0;Data Source=D:\DATA_REF\ b;Persist Security Info=False"
Adodc2。
RecordSource = "SELECT * FROM MDLK_SJ1 where 码单号='" & zh_bm(4, i) & "'" ' order by 码单号"
Adodc2。
Refresh
DataGrid2。Refresh
ElseIf zsl1 = 1 Then
Label2。BackColor = vbRed
For j = 0 To 11
Adodc2。
Recordset(j) = zh_bm(j, i)
Next j
Adodc2。Recordset。Update
Adodc2。ConnectionString = "Provider=Microsoft。
Jet。OLEDB。4。0;Data Source=D:\DATA_REF\ b;Persist Security Info=False"
Adodc2。RecordSource = "SELECT * FROM MDLK_SJ1 where 码单号='" & zh_bm(4, i) & "'" ' order by 码单号"
Adodc2。
Refresh
DataGrid2。Refresh
End If
Next i
'以下是目标数据表显示刷新
Adodc2。ConnectionString = "Provider=Microsoft。
Jet。OLEDB。4。0;Data Source=D:\DATA_REF\ b;Persist Security Info=False"
Adodc2。RecordSource = "SELECT * FROM MDLK_SJ1 order by 码单号"
Adodc2。
Refresh
DataGrid2。Refresh
上述代码在VB6调试通过,供参考。
。收起