从文本框更新数据库记录时出错

Error updating database record from a textbox

美好的一天!我正在尝试使用文本框更新数据库中的信息。我通过这个得到记录的ID:

Dim dataID As String
dataID = lblID.Caption
rx.Open "SELECT * FROM tblclient WHERE ID = '& Trim$(dataID) &'", con, 3, 3

然后我检查记录是否可用,但不幸的是,即使有记录,也没有找到匹配的记录。这是我的第一个问题。

If rx.EOF Then
    MsgBox "No matching data"
Else
    MsgBox "There is a match"
End If

我的第二个问题是,我无法更新数据库中的信息,我正在使用这行代码,我知道我这样做不对,但我似乎找不到任何解决方案,我在 VB6 中真的非常新。

With rx
 .Fields("Firstname") = txtFirstName.Text
.Fields("Lastname") = txtLastName.Text
 .Fields("District") = txtDistrict.Text
 .Fields("co_Maker") = txtCoMaker.Text
 .Fields("Address") = txtAddress.Text
 .Fields("Interest") = txtInterest.Text
 .Fields("ContactNo") = txtContactNo.Text
 .Update
End With

请有人帮助我。 我正在使用 VB6、MySQL 数据库、ADO 和数据网格。

这是我的整体代码:

    Private Sub cmdSavetest_Click()
    Set Connect = New Class1
    Set rx = New adodb.Recordset

    Dim dataID As String

    dataID = lblID.Caption

    rx.Open "SELECT * FROM tblclient WHERE ID = '& Trim$(dataID) &'", con, 3, 3

    If rx.EOF Then
        MsgBox "No matching data"
    Else
         MsgBox "There is a match"
    End If

     With rx
      .Fields("Firstname") = txtFirstName.Text
      .Fields("Lastname") = txtLastName.Text
      .Fields("District") = txtDistrict.Text
      .Fields("co_Maker") = txtCoMaker.Text
      .Fields("Address") = txtAddress.Text
      .Fields("Interest") = txtInterest.Text
      .Fields("ContactNo") = txtContactNo.Text
      .Update
    End With

    rx.Close
End Sub

你的SQL语句大概应该是:

"SELECT * FROM tblclient WHERE ID = '" & Trim$(dataID) & "'"

给你这条线:

rx.Open "SELECT * FROM tblclient WHERE ID = '" & Trim$(dataID) & "'", con, 3, 3

在对 SQL 语句进行故障排除时,您可以将它变成一个字符串变量,这样您就可以确保它在立即 window 中看起来不错(VB6 中的 Ctrl-G IDE ):

Dim sSQLStatement As String
sSQLStatement = "SELECT * FROM tblclient WHERE ID = '" & Trim$(dataID) & "'"
rx.Open sSQLStatement, con, 3, 3

您可以在 rx.Open 行上添加断点(在要中断的行上按 F9),然后将鼠标悬停在 [=16] 上检查 sSQLStatement 的值=] 变量或在立即 window 中键入以下内容:

? sSQLStatement

至于你的第二个问题,我想你只需要将.Value添加到Fields 属性:

With rx
    .Fields("Firstname").Value = txtFirstName.Text
    .Fields("Lastname").Value = txtLastName.Text
    .Fields("District").Value = txtDistrict.Text
    .Fields("co_Maker").Value = txtCoMaker.Text
    .Fields("Address").Value = txtAddress.Text
    .Fields("Interest").Value = txtInterest.Text
    .Fields("ContactNo").Value = txtContactNo.Text
    .Update
End With