我正在尝试从使用MySQL和Visual Basic 6的数据库中删除所选记录,但它不起作用,即使没有错误我也会进入它。这是我的连接代码以及按钮单击
Public rs As New ADODB.Recordset
Public cn As New ADODB.Connection
Public sql As String
Dim dbpath As String
Sub main()
Set rs = New ADODB.Recordset
Set cn = New ADODB.Connection
With rs
.CursorLocation = adUseClient
.CursorType = adOpenDynamic
.LockType = adLockOptimistic
End With
dbpath = "DSN=RetailPOS;"
On Error GoTo errhandler
With cn
.Open dbpath
'.CursorLocation = adUseClient
MDIMAIN.Show
End With
'cn.Open dbpath
Exit Sub
errhandler:
MsgBox "Error Number: " & Err.Number & vbCrLf & "Description: " & Err.Description, _
vbCritical
End Sub
按钮点击代码
Private Sub RmvBtn_Click()
If rs.State = 1 Then rs.Close
sql = "DELETE FROM tbluserinfo WHERE UserGroup = '" & Txt(3).Text & "'"
cn.Execute (sql)
MsgBox "The selected user was successfully deleted!", vbInformation + vbOKOnly, ShowProductLabel
End Sub
答案 0 :(得分:0)
由于UserGroup是数字,您应该使用
sql = "DELETE FROM tbluserinfo WHERE UserGroup = " & Txt(3).Text
在调用查询之前,您还需要验证Txt(3).Text。如果它为空,那么您将最终得到以下查询,这将引发错误。
DELETE FROM tbluserinfo WHERE UserGroup =
您还应该检查以确保Txt(3).Text是数字。在IsNumeric()中添加Trim()将捕获一个空白字符串,因为它不会是数字。
If Not IsNumeric(Trim(Txt(3).Text)) Then
'Tell the user and don't run the query
End If