Results 1 to 7 of 7
  1. #1
    DMT Dave is online now VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,193

    Trying To Find Closest Match

    Hi Guy's after a stressful week, im struggling with this

    I am trying to find a mileage from 3 months ago up to date (today), the dates records may not match so anything from closest date to 08/12/12 to 08/03/23 and calculate what mileage difference is

    The following is coming back with incorrect result, can't the the forest for trees!!!!



    Code:
    Dim dtStart As Date, dtEnd As DateDim iMilesStart As Integer, iMilesEnd As Integer, iMilesTotal As Integer
    
    
    dtStart = InputBox("Enter Start Date ?" & vbCrLf & vbCrLf & _
        "Default is 3 Months", "ENTER START DATE", DateAdd("m", -3, Now()))
        
    dtEnd = InputBox("Enter End Date ?" & vbCrLf & vbCrLf & _
        "Default is Now", "ENTER END DATE", Format(Now(), "dd-mmm-yyyy"))
        
    iMilesStart = Nz(DMin("Mileage", "tblServiceData", "[FuelDate] > #" & dtStart & "#"), 0)
    iMilesStart = Nz(DMin("Mileage", "tblServiceData", "[FuelDate] < #" & dtEnd & "#"), 0)
        
        Forms!frmService!txtStart = dtStart
        Forms!frmService!txtMilesStart = iMilesStart
        Forms!frmService!txtEnd = dtEnd
        Forms!frmService!txtMilesEnd = iMilesEnd
        Forms!frmService!txtMilesTotal = iMilesEnd - iMilesStart

  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,521
    For starters, you realize you set iMilesStart twice and never set iMilesEnd?
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Oh, and for the end I think you'd want DMax, not DMin.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  4. #4
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,927
    Quote Originally Posted by pbaldy View Post
    For starters, you realize you set iMilesStart twice and never set iMilesEnd?
    Bad copying and pasting Dave?
    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
    DMT Dave is online now VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,193
    Yes agree, i need Ctrl+Alt+Delete on the side of my head

    I can clearly see what Paul pbaldy pointed out....

  6. #6
    DMT Dave is online now VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,193
    Hi Guy's yes fixed the issue now thanks to you guy's noticing, also i had some over flow in values when up to a 6 digit number we as set as integer, change it to Long and perfect

    there are no decimals or anything and In my experience i thought Integer is a whole number and Long can be split into decimals etc.... ?

    However, this works great

    Code:
    Dim dtStart As Date, dtEnd As DateDim lMilesStart As Long, lMilesEnd As Long, lMilesTotal As Integer
    Dim sVeh As String
    
    
    sVeh = Me.Vehicle
    
    
    dtStart = InputBox("Enter Start Date ?" & vbCrLf & vbCrLf & _
        "Default is 3 Months", "ENTER START DATE", Format(DateAdd("m", -3, Now()), "dd-mm-yyyy"))
        
    dtEnd = InputBox("Enter End Date ?" & vbCrLf & vbCrLf & _
        "Default is Now", "ENTER END DATE", Format(Now(), "dd-mm-yyyy"))
        
    lMilesStart = Nz(DMin("Mileage", "tblServiceData", "[FuelDate] > #" & dtStart & "# And [Vehicle] = '" & sVeh & "'"), 0)
        Forms!frmService!txtStart = dtStart
    lMilesEnd = Nz(DMax("Mileage", "tblServiceData", "[FuelDate] < #" & dtEnd & "# And [Vehicle] = '" & sVeh & "'"), 0)
        
        Forms!frmService!txtMilesStart = lMilesStart
        Forms!frmService!txtEnd = dtEnd
        Forms!frmService!txtMilesEnd = lMilesEnd
        Forms!frmService!txtMilesTotal = lMilesEnd - lMilesStart

  7. #7
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    About the number types:

    Data Type Description Decimal precision Storage size
    Byte Numbers from 0 to 255 (no fractions).
    Use Number | Byte in preference to Yes/No (True=-1, False=0)
    None 1 byte
    Decimal Numbers from -10^28-1 through 10^28-1 (.mdb) 28 12bytes
    Integer Numbers from -32,768 to 32,767 (no fractions). None 2 bytes
    Long Integer (Default) Numbers from -2,147,483,648 to 2,147,483,647 (no fractions). None 4 bytes
    Single Numbers from
    -3.402823E38 to -1.401298E-45
    for negative values and from
    1.401298E-45 to 3.402823E38 for positive values.
    7 4 bytes
    Double Numbers from
    -1.79769313486231E308 to -4.94065645841247E–324
    for negative values and from
    4.94065645841247E-324 to 1.79769313486231E308 for positive values.
    15 8 bytes
    Replication ID Globally unique identifier (GUID) N/A 16 bytes


    From here https://ss64.com/access/syntax-datatypes.html
    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 ↓↓

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

Similar Threads

  1. Find File Name That Is Closest Match
    By DMT Dave in forum Access
    Replies: 10
    Last Post: 06-07-2022, 11:32 AM
  2. Find Closest Lowest Number
    By koturtle in forum Queries
    Replies: 9
    Last Post: 03-31-2020, 02:57 PM
  3. Update table based on closest match on date
    By dalahans in forum Queries
    Replies: 6
    Last Post: 06-07-2018, 07:50 AM
  4. Trouble finding closest match
    By cutsygurl in forum SQL Server
    Replies: 1
    Last Post: 02-22-2013, 03:59 PM
  5. find the closest year
    By 12345678 in forum Queries
    Replies: 1
    Last Post: 05-25-2011, 05:07 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