永发信息网

VB使用ADODB操作ACCESS进行删除、增加、修改

答案:1  悬赏:0  手机版
解决时间 2021-05-18 10:53
  • 提问者网友:骨子里的高雅
  • 2021-05-17 19:27

各位大侠,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

最佳答案
  • 五星知识达人网友:末日狂欢
  • 2021-05-17 19:47

先在工程中引用microsoft activex data objects 2.7 library


然后创建对象


为了方便开发扩展建议数据库对象在通用里定义。


ADODB rs = New ADODB.Recordset
ADODB conn = New ADODB.Connection


在事件或方法里执行一下语句。


conn.open "PROVIDER=MICROSOFT.JET.OLEDB.4.0;DATA SOURCE=" & Server.MapPath("aaa.mdb")


rs.open "SELECt * FROM TABLE1 WHERe A=A" ,conn 这是查询语句


rs.open "INSERT INTO TEBLE1(name1) VALUES("+ s +") " ,conn 这是添加语句


以此类推吧都是TSQL语句了



不会的可以追问



我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯