|
以下代码是一个从Excel里添加记录到access的实例:
Dim i As Integer, j As Integer, k As Integer, sht As Worksheet 'I,j,k为整数变量;sht 为excel工作表对象变量,指向某一工作表
Dim cn As New ADODB.Connection '定义数据链接对象 ,保存连接数据库信息;请先添加ADO引用
Dim rs As New ADODB.Recordset '定义记录集对象,保存数据表
Dim strCn As String, strSQL As String '字符串变量
Dim mdbFile As String
On Error GoTo add_err
mdbFile = "d:\数据库\原始数据\当月业绩达成查询数据库.mdb"
strCn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & mdbFile '定义数据库链接字符串
cn.Open strCn '与数据库建立连接,如果成功,返回连接对象cn
Set rs = New ADODB.Recordset
rs.Open "出货明细表", cn, adOpenKeyset, adLockOptimistic
If Cells(2, 12) > 0 Then
MsgBox "您有送达方代码没有与售达方代码建立联系,这将导致系统无法识别业绩归属人,所以请先在【送达方关系】表中添加该送达方对应的售达方!", vbExclamation
Else
j = 1
For I = 2 To 20000
If j > Cells(1, 11) Then
Exit For
Else
If Cells(I, 8) = "Y" Then
rs.AddNew
rs(1) = Cells(1, 13)
rs(2) = Cells(I, 2)
rs(3) = Cells(I, 3)
rs(4) = Cells(I, 4)
rs(5) = Cells(I, 5)
rs(6) = "应出未出"
rs.Update
j = j + 1
Else
End If
End If
Next I
MsgBox "数据记录添加成功!", vbInformation
End If
add_exit:
Exit Sub
add_err:
MsgBox Err() & vbCrLf & Error()
Resume add_exit
但是假如要在Excel里修改Access里面的数据,应该怎么修改这段代码呢? |
|