Results 1 to 3 of 3
  1. #1
    charis89 is offline Novice
    Windows 7 32bit Access 2003
    Join Date
    Aug 2015
    Posts
    8

    unable to assign recordset result to variable in access 2003


    I have a form with a button, when clicked, it performs the following

    Code:
    Private Sub btnAllList_Click()
    
    
    Dim dbTemp As DAO.Database
    Dim rstTemp As DAO.Recordset
    Dim minNo, maxNo As Integer
    
    
    Set dbTemp = CurrentDb
    
    
    strSQL = "SELECT Min([id_no]) AS FirstNo, Max([id_no]) AS LastNo FROM T_RESULT;"
    
    
    Set rstTemp = dbTemp.OpenRecordset(strSQL)
    
    
    minNo = 0
    maxNo = 0
    
    
    If Not (rstTemp.BOF And rstTemp.EOF) Then
        If rstTemp.RecordCount <> 0 Then
            Do While Not rstTemp.EOF
                minNo = rstTemp![FirstNo]
                maxNo = rstTemp![LastNo]
                MsgBox "minNo: " & minNo & ", rstTemp![LastNo]: " & rstTemp![LastNo] & ", maxNo: " & maxNo
                Exit Do
            Loop
        End If
    End If
    
    
    rstTemp.Close
    
    
    Exit_btnAllList_Click:
        Exit Sub
    
    
    Err_btnAllList_Click:
        MsgBox Err.Description
        Resume Exit_btnAllList_Click
    End Sub
    What I tried to do so far is get the minimum and maximum id no from the table T_RESULT. Message box shows "minNo: 284, rstTemp![LastNo]: 290, maxNo: 0".
    Not sure why maxNo is not getting 290.
    Datatype of id no in T_RESULT is Number.

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Why are you looping recordset? That query will have no more than one record.

    If Not rst.EOF Then
    minNo = rstTemp![FirstNo]
    maxNo = rstTemp![LastNo]
    End If
    MsgBox "FirstNo: " & minNo & ", LastNo: " & maxNo

    minNo variable type is not specified, maxNo is set as Integer. What number type is id_no field? An autonumber (long integer)?

    Every variable must have its own type specified or it will default to Variant. Variant is okay.

    Dim minNo As Long, maxNo As Long
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    charis89 is offline Novice
    Windows 7 32bit Access 2003
    Join Date
    Aug 2015
    Posts
    8
    The number type for the field is long integer.
    I changed it to long and it works now. Thanks!!

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 12
    Last Post: 07-25-2013, 12:49 PM
  2. Replies: 1
    Last Post: 11-09-2011, 12:08 PM
  3. Unable to Read from COM ports in Access 2003
    By userThree in forum Programming
    Replies: 3
    Last Post: 07-31-2011, 08:59 PM
  4. Replies: 6
    Last Post: 07-13-2011, 02:47 PM
  5. Access 2003 returns empty recordset
    By Leelers in forum Queries
    Replies: 0
    Last Post: 03-20-2009, 11:11 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums