Page 2 of 2 FirstFirst 12
Results 16 to 25 of 25
  1. #16
    sheusz is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    May 2015
    Posts
    151
    Hi Arvil



    I've tried this and it works OK, except it does not show the matching result for the MachineNo on the current form, it is actually returning the highest value of HourMtr in the xService table. I need it to show the highest value for the HourMtr based on the MachineNo displayed on the current form (Me.MachineNo)?

  2. #17
    Join Date
    Apr 2017
    Posts
    1,681
    Quote Originally Posted by sheusz View Post
    Hi Arvil

    I've tried this and it works OK, except it does not show the matching result for the MachineNo on the current form, it is actually returning the highest value of HourMtr in the xService table. I need it to show the highest value for the HourMtr based on the MachineNo displayed on the current form (Me.MachineNo)?
    I used the part of formula in your post#1 as starting point.

    Maybe
    Code:
    DMax("[HourMtr]","xServices","[MachineNo]='" & Me.MachineNo & "'  AND HourMtr >0")

  3. #18
    sheusz is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    May 2015
    Posts
    151
    Hi Arvil
    Thanks for that. I'm getting the #Name? error in the text box. The on the form the MachineNo is displayed in a text box that is bound to the field MachineNo in table. Could this be the problem?

  4. #19
    Join Date
    Apr 2017
    Posts
    1,681
    Code:
    DMax("[HourMtr]", "xServices", "[MachineNo] = '" & Forms!YourFormName!YourControlName & "'"  AND [HourMtr] >0")
    Btw. I advice to rename all controls in your forms, as Access (and you too) can really get confused otherwise. E.g.:
    table field: FieldName;
    linked text box: txtFieldName;
    linked combo box: cbbFieldName;
    label for linked control: lblFieldName;
    etc.

    Also, name all forms like fFormName
    When you add another form fFormName2 to form fFormname1 as subform, Access by default names the subform control which contains the form fFormname2 with same name (i.e. fFormname2). I always immediately rename the subform control as sfFormname2, to avoid further confusion. NB! The subform control and the form displayed in subform are different objects, with entirely different set of properties. When you click on unselected subform once, the subform control will be activated. When you click on it again, the form contained in subform will be activated

  5. #20
    sheusz is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    May 2015
    Posts
    151
    Hi ArviL

    Thank you again for your explanations and recommendations. There is a lot of work in implementing your ideas, but I will get to it.

    I have noticed that the MachineNo field in the xServices table actually has a space in it. Hence the space in the code below. (yet something else to clean up)

    I've adapted the code you suggested, but get an error when trying to save it. The expression you entered contains an invalid string

    Code:
    =DMax("[HourMtr]", "xServices", "[Machine No] = '" & Forms!frmMachineComponents!txtMachineNo & "'"  AND [HourMtr] >0")
    I really do appreciate your help.

  6. #21
    Join Date
    Apr 2017
    Posts
    1,681
    [QUOTE=sheusz;515192]The expression you entered contains an invalid string[/code]
    Instead
    '" & Forms!frmMachineComponents!txtMachineNo & "'" AND [HourMtr] >0")
    must be
    '" & Forms!frmMachineComponents!txtMachineNo & "' AND [HourMtr] >0")
    or
    '" & Forms!frmMachineComponents!txtMachineNo & "'" & " AND [HourMtr] >0")

    Without DB I can't test it, so it all is on fly!

  7. #22
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    Code:
    =DMax("[HourMtr]", "xServices", "[Machine No] = '" & Forms!frmMachineComponents!txtMachineNo & "'  AND [HourMtr] > 0")
    ?
    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. #23
    sheusz is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    May 2015
    Posts
    151
    Hello AvriL & Welshgasman

    Thank you for your replies. They both have given me the result I was after

    As always I've spotted a couple of issues, not related to this problem, but things that need to be addressed "on the ground". Turns out that the HourMtr can be reset to a lower value than one preceding the last value recorded. (It's a firmware bug on the machines)

    e.g (in descending order of reading dd/mm/yy)

    01/08/23 500
    20/07/23 0
    18/07/23 900
    15/05/23 1200

    The code you have provided gives the highest value for HourMtr, which I think is the best way to go for now. But down the track I think I'll have to code for the highest value, then total all of the values after the highest reading was taken, and add them to the highest reading, to give true data. I think I can handle that.

  9. #24
    Join Date
    Apr 2017
    Posts
    1,681
    To get a latest non-zero HourMtr value for MachineNo, you can use DMAX() to get max date for this MachineNo with HourMTR > 0, and then use this found date to get HourMtr for this MachineNo at max date using DLOOKUP(). This will work so long as you never have several different HourMtr values for same MachineNo at same date.

  10. #25
    sheusz is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    May 2015
    Posts
    151
    Thanks AvriL That is a great help.

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

Similar Threads

  1. Replies: 3
    Last Post: 03-09-2021, 08:44 AM
  2. Replies: 2
    Last Post: 10-07-2019, 10:46 AM
  3. Replies: 4
    Last Post: 04-09-2019, 02:05 PM
  4. Replies: 4
    Last Post: 03-11-2017, 09:48 PM
  5. Replies: 1
    Last Post: 02-23-2012, 02:27 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