Results 1 to 15 of 15
  1. #1
    easyrider is offline Advanced Beginner
    Windows 11 Office 365
    Join Date
    Dec 2021
    Location
    Lancaster County, PA
    Posts
    64

    Looping though a recordset returns the 'Epoch' date

    Hi guys,



    I have a a form that during the On Load event assigns a date to a textbox based on a public function. This public function loops through the records in a certain table and looks for a specific type of record. The function then returns that date to the textbox.

    The problem that I'm having is that if a date for the specific type of record exists, it returns the correct date. If it does not exist, then it returns the 'Epoch' date of 12/30/1899.

    How do I get the textbox to display nothing instead of the Epoch date if there are no matching records?

    sample db is attached. To demonstrate, open it and on the main form, click the "Show Final Visit" button for the first user. On the Show Visit form, the final visit date is 12/30/1899. Close the show visit form and navigate (little blue buttons at the bottom of the main form) to the next user that does have a record with final date.

    STNG_Planets.zip

  2. #2
    xps35's Avatar
    xps35 is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Jun 2022
    Location
    Schiedam, NL
    Posts
    299
    Change the first line of the function to:
    Code:
    Public Function GetFinalVisitDate(User_ID As Integer) As Variant
    Groeten,

    Peter

  3. #3
    easyrider is offline Advanced Beginner
    Windows 11 Office 365
    Join Date
    Dec 2021
    Location
    Lancaster County, PA
    Posts
    64
    Quote Originally Posted by xps35 View Post
    Change the first line of the function to:
    Code:
    Public Function GetFinalVisitDate(User_ID As Integer) As Variant
    Well, THAT was an easy fix. Thanks!!

  4. #4
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,879
    Why a recordset and not a dlookup?

    Code:
    dlookup("VisitDate","Visit_T","User_ID = " & User_ID & " And VisitType_ID = 3")
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  5. #5
    easyrider is offline Advanced Beginner
    Windows 11 Office 365
    Join Date
    Dec 2021
    Location
    Lancaster County, PA
    Posts
    64
    Quote Originally Posted by moke123 View Post
    Why a recordset and not a dlookup?

    Code:
    dlookup("VisitDate","Visit_T","User_ID = " & User_ID & " And VisitType_ID = 3")
    A while back, I had watched several videos by Sean McKenzie on youtube about looping through a recordset and thought it was a cool new tool to add to my problem solving toolbox. Certainly your suggestion will work tho, thanks!

  6. #6
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,879
    As long as you realize your not really looping the recordset, assuming there is only one final visit for each userID. You are only opening one record.
    You can just as easily use the dlookup as the controlsource of the textbox.

    Code:
    =dlookup("VisitDate","Visit_T","User_ID = " & me.User_ID & " And VisitType_ID = 3")
    Attached Files Attached Files
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  7. #7
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,556
    Quote Originally Posted by easyrider View Post
    A while back, I had watched several videos by Sean McKenzie on youtube about looping through a recordset and thought it was a cool new tool to add to my problem solving toolbox. Certainly your suggestion will work tho, thanks!
    You could use a wood chisel as a screwdriver, but not really the correct tool?
    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

  8. #8
    Join Date
    Jun 2010
    Location
    Belgium
    Posts
    1,180
    be careful, looping through a recordset is sometimes necessary, but is a painstaking, slow process and should be kept as a last resort solution. See also RBAR (Row By Agonizing Row)

  9. #9
    easyrider is offline Advanced Beginner
    Windows 11 Office 365
    Join Date
    Dec 2021
    Location
    Lancaster County, PA
    Posts
    64
    Quote Originally Posted by Welshgasman View Post
    You could use a wood chisel as a screwdriver, but not really the correct tool?
    What about using a screwdriver as a wood chisel? Lol. Dlookup only returns a single value based on the criteria specified. In this case, I wanted to find any and all records that met a certain criteria. Since you don't like my choice of tools, what would you use?

  10. #10
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,556
    Going on post #6, it would be DlookUp()
    However looking at your code, there is no order, so if I added a record, that had a date before the last date of visit, because I forgot it at the time, then you would get that, if more than one record exists, which I would have thought is very likely?
    In which case DMax() seem more appropriate?

    Still using a recordset, it would be sorted by date descending and pick first record. However why retieve a set of records, when only one is required?
    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,879
    Dlookup only returns a single value based on the criteria specified. In this case, I wanted to find any and all records that met a certain criteria.
    Perhaps we need some context.

    "Final Visit" suggests that there would only be 1 final visit and your table structure suggest there would only be 3 visits total per user.
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  12. #12
    Join Date
    Jun 2010
    Location
    Belgium
    Posts
    1,180
    I wanted to find any and all records that met a certain criteria
    In this case a select statement with the correct WHERE criterium does the trick. If you only want 1 value add TOP 1 and the correct ORDER BY. I really don't see why you would want to loop through a recordset.

  13. #13
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,556
    In fact if you made the subform a little bigger, big enough for 3 records, there would be no need for the second form. Everything would be visible on the main form?
    Attached Thumbnails Attached Thumbnails Final.png  
    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
    easyrider is offline Advanced Beginner
    Windows 11 Office 365
    Join Date
    Dec 2021
    Location
    Lancaster County, PA
    Posts
    64
    Sorry for the hiatus - I had to go out of town the other week unexpectedly. Thanks to Peter for answering my initial question regarding the Epoch date.

    After reviewing the other comments/suggestions about looping through the recordset, I've decided to follow the suggestion by moke123 and use DLookup instead. However due to the alleged performance issues of the domain aggregate functions like DLookup, I created a query to get the record I'm after and then run DLookup against that query.

    Cheers,
    -Bill

  15. #15
    Join Date
    Apr 2017
    Posts
    1,792
    Quote Originally Posted by easyrider View Post
    What about using a screwdriver as a wood chisel? Lol. Dlookup only returns a single value based on the criteria specified. In this case, I wanted to find any and all records that met a certain criteria. Since you don't like my choice of tools, what would you use?
    I'd create an UDF, e.g. LastVisit(parUserID, parQueryType), which will return a value from a query like
    SELECT TOP 1 VisitDate FROM visit_T WHERE (QueryConditions using parameter values of UDF) ORDER BY VisitDate DESC

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

Similar Threads

  1. Looping through DAO recordset
    By virgilio in forum Access
    Replies: 7
    Last Post: 02-27-2020, 11:32 PM
  2. Replies: 3
    Last Post: 03-06-2019, 11:49 AM
  3. Replies: 3
    Last Post: 08-08-2018, 08:13 AM
  4. Replies: 7
    Last Post: 08-26-2016, 07:45 AM
  5. looping through recordset (columnwise)
    By pradeep.sands in forum Queries
    Replies: 1
    Last Post: 06-27-2013, 09:46 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