简体   繁体   中英

How to update recordset? How to pass data value from a datagrid to textbox and edit in VB6?

I am using VB6 in my system. I want to pass the selected row value of a datagrid to the textbox and edit the record. But I'm getting this error every time I run the code. "Either BOF or EOF is True, or the current record has been deleted. Requested operation requires a current record." Here's my codes in update button. Please help. Thanks in advance! :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

The error is telling you that the query did not bring back any records. Your code just assumes there will be a record. You should check for an empty recordset before trying to assign values.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM