如何更新记录集?如何将数据值从数据网格传递到文本框并在VB6中编辑?

时间:2015-10-03 02:30:01

标签: ms-access datagrid vb6

我在我的系统中使用VB6。我想将数据网格的选定行值传递给文本框并编辑记录。但是每次运行代码时我都会收到此错误。 “BOF或EOF都是True,或者当前记录已被删除。请求的操作需要当前记录。”这是我更新按钮中的代码。请帮忙。提前致谢! :d

Private Sub cmdEdit_Click()
Dim conn As New Connection
Dim myRS As New Recordset
Dim sql As Integer

conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;DataSource=C:\Users\FSCNDCIT\Desktop\GSTD\GSTDdb.mdb"
myRS.CursorLocation = adUseClient
myRS.Open "SELECT * FROM Table1 WHERE ID = '" & DataGrid1.Text & "'", conn, adOpenDynamic, adLockBatchOptimistic

frmGoSee.txtID.Text = myRS!ID  'This line was highlighted.
frmGoSee.txtGSTD.Text = myRS!GSTDCode
frmGoSee.txtGSTDCode.Text = myRS!WorkGroup
frmGoSee.txtTL.Text = myRS!TL
frmGoSee.txtDeptHead.Text = myRS!DeptHead
frmGoSee.txtParticipants.Text = myRS!Participants
frmGoSee.txtCoach.Text = myRS!Coach
frmGoSee.txtProblem_Des.Text = myRS!Problem_Des
frmGoSee.txtMI.Text = myRS!MI
frmGoSee.txtInter_Correction.Text = myRS!Inter_Correction
frmGoSee.txtICWho.Text = myRS!ICWho
frmGoSee.txtICWhen.Text = myRS!ICWhen
frmGoSee.txtICStatus.Text = myRS!ICStatus
frmGoSee.lblpicture.Caption = myRS!Picture
frmGoSee.Image1.Picture = LoadPicture(lblpicture)

myRS.Update
Set myRS = Nothing
conn.Close

End Sub

1 个答案:

答案 0 :(得分:1)

错误告诉您查询没有带回任何记录。您的代码只假设会有一条记录。在尝试分配值之前,应检查空记录集。

Private Sub cmdEdit_Click()
    Dim conn As New Connection
    Dim myRS As New Recordset
    Dim sql As Integer

    conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;DataSource=C:\Users\FSCNDCIT\Desktop\GSTD\GSTDdb.mdb"
    myRS.CursorLocation = adUseClient
    myRS.Open "SELECT * FROM Table1 WHERE ID = '" & DataGrid1.Text & "'", conn, adOpenDynamic, adLockBatchOptimistic

    If myRS.EOF = False Then
        frmGoSee.txtID.Text = myRS!ID  'This line was highlighted.
        frmGoSee.txtGSTD.Text = myRS!GSTDCode
        frmGoSee.txtGSTDCode.Text = myRS!WorkGroup
        frmGoSee.txtTL.Text = myRS!TL
        frmGoSee.txtDeptHead.Text = myRS!DeptHead
        frmGoSee.txtParticipants.Text = myRS!Participants
        frmGoSee.txtCoach.Text = myRS!Coach
        frmGoSee.txtProblem_Des.Text = myRS!Problem_Des
        frmGoSee.txtMI.Text = myRS!MI
        frmGoSee.txtInter_Correction.Text = myRS!Inter_Correction
        frmGoSee.txtICWho.Text = myRS!ICWho
        frmGoSee.txtICWhen.Text = myRS!ICWhen
        frmGoSee.txtICStatus.Text = myRS!ICStatus
        frmGoSee.lblpicture.Caption = myRS!Picture
        frmGoSee.Image1.Picture = LoadPicture(lblpicture)

        'Commented because nothing in the record has changed
        'There is nothing to update
        'myRS.Update
    End If

    'checking the state of your objects here before closing would be good practice
    If Not myRS Is Nothing Then
        If myRS.State = adStateOpen Then
            myRS.Close
        End If
        Set myRS = Nothing
    End If
    If Not conn Is Nothing Then
        If conn.State = adStateOpen Then
            conn.Close
        End If
        Set conn = Nothing
    End If

End Sub