Results 1 to 3 of 3
  1. #1
    gbmatay1 is offline Novice
    Windows 7 32bit Access 2013 32bit
    Join Date
    Apr 2016
    Posts
    1

    Problem getting value of current record using me.variable

    Access 2013

    I'm having problems picking up the value of a variable in the current record displayed on a form

    I'm using the following code picked up from Allen Brown's Website:

    Dim rs As DAO.Recordset

    If Not IsNull(Me.CustomerID) Then
    Set rs = CurrentDb().OpenRecordset("tblSys", dbOpenDynaset)
    With rs
    .FindFirst "[Variable] = 'CustomerIDLast'"
    If .NoMatch Then
    .AddNew 'Create the entry if not found.
    ![Variable] = "CustomerIDLast"
    ![Value] = Me.CustomerID
    ![Description] = "Last customerID, for form " & Me.Name
    .Update
    Else
    .Edit 'Save the current record's primary key.
    ![Value] = Me.CustomerID
    .Update
    End If
    End With


    rs.Close
    End If
    Set rs = Nothing

    Whichever record select through my form I get the value of CustomerID from the first record in the table not the currently selected record.


    Thanks

  2. #2
    NoellaG's Avatar
    NoellaG is offline VIP
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Jun 2010
    Location
    Belgium
    Posts
    1,175
    Hi,

    you open a new independant recordset, containing all records in the table. One solution to do this: get the id from the current record on the form like lngID = me.[ID] and then pass this trrough to your code as a parameter and than filter your recordset on "..... where ID = " & lngID
    Other solution: work with a recordset clone and bookmarks.

    kind regards
    Noëlla

  3. #3
    ssanfu is offline Master of Nothing
    Windows XP Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    You have field names: "Variable", "Value" and "Description". These are reserved words in Access and shouldn't be used for object names.

    This line is incorrect
    Code:
    Set rs = CurrentDb().OpenRecordset("tblSys", dbOpenDynaset)
    Should not have the red parenthesis.
    This line is correct
    Code:
    Set rs = CurrentDb.OpenRecordset("tblSys", dbOpenDynaset)
    Code:
    .FindFirst "[Variable] = 'CustomerIDLast'"
    This line says "Find the string 'CustomerIDLast' in the field [Variable] in the recordset rs".
    It looks like you will always find the same record unless you have some way of changing the record with the value of "CustomerIDLast" in the field [Variable] to a different value.
    .

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

Similar Threads

  1. Replies: 1
    Last Post: 05-18-2015, 01:27 AM
  2. Replies: 3
    Last Post: 02-06-2015, 01:18 PM
  3. Replies: 16
    Last Post: 02-06-2013, 03:06 PM
  4. Variable substitution problem.
    By bsc in forum Programming
    Replies: 2
    Last Post: 01-11-2012, 09:33 AM
  5. Replies: 4
    Last Post: 08-05-2010, 01:26 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