各位大侠,VB使用ADODB操作ACCESS进行删除、增加、修改怎么做啊,麻烦各位大侠给个代码???
这是我写的代码都是有问题,请大家帮我查下,谢谢
删除代码
Private Sub cmdDel_Click()
Dim sq As String
On Error GoTo aaaa
Dim i As Long, j As Long
j = 0
For i = 1 To List1.ListItems.Count
If List1.ListItems(i).Selected = True Then j = j + 1
Next
If j = 0 Then
MsgBox "没有选中任何客户。", vbInformation
Exit Sub
End If
If MsgBox("注意:此操作会同时删除客户记录。" & vbCrLf & vbCrLf & "确定删除选中的 " & j & " 个客户吗?", vbInformation + vbOKCancel + vbDefaultButton2) = vbCancel Then Exit Sub
For i = List1.ListItems.Count To 1 Step -1
If List1.ListItems(i).Selected = True Then
Call OpenConn
sq = "Delete From 客户管理 Where 客户编号='" & List1.ListItems(i).SubItems(2) & "'"
conn.Execute (sq)
List1.ListItems.Remove i
conn.Close
Set conn = Nothing
End If
Next
Exit Sub
aaaa:
MsgBox Err.Description, vbCritical
End Sub
增加与修改代码
If Me.Tag = "" Then '添加模式
Call OpenConn
sql = "select * from 客户管理 where 客户编号='" & txtclientCode.Text & "'"
rs.Open sql, conn, adOpenKeyset, adLockOptimistic
If rs.RecordCount > 0 Then
addclientoperator.Text = userID
MsgBox "客户编号重复,不能添加数据", vbExclamation, "查询"
rs.Close
conn.Close
Set rs = Nothing
Set conn = Nothing
Exit Sub
Else
'rs.Close
Set rs = Nothing
sql = "select * from 客户管理"
rs.Open sql, conn, adOpenKeyset, adLockOptimistic
rs.AddNew
rs.Fields("客户编号") = txtclientCode.Text
rs.Fields("客户名") = addclientName.Text
rs.Fields("联系电话") = addclientphone.Text
rs.Fields("邮箱") = addclientEmail.Text
rs.Fields("国家") = addclientcountry.Text
rs.Fields("联系地址") = addclientaddress.Text
rs.Fields("操作员") = addclientoperator.Text
rs.Update
rs.MoveNext
rs.Close
conn.Close
Set rs = Nothing
Set conn = Nothing
txtclientCode.Text = ""
addclientName.Text = ""
addclientphone.Text = ""
addclientphone.Text = ""
addclientEmail.Text = ""
addclientcountry.Text = ""
addclientaddress.Text = ""
addclientoperator.Text = ""
clientFrom.List1.ListItems.Clear '先清空表格里的数据
Call OpenConn
client2
MsgBox "新项目添加完成!", vbInformation, "增加项目"
End If
Else
Call OpenConn
conn.Execute "update 客户管理 SET " & _
"客户编号='" & txtclientCode.Text & "',客户名=" & _
addclientName.Text & ",联系电话=" & _
addclientphone.Text & ",邮箱=" & _
addclientEmail.Text & ",国家=" & _
addclientcountry.Text & ",联系地址=" & _
"'" & addclientaddress.Text & "',操作员=" & _
"'" & addclientoperator.Text & "' Where 客户编号='" & txtclientCode.Text & "'"
conn.Close
Set conn = Nothing
txtclientCode.Text = ""
addclientName.Text = ""
addclientphone.Text = ""
addclientphone.Text = ""
addclientEmail.Text = ""
addclientcountry.Text = ""
addclientaddress.Text = ""
addclientoperator.Text = ""
'clientFrom.List1.ListItems.Clear '先清空表格里的数据
Call OpenConn
client2
MsgBox "新项目修改完成!", vbInformation, "增加项目"
Unload Me
End If
End Sub