Page 1 of 2 12 LastLast
Results 1 to 15 of 20
  1. #1
    Miles R is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Mar 2019
    Posts
    280

    FindFirst in recordset where Set recordset is used

    I'm trying to debug some code where FindFirst matching on a name is not getting the correct record.
    I'm a bit rusty on my record sets and before I can continue debugging, I'm puzzled by the following code
    Code:
    Set rstBirdRecord = [Form_Bird List].Recordset
    rstBirdRecord.FindFirst "BirdName = """ & strBirdName & """"
    What is puzzling is why the FindFirst triggers the Current Event of the form [Form_Bird List]
    I am sure I must have understood this when I wrote the code years ago, but can't figure it out now. Surely it is finding the record in rstBirdRecord and not [Form_Bird List].Recordset
    Or is rstBirdRecord just an alias?

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 11 Access 2021
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    What event is this code in?

    I just tested this code and it is going to matched record on form. This will trigger Current event.
    If there is no match then focus just goes to first record on form.
    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
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,157
    If you are on the form concerned use the RecordsetClone for searches then set the forms bookmark to match the recordset bookmark if found, not the forms actual recordset.
    Last edited by Minty; 10-22-2024 at 08:32 AM.
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  4. #4
    Miles R is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Mar 2019
    Posts
    280
    Thanks for the replies.
    The event is on another form and I do actually want to trigger the Current Event in the Bird list form.
    I was just puzzled by whether Set rstBirdRecord = [Form_Bird List].Recordset creates an alias for the original recordset. I'm assuming it does.

    Anyway, I have solved the original bug by doing a Requery on the recordset. It seems to have been confused when the record source of the Bird List form was recreated.

  5. #5
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,879
    A findfirst procedure in the form would look like this

    Code:
    Dim rs as DAO.Recordset
    Set rs = Me.recordsetClone
    
         with rs
             .findFirst "BirdName = """ & strBirdName & """"
    
         if not .NoMatch then
              Me.bookmark = .bookmark
         else
             'A messagebox if preferred
          end if
    
         end with
    edit: If on another form or subform then you would set the the recordset = YourformReference.RecordsetClone and the bookmark as YourformReference.bookmark = .bookmark
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  6. #6
    Miles R is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Mar 2019
    Posts
    280
    Thanks for the reply.
    Don't think I need a clone as I am working with the original recordset.
    I was just trying to figure out if Set rstBirdRecord = [Form_Bird List].Recordset means that they are equivalent. No one has answered that yet.
    I think they must be, because if I replace
    rstBirdRecord with [Form_Bird List] in the FindFirst line, I get the same result - although there is no Intellisense with [Form_Bird List] but there is with rstBirdRecord

  7. #7
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,157
    The point of using the RecordsetClone is that you don't mess with the forms recordset unnecessarily, and create strange errors like
    "This record has been edited by another User" etc.

    As you have discovered using FindFirst will move the forms record pointer regardless of whether it does or doesn't find a record, triggering the current event amongst others.
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  8. #8
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,879
    Code:
    Form_Bird List
    Using the Form_ prefix is generally used when instantiating a form instance. set frm = New Form_SomeForm
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  9. #9
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,556
    Quote Originally Posted by Miles R View Post
    Thanks for the reply.
    Don't think I need a clone as I am working with the original recordset.
    I was just trying to figure out if Set rstBirdRecord = [Form_Bird List].Recordset means that they are equivalent. No one has answered that yet.
    I think they must be, because if I replace
    rstBirdRecord with [Form_Bird List] in the FindFirst line, I get the same result - although there is no Intellisense with [Form_Bird List] but there is with rstBirdRecord
    I would say you do.
    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

  10. #10
    Miles R is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Mar 2019
    Posts
    280
    Thanks for the replies.

    I have solved the initial problem by doing a Requery of the Recordset before doing the FindFirst, although I don't know why this is needed, as changing the RecordSource with slightly different filters has already done a Requery.

    If I get the time, I will dig into this deeper, but it is solved anyway.

  11. #11
    Miles R is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Mar 2019
    Posts
    280
    Further info on this - there is sometimes a discrepancy in values from the current row of the record set :
    Me!TaxonomySequence.Value gives one number and [Form_Bird List].Recordset.Fields("TaxonomySequence") gives a different number (the correct one) unless I use Requery. (both refer to the record set of [Form_Bird List]).
    Maybe its something to do with Dirty values possibly.
    Not sure yet.

  12. #12
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,556
    Try the .Text property of the control.
    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

  13. #13
    Miles R is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Mar 2019
    Posts
    280
    Tried that but Me!TaxonomySequence.Text gives an error - object does not support this property or method
    Don't forget that M!TaxonomySequence is referring to the Recordset and is not a control.

  14. #14
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,879
    Don't forget that M!TaxonomySequence is referring to the Recordset and is not a control.
    How could we forget it if this is the first time your mentioning it?

    I suspect you doing more than what you are telling us. If your adding or editing in the form it won't be in the recordset until you save it and refresh the recordset.
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  15. #15
    Miles R is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Mar 2019
    Posts
    280
    Moke123,

    Thanks for the reply and sorry to be pedantic, but I did mention it further up when I said :
    Me!TaxonomySequence.Value gives one number and [Form_Bird List].Recordset.Fields("TaxonomySequence") gives a different number (the correct one) unless I use Requery. (both refer to the record set of [Form_Bird List]).

    Yes, I am adjusting the recordset, but from my testing, the record set is updated and the syntax
    [Form_Bird List].Recordset.Fields("TaxonomySequence") gives the correct value, but Me!TaxonomySequence.Value gives another, which does not seem to be the value in the current record.

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

Similar Threads

  1. Replies: 7
    Last Post: 09-28-2023, 08:41 AM
  2. How to Set Focus to a Record and Using FindFirst
    By BallinWallin in forum Programming
    Replies: 3
    Last Post: 07-20-2014, 08:05 AM
  3. recordset .findfirst question
    By RonL in forum Programming
    Replies: 4
    Last Post: 06-22-2013, 01:12 PM
  4. Recordset FindFirst Not Working
    By ShoresJohn in forum Programming
    Replies: 5
    Last Post: 03-01-2012, 06:59 PM
  5. Recordset Findfirst Problem
    By ColPat in forum Programming
    Replies: 6
    Last Post: 07-22-2010, 04:34 AM

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