Page 1 of 2 12 LastLast
Results 1 to 15 of 20
  1. #1
    carmenv323 is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Oct 2021
    Location
    Massachusetts
    Posts
    78

    Recordset returning only first value

    Hello,



    I copied the following from the SQL view of my query
    'SELECT EstimateUsers.Email
    'From EstimateUsers
    'WHERE (((EstimateUsers.Enabled)=True));

    This returns all the rows that fit the where clause, but when I adjust it to VBA I'm only getting the first result. What am I doing wrong so that I can see all the rows?

    Thank you

    Code:
            Dim SQL, strReviewers As String
            Dim rstReviewer As DAO.Recordset
    
    
            SQL = "SELECT EstimateUsers.Email" & _
                " From EstimateUsers " & _
                " WHERE (((EstimateUsers.Enabled)=True));"
    
    
            Set rstReviewer = CurrentDb.OpenRecordset(SQL)
            strReviewers = rstReviewer!Email
    
    
            MsgBox strReviewers
    Eventually I'll need to add an ";" in between to use in an email

  2. #2
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    You have to loop over the records in a recordset. FYI - SQL is a variant because you didn't explicitly typecast it. Don't know if you're aware of that.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  3. #3
    VicM is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    Sep 2017
    Location
    US-PA & FL
    Posts
    55
    Hi carmenv323,

    The SQL statement in the query reports all the requested results as you've stated.

    However in order to achieve the results you want in a VBA subroutine, you have to do a bit more work.

    I'll post the code, but I shortened some of the names in your code to make my work go faster. You'll have to insert your particular names in place instead.

    Code:
           Dim SQL As String, strReviewers As String, mbox As String  'The mbox string will build what eventually
            Dim rstReviewer As DAO.Recordset                           'gets output in the Msgbox
            Dim i As Integer
    
    
            SQL = "SELECT EUEmail" & _
                " From EUser " & _
                " WHERE EUEnabled=True;"
    
    
            Set rstReviewer = CurrentDb.OpenRecordset(SQL, dbOpenDynaset)
            strReviewers = rstReviewer.Fields("EUEmail")
            rstReviewer.MoveLast    'You have to execute these two statements because the recordset doesn't yet know
            rstReviewer.MoveFirst   'how many records it has.  And you have to return it to the beginning in order for
                                            'the next loop to function properly.
    
        For i = 1 To rstReviewer.RecordCount        'The previous two statements got us number of records in recordset
            If i <> rstReviewer.RecordCount Then    'We set a loop for that number of records
                mbox = mbox & rstReviewer.Fields(0) & ", " 'There's only one column in your recordset, its index is 0 (zero)
            Else                                                        'We add the comma to delineate the emails
                mbox = mbox & rstReviewer.Fields(0)        'But don't add it after the last record
            End If
            rstReviewer.MoveNext                        'After each iteration we have to move to next record in recordset
        Next i
    
            MsgBox mbox                                   'And VOILA! this will print out all the emails.

  4. #4
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    For i = 1 To rstReviewer.RecordCount 'The previous two statements got us number of records in recordset
    If i <> rstReviewer.RecordCount Then 'We set a loop for that number of records
    mbox = mbox & rstReviewer.Fields(0) & ", " 'There's only one column in your recordset, its index is 0 (zero)
    Else 'We add the comma to delineate the emails
    mbox = mbox & rstReviewer.Fields(0) 'But don't add it after the last record
    End If
    rstReviewer.MoveNext 'After each iteration we have to move to next record in recordset Next i
    Or you could do what pretty much everyone else does, just walk the recordset until EOF?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  5. #5
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    A MsgBox can display only so much data. Not practical if thousands of records involved.

    What do you really want to accomplish beyond simply opening a recordset object? If just want to view data, would be better to open a form or report bound to query.
    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.

  6. #6
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    ^^^ what he said. A common approach is to just drop the last separator character with Left(mbox,Len(mbox)-1)
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  7. #7
    VicM is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    Sep 2017
    Location
    US-PA & FL
    Posts
    55
    There are obviously several ways to accomplish this. Since I don't do this for a living and just dabble for my own benefit, it's what I came up with. All your other suggestions are very valid.
    And as June7 stated, this is an unwieldy way to show those records. The table I created contained only 3 records. So it was basically a proof of concept.
    Perhaps carmenv can absorb all the suggestions.

  8. #8
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    Quote Originally Posted by VicM View Post
    There are obviously several ways to accomplish this. Since I don't do this for a living and just dabble for my own benefit, it's what I came up with. All your other suggestions are very valid.
    And as June7 stated, this is an unwieldy way to show those records. The table I created contained only 3 records. So it was basically a proof of concept.
    Perhaps carmenv can absorb all the suggestions.
    I generally go with the least code as long as it is understandable? No point writing a flash one liner, if it is hard to work out what it is doing.
    I would also consider myself a dabbler.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  9. #9
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,654
    I would also consider myself a dabbler.
    Not me. I'm an addict. Sometimes I think I need an intervention, or Access Anonymous, or something.
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  10. #10
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    Quote Originally Posted by moke123 View Post
    Not me. I'm an addict. Sometimes I think I need an intervention, or Access Anonymous, or something.
    Well I did still contribute when I was on holiday.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  11. #11
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,654
    Well I did still contribute when I was on holiday.
    Tell tale sign of addiction.
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  12. #12
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    My tweak is to put the comma first

    s=“,” & newvalue & s

    then just one function

    s=mid(s,2)

    many ways, just use one you are comfortable with

  13. #13
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    I would use ; to start with?
    Also Outlook does not complain if the string ends with a ; so no real need to trim the string?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  14. #14
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    I would use ; to start with?
    That would be a Regional Settings thing?
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  15. #15
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    Quote Originally Posted by Micron View Post
    That would be a Regional Settings thing?
    OP said they were eventually going to use ; ?

    Eventually I'll need to add an ";" in between to use in an email
    I must admit I was not aware that it was regional settings issue. I use , to seperate parameters in functions, yet use ; for multiple email addresses?
    I thought ; was a standard email address separator, regardless of region.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Recordset Returning Selected Day Of Week
    By DMT Dave in forum Access
    Replies: 16
    Last Post: 03-03-2022, 06:28 PM
  2. Returning a recordset for a form
    By Newby in forum Access
    Replies: 6
    Last Post: 02-28-2013, 09:59 AM
  3. Difficulty returning ADODB recordset from function
    By randman1 in forum Programming
    Replies: 4
    Last Post: 07-19-2012, 01:07 PM
  4. Recordset returning blank value
    By Mohamed in forum Access
    Replies: 1
    Last Post: 10-24-2011, 09:31 AM
  5. Recordset not returning records
    By TinaCa in forum Programming
    Replies: 3
    Last Post: 08-03-2011, 09:26 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