Results 1 to 9 of 9
  1. #1
    jtm013 is offline Competent Performer
    Windows 7 64bit Access 2013
    Join Date
    Aug 2014
    Posts
    117

    Help with a Recordset IF statement

    Hello All,



    I am trying to set up a Recordset to loop through a table and check if any records contain a specific value.
    I have the code below in place which seems to do the job, except it returns a message box for every record checked.
    I only require the code to check all the records and then supply one message box with the requisite information.
    How would I go about making this adjustment?

    Thanks for any and all assistance!

    Code:
    Dim rst As DAO.Recordset
    Set rst = CurrentDb.OpenRecordset("Select * FROM TblLogin WHERE [Login]=1")
    
    
    
    'Check to see if the recordset actually contains rows
    If Not (rst.EOF And rst.BOF) Then
        rst.MoveFirst
        Do Until rst.EOF = True
            If rst![statusCurrent] = "Online" Then
                MsgBox "Another User is Online" 
                Else
                    MsgBox "No Users Online"
            End If
            'Move to the next record. Don't ever forget to do this.
            rst.MoveNext
        Loop
    Else
        MsgBox "There are no records in the recordset."
    End If
    
    
    MsgBox "Finished looping through records."
    
    
    rst.Close 'Close the recordset
    Set rst = Nothing 'Clean up

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,643
    Declare a string variable, replace the message box in the loop with

    VariableName = VariableName & rst!UserName & vbCrLf

    then after the loop, if the variable length is greater than zero, give a message box using it. Replace rst!UserName with the appropriate field from the recordset.

    Personally I would add the status to the recordset SQL so that only the applicable records were returned.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    jtm013 is offline Competent Performer
    Windows 7 64bit Access 2013
    Join Date
    Aug 2014
    Posts
    117
    Quote Originally Posted by pbaldy View Post
    Personally I would add the status to the recordset SQL so that only the applicable records were returned.
    Wow, I must really need more coffee- that is a much simpler idea than I was trying to do...
    Final code ends up looking like this and works great.

    Code:
    Dim rst As DAO.RecordsetSet rst = CurrentDb.OpenRecordset("Select * FROM TblLogin WHERE [Login]=1 AND [statusCurrent]='Online'")
    '"Select * FROM 'Tbl_Corrective_Action_Group' WHERE [Login] = '1'")
    
    
    'Check to see if the recordset actually contains rows
    If Not (rst.EOF And rst.BOF) Then
        rst.MoveFirst
        Do Until rst.EOF = True
            rst.MoveNext
        Loop
    Else
        MsgBox "There are no other Users Online."
        rst.Close
        Set rst = Nothing
        Exit Sub
    End If
    
    
    MsgBox "There is another User Online."                                  
    
    
    rst.Close 'Close the recordset
    Set rst = Nothing 'Clean up
    
    
    End Sub
    Thanks for the assist! - SOLVED.

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,643
    Happy to help! I thought you wanted a list of the users, so it can be even simpler as you don't need the loop:

    Code:
    Dim rst As DAO.RecordsetSet rst = CurrentDb.OpenRecordset("Select * FROM TblLogin WHERE [Login]=1 AND [statusCurrent]='Online'")
    '"Select * FROM 'Tbl_Corrective_Action_Group' WHERE [Login] = '1'")
    
    
    'Check to see if the recordset actually contains rows
    If Not (rst.EOF And rst.BOF) Then
      MsgBox "There is another User Online."
    Else
      MsgBox "There are no other Users Online."
      rst.Close
      Set rst = Nothing
      Exit Sub
    End If
    
    rst.Close 'Close the recordset
    Set rst = Nothing 'Clean up
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    John_G is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    Here's an even easier way - doesn't even need a recordset or a Dim statement:


    if DCount("*","tblLogin","[Login] = 1 AND [statusCurrent]='Online'" ) > 0 then
    MsgBox "There is another User Online."
    Else
    MsgBox "There are no other Users Online."
    End If

  6. #6
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,649
    Since you just want to know if the value exists and don't really do anything with the recordset, an alternative is domain aggregate DLookup() or DCount().

    If Not IsNull(DLookup("ID", "TblLogin", "[Login]=1 AND [statusCurrent]='Online'")) Then
    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.

  7. #7
    jtm013 is offline Competent Performer
    Windows 7 64bit Access 2013
    Join Date
    Aug 2014
    Posts
    117
    Quote Originally Posted by pbaldy View Post
    Happy to help! I thought you wanted a list of the users, so it can be even simpler as you don't need the loop:
    OOOOOOhhhh right. Thanks again for the assist. I am using this code as a checker to make sure no users are logged into the database before putting it down for maintenance. Okay revised final code is now:

    Code:
    Dim rst As DAO.Recordset
    Set rst = CurrentDb.OpenRecordset("Select * FROM TblLogin WHERE [Login]=1 AND [statusCurrent]='Online')
    
    
    'Check to see if the recordset actually contains rows
    If Not (rst.EOF And rst.BOF) Then
        MsgBox "There is another User Online." _
            & vbCrLf & "Use Maintenance Shutdown to Log Off the Users.", vbInformation, "Information"
    Else
        CurrentDb.Properties("StartupForm") = "frmDbDown"
    
    
            MsgBox "There are no other Users Online." _
                & vbCrLf & "DB is down for Maintenance.", vbInformation, "Information."
                    Me.lblDBIS.Caption = "DB Is Currently DOWN"
        rst.Close
        Set rst = Nothing
        Exit Sub
    End If
    
    
    rst.Close 'Close the recordset
    Set rst = Nothing 'Clean Up
    
    
    End Sub

  8. #8
    jtm013 is offline Competent Performer
    Windows 7 64bit Access 2013
    Join Date
    Aug 2014
    Posts
    117
    Quote Originally Posted by June7 View Post
    Since you just want to know if the value exists and don't really do anything with the recordset, an alternative is domain aggregate DLookup() or DCount().

    If Not IsNull(DLookup("ID", "TblLogin", "[Login]=1 AND [statusCurrent]='Online'")) Then
    I know a DLookup will work, but to be honest I wanted an excuse to use a Recordset since I am not very familiar with them and how they work, figured why not learn something new.

  9. #9
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,649
    Now you know they can be overkill in some situations.
    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.

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

Similar Threads

  1. Replies: 6
    Last Post: 12-03-2013, 11:14 PM
  2. Insert Recordset with conditional statement
    By mjd973 in forum Programming
    Replies: 3
    Last Post: 04-29-2013, 06:20 AM
  3. Replies: 2
    Last Post: 10-25-2012, 02:53 AM
  4. Form data in SELECT statement recordset
    By nvrwrkn in forum Programming
    Replies: 15
    Last Post: 10-16-2012, 03:57 PM
  5. Replies: 2
    Last Post: 03-08-2012, 12:59 PM

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