请教:请帮修改一下vb连接access数据库中删除的代码
我添加了一个DataGrid控件,用来显示数据库,并添加了四个Text1(0~3)控件数组,用来分别显示数据库中的值,为什么我编写的删除代码总是删除不了,请帮忙修改一下,...
我添加了一个DataGrid控件,用来显示数据库,并添加了四个Text1(0~3)控件数组,用来分别显示数据库中的值,为什么我编写的删除代码总是删除不了,请帮忙修改一下,谢谢!
SelectSQL(SQL, msg)函数那段代码我也不明白,能给解释一下吗?
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim msg As String
Dim Index As Integer
Private Sub CmdDelete_Click()
If rs.RecordCount > 0 Then
msg = MsgBox("删除该条记录吗?", vbYesNo)
If msg = vbYes Then
rs.Delete
Call LoadData
For Index = 0 To 3
Text1(Index).Text = ""
Text1(Index).Enabled = False
Next Index
If rs.RecordCount = 0 Then
For Index = 0 To 3
Command1(Index).Enabled = False
Next Index
End If
CmdAdd.Enabled = True: CmdModify.Enabled = False
CmdDelete.Enabled = True
CmdCancel.Enabled = True: CmdSave.Enabled = False
MsgBox ("成功删除数据!")
End If
Else
MsgBox ("没有可删除的数据")
End If
End Sub
Private Sub LoadData()
Set rs = Nothing
SQL = "select * from 材质数据库 order by 材质"
Set rs = SelectSQL(SQL, msg)
Set Me.DataGrid1.DataSource = rs
DataGrid1.Refresh
End Sub
Public Function SelectSQL(ByVal SQL As String, ByRef msg As String) As ADODB.Recordset
Dim Conn As ADODB.Connection
Dim rst As ADODB.Recordset
Dim sTokens() As String
On Error GoTo ErrorHandle
sTokens = Split(SQL)
If InStr("SELECT", UCase((sTokens(0)))) Then
If OpenConn(Conn) Then
Set rst = New ADODB.Recordset
rst.CursorLocation = adUseClient
rst.Open Trim$(SQL), Conn, adOpenDynamic, adLockBatchOptimistic
Set SelectSQL = rst
msg = "查询到" & rst.RecordCount & "条记录!"
End If
Else
msg = "SQL语句有误:" & SQL
End If
Finally_Exit:
Set rst = Nothing
Set Conn = Nothing
Exit Function
ErrorHandle:
msgstring = "查询错误:" & Err.Description
Resume Finally_Exit
End Function
非常感谢楼下几位,但是我尝试做了一下还是不行啊,按下确定键后数据又恢复以前的了,并不能删除。
是不是Call LoadData 所致啊?? 展开
SelectSQL(SQL, msg)函数那段代码我也不明白,能给解释一下吗?
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim msg As String
Dim Index As Integer
Private Sub CmdDelete_Click()
If rs.RecordCount > 0 Then
msg = MsgBox("删除该条记录吗?", vbYesNo)
If msg = vbYes Then
rs.Delete
Call LoadData
For Index = 0 To 3
Text1(Index).Text = ""
Text1(Index).Enabled = False
Next Index
If rs.RecordCount = 0 Then
For Index = 0 To 3
Command1(Index).Enabled = False
Next Index
End If
CmdAdd.Enabled = True: CmdModify.Enabled = False
CmdDelete.Enabled = True
CmdCancel.Enabled = True: CmdSave.Enabled = False
MsgBox ("成功删除数据!")
End If
Else
MsgBox ("没有可删除的数据")
End If
End Sub
Private Sub LoadData()
Set rs = Nothing
SQL = "select * from 材质数据库 order by 材质"
Set rs = SelectSQL(SQL, msg)
Set Me.DataGrid1.DataSource = rs
DataGrid1.Refresh
End Sub
Public Function SelectSQL(ByVal SQL As String, ByRef msg As String) As ADODB.Recordset
Dim Conn As ADODB.Connection
Dim rst As ADODB.Recordset
Dim sTokens() As String
On Error GoTo ErrorHandle
sTokens = Split(SQL)
If InStr("SELECT", UCase((sTokens(0)))) Then
If OpenConn(Conn) Then
Set rst = New ADODB.Recordset
rst.CursorLocation = adUseClient
rst.Open Trim$(SQL), Conn, adOpenDynamic, adLockBatchOptimistic
Set SelectSQL = rst
msg = "查询到" & rst.RecordCount & "条记录!"
End If
Else
msg = "SQL语句有误:" & SQL
End If
Finally_Exit:
Set rst = Nothing
Set Conn = Nothing
Exit Function
ErrorHandle:
msgstring = "查询错误:" & Err.Description
Resume Finally_Exit
End Function
非常感谢楼下几位,但是我尝试做了一下还是不行啊,按下确定键后数据又恢复以前的了,并不能删除。
是不是Call LoadData 所致啊?? 展开
展开全部
你 SelectSQL函数只能查询记录,而不能删除记录。。
请参考下面的函数。携迹
Public Function ExecuteSQL(ByVal SQL As String, MsgString As String) As ADODB.Recordset
Dim rst As ADODB.Recordset
Dim sTokens() As String
Dim sF As String
On Error GoTo ExecuteSQL_Error
sTokens = Split(SQL)
sF = LCase(sTokens(0))
Select Case sF
Case "insert", "delete", "update"
With Cnc
.ActiveConnection = Cn
.CommandType = adCmdText
.CommandText = SQL
.Execute
End With
If sF = "insert" Then
MsgString = "记录插入成功!"
ElseIf sF = "delete" Then
MsgString = "记录删除成功!"
ElseIf sF = "update" Then
MsgString = "记录更新成功!"
End If
Case "厅隐信select"
Set rst = New ADODB.Recordset
rst.Open Trim(SQL), Cn, adOpenStatic, adLockBatchOptimistic
Set ExecuteSQL = rst
MsgString = "查询到" & rst.RecordCount & "扮轮 条记录 "
End Select
ExecuteSQL_Exit:
Set rst = Nothing
Exit Function
ExecuteSQL_Error:
MsgString = "查询错误: " & vbCrLf & Err.Number & vbCrLf & Err.Description
Message MsgString
Resume ExecuteSQL_Exit
End Function
请参考下面的函数。携迹
Public Function ExecuteSQL(ByVal SQL As String, MsgString As String) As ADODB.Recordset
Dim rst As ADODB.Recordset
Dim sTokens() As String
Dim sF As String
On Error GoTo ExecuteSQL_Error
sTokens = Split(SQL)
sF = LCase(sTokens(0))
Select Case sF
Case "insert", "delete", "update"
With Cnc
.ActiveConnection = Cn
.CommandType = adCmdText
.CommandText = SQL
.Execute
End With
If sF = "insert" Then
MsgString = "记录插入成功!"
ElseIf sF = "delete" Then
MsgString = "记录删除成功!"
ElseIf sF = "update" Then
MsgString = "记录更新成功!"
End If
Case "厅隐信select"
Set rst = New ADODB.Recordset
rst.Open Trim(SQL), Cn, adOpenStatic, adLockBatchOptimistic
Set ExecuteSQL = rst
MsgString = "查询到" & rst.RecordCount & "扮轮 条记录 "
End Select
ExecuteSQL_Exit:
Set rst = Nothing
Exit Function
ExecuteSQL_Error:
MsgString = "查询错误: " & vbCrLf & Err.Number & vbCrLf & Err.Description
Message MsgString
Resume ExecuteSQL_Exit
End Function
展开全部
rs.Delete
rs.update
删除完更新
多加一句rs.Requery
rs.update
删除完更新
多加一句rs.Requery
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
很简渗饥拍单~
dim rs as integer
rs=msgbox("确定要删丛羡除数据吗?",1,"确定")
if rs=1 then
data1.recordest.delete
data1.refresh
end if
end sub
我也是新手。。肢帆这个是Access连接数据库的删除代码~~~
dim rs as integer
rs=msgbox("确定要删丛羡除数据吗?",1,"确定")
if rs=1 then
data1.recordest.delete
data1.refresh
end if
end sub
我也是新手。。肢帆这个是Access连接数据库的删除代码~~~
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询