想通过ado的excute方法insert、update、delete操作数据库,程序(事件)能正常运行,但无法成功(增删改)access数据库的内容,请大手指点迷津
'''定义全局变量
Public conn As New ADODB.Connection
Public conn_cmd As New ADODB.Command
Public str_sql As String
'''click事件
Private Sub Command1_Click(Index As Integer)
Dim i As Integer
Dim str As String
Dim ntr As String
select case index
case 0
If MsgBox("数据删除不可恢复,请确认", vbYesNo) = vbYes Then '''获取delete的where条件
For i = 0 To Text1.UBound
If Text1(i).Text <> "" Then
If i = 5 Or i = 6 Then
str = Label1(i).Caption & " = " & Text1(i).Text
ElseIf i = 0 Or i = 1 Or i = 2 Or i = 3 Or i = 4 Or i = 7 Then
str = Label1(i).Caption & " like '%" & Text1(i).Text & "%'"
Else
End If
ntr = ntr & " and " & str
Else
End If
Next
ntr = Mid(ntr, 5)
Debug.Print str_sql
'''这是得到SQL语句:delete from User_manange where User_name like '%代号9%';
conn.ConnectionString = "provider=microsoft.jet.oledb.4.0;data source=" & Database_path & ";persist security info=false"
conn.CursorLocation = adUseClient
conn.Mode = adModeReadWrite
conn.Open
conn_cmd.ActiveConnection = conn
conn_cmd.CommandType = adCmdText
conn_cmd.CommandText = str_sql
conn_cmd.Execute
conn.Close
Set conn_cmd = Nothing
Set conn = Nothing
MsgBox "成功删除数据", vbInformation, "温馨提示"
'''重新加载datagrid:
str_sql = "select * from User_manage;"
With Form7.Adodc1
.ConnectionString = "provider=microsoft.jet.oledb.4.0;" & "data source=" & Database_path & ";Persist Security Info=False"
.RecordSource = str_sql
.CommandType = adCmdText
.Visible = False
.Refresh
End With
Set DataGrid1.DataSource = Adodc1
DataGrid1.ReBind
Else
Exit Sub
End If
case else
end select
end sub