Results 1 to 7 of 7
  1. #1
    todmac is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2013
    Posts
    20

    Find most recent measurement through code

    Hello Everyone,


    I have been trying to get a database together to track the most recent measurements of different items, in different positions on heavy equipment.
    I tried to get a query that would do this but couldnt manage it.
    I made an attempt and writing code that could find it once the user updates the unit number, but I cannot get it to work.

    Again I am trying to find the most recent measurement (by date) for a specific unit number, item, and wheel position.
    Any help would be greatly appreciated, I cant seem to get going on this project and need to wrap it up soon.

    Code:
    Private Sub UnitNumber_AfterUpdate()
    Dim UnitNumber As Integer
    Dim db As DAO.Database
    Set db = CurrentDb
    Dim StrSql As String
    Dim LFTreadDepth As Integer
    Dim Item As String
    Dim WheelPosition As String
    Item = "Brake Lining"
    WheelPosition = "Right Front"
    UnitNumber = Me.UnitNumber
    StrSql = "select main.measurement where main.unitnumber = unitnumber and main.item = item and main.Wheel_position= WheelPosition"
    CurrentProject.Connection.Execute StrSql
    End Sub

  2. #2
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Try this:
    Code:
    Private Sub UnitNumber_AfterUpdate()
       On Error GoTo Err_Handler
    
       Dim db As DAO.Database
       Dim r As DAO.Recordset
       Dim StrSql As String
       Dim Item As String
       Dim WheelPosition As String
       Dim UnitNumber As Integer
       Dim LFTreadDepth As Integer
    
       Set db = CurrentDb
    
       Item = "Brake Lining"
       WheelPosition = "Right Front"
       UnitNumber = Me.UnitNumber
    
       StrSql = "SELECT  main.measurement FROM Main "
       StrSql = StrSql & " WHERE main.unitnumber = " & UnitNumber & " AND main.item = '" & Item & "' AND main.Wheel_position = '" & WheelPosition & "'"
       StrSql = StrSql & " ORDER BY [YourDatefield] DESC "
    
       Set r = db.OpenRecordset(StrSql)
    
       If Not r.BOF And Not r.EOF Then
    
          'do something with the measurement field value
    
       Else
          MsgBox "No measurement found for Unit " & UnitNumber & ", " & Item & " and " & WheelPosition
       End If
       '   CurrentProject.Connection.Execute StrSql    '<< only used for action queries, not select queries
    
    Exit_Here:
       'close recordset and clean up
       StrSql.Close
       Set StrSql = Nothing
       Set db = Nothing
    
    Err_Handler:
       MsgBox Err.Description & "  " & Err.Number & " -   " & UnitNumber & ", " & Item & " and " & WheelPosition, vbExclamation, "Error"
       Resume Exit_Here
    
    End Sub

    Change [YourDatefield] to the name of your date field.

    The text fields didn't have required delimiters in the SQL string.
    You didn't say what you wanted to do with the value if found........

  3. #3
    todmac is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2013
    Posts
    20

    Too few parameters?

    Thank you so much for your help!

    I updates the code, but can't seem to get past this error

    Syntax error (missing operator) in query expression ‘main.unitnumber = ‘9400’ AND main.item = ‘Brake Lining’ AND main.Wheel Position= ‘Right Front”.

    Perhaps it's an issue with the quotes? I see the final one is a double quote and the rest are single?

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    What is your actual code, because Steve's wouldn't have resulted in that? Also, is this appropriate to what you want?

    http://www.baldyweb.com/LastValue.htm

    You'd have to adapt it since you would group by more fields.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    todmac is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2013
    Posts
    20

    Still need help

    Quote Originally Posted by pbaldy View Post
    What is your actual code, because Steve's wouldn't have resulted in that? Also, is this appropriate to what you want?

    http://www.baldyweb.com/LastValue.htm

    You'd have to adapt it since you would group by more fields.
    Sorry no, that isnt nearly complex enough for the number of variables I have.

    I copied the code exactly, so not sure.

  6. #6
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    Steve's code didn't put quotes around unitnumber, and did include the closing single quote, so I can't imagine that error came from his code. You can use this to see the completed SQL:

    http://www.baldyweb.com/ImmediateWindow.htm
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    The double quotes define literal string. Concatenate variables. Steve's code shows that.

    Is unitnumber a text type field? If it is, also needs apostrophe delimiters.

    Put [] around any names with spaces or special characters/punctuation (underscore is exception). Does Wheel_Position really have an underscore in it?
    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.

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

Similar Threads

  1. Replies: 1
    Last Post: 06-09-2013, 09:16 AM
  2. Call Logging - Elapsed Measurement of time
    By mdjrmeyer in forum Forms
    Replies: 2
    Last Post: 06-21-2012, 03:37 PM
  3. Replies: 2
    Last Post: 03-02-2012, 09:17 AM
  4. Find primary key name by code
    By MichaelS in forum Access
    Replies: 2
    Last Post: 09-22-2011, 07:09 AM
  5. Ruler measurement
    By Alex Motilal in forum Access
    Replies: 1
    Last Post: 01-07-2010, 09:02 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