runtime error 3021
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: runtime error 3021

  1. #1
    Join Date
    Jan 2011
    Posts
    12

    runtime error 3021

    runtime error 3021 (either BOF or EOF is true, or the current record
    has been deleted. requested operation requires a current record)

    heres my code

    Code:
    Dim rs As ADODB.Recordset
    Dim bData() As Byte
    
    Private Sub Form_Load()
        MSComm1.PortOpen = True
        connect
        Set rs = New ADODB.Recordset
        rs.Source = "Users"
        rs.ActiveConnection = cn
        rs.CursorLocation = adUseClient
        rs.LockType = adLockOptimistic
        rs.Open
        
        
        
    End Sub
    
    Private Sub showRecord()
        If rs.EOF Or rs.BOF Then Exit Sub
        ReDim bData(rs.Fields("photo").ActualSize)
        bData = rs.Fields("photo").Value
        szfile = App.Path & "\tempPic.tmp"
        Open szfile For Binary Access Write As #1
        Put #1, , bData
        Close #1
        Image1.Picture = LoadPicture(szfile)
        Label1.Caption = rs.Fields("Studentno")
        Label3.Caption = rs.Fields("Surname")
        Label4.Caption = rs.Fields("Firstname")
        Label5.Caption = rs.Fields("Middlename")
        Label6.Caption = rs.Fields("College")
        Label7.Caption = rs.Fields("Course")
        Label8.Caption = rs.Fields("Yrsec")
        
    
        
    End Sub
    
    
    
    Private Sub MSComm1_OnComm()
    Text1.Text = Clear
    Dim strinput As String
    strinput = MSComm1.Input
    Text1.Text = Text1.Text & strinput
    Label9.Caption = Format$(Now, "m/d/yy h:mm Am/Pm")
    End Sub
    
    Private Sub Text1_Change()
    Dim strSQL As String
    rs.Close
    strSQL = "SELECT * "
    strSQL = strSQL & "FROM users "
    strSQL = strSQL & "WHERE  Rfid = '" & Text1.Text & "'"
    rs.Open strSQL, GetConnString, adOpenForwardOnly, adLockReadOnly
    
    If Text1.Text = rs.Fields("Rfid") Then <----- error is here
    showRecord
    Else
    Form1.Show
    End If
    
    
    End Sub
    Private Function GetConnString() As String
        GetConnString = "Provider=Microsoft.jet.oledb.4.0;Data Source=" & App.Path & "\RegID.mdb"
    End Function

  2. #2
    DataMiser is online now Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    4,838

    Re: runtime error 3021

    The error is basically telling you that the query returned no results and you are trying to read from something that is not there.
    Always use [code][/code] tags when posting code.

  3. #3
    Join Date
    Jan 2011
    Posts
    12

    Re: runtime error 3021

    yes thats true.. but if ididnt return any result, isnt it must go to the else then open form1?? because it is in if clause

  4. #4
    DataMiser is online now Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    4,838

    Re: runtime error 3021

    Quote Originally Posted by killua14 View Post
    yes thats true.. but if ididnt return any result, isnt it must go to the else then open form1?? because it is in if clause
    Code:
     
    If Text1.Text = rs.Fields("Rfid") Then
    Will always return an error when there are no records in your recordset. Basically you are trying to compare the value in text1 to something that does not exist and that results in an error.

    You must either check the BOF EOF status first or check the recordcount property. You can only read the fields of the RS when there is a valid record currently selected.
    Always use [code][/code] tags when posting code.

  5. #5
    Join Date
    Jan 2011
    Posts
    12

    Re: runtime error 3021

    so how to that?? checking the BOF and EOF?? thxs in advance

  6. #6
    DataMiser is online now Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    4,838

    Re: runtime error 3021

    By using an If Then block of course. BOF and EOf are both properties of the RS object. There are literally 1,000s of examples plastered all over the internet.
    Always use [code][/code] tags when posting code.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center