Results 1 to 13 of 13
  1. #1
    Thermalmonster is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Jul 2014
    Posts
    18

    Recall previous values on a form

    Hi



    I have a form which I want to recall the value of the previous record when a new one is made.
    the code im using is fine until access or the form is closed down.
    on reopening it does not display the previous record
    There are 30 fields to recall so i dont want to retype them
    Any assistance apperciated

    Cheers

    Heres my code
    Private Sub CD1_AfterUpdate()

    If Not IsNull(Me.CD1.Value) Then
    CD1.DefaultValue = Me.CD1.Value
    End If
    End Sub

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    Code in the form Open or Load or Current event could retrieve the last record in a recordset object and loop through the recordset fields and set the DefaultValue property of each control.
    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.

  3. #3
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    You have the right idea, but your syntax is a little off. It should be

    Code:
    Private Sub CD1_AfterUpdate()
       Me.CD1.DefaultValue = """" & Me.CD1.Value & """"
    End Sub

    This syntax is valid for Text, Number, DateTime and Boolean Datatypes.

    Linq ;0)>
    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

  4. #4
    Thermalmonster is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Jul 2014
    Posts
    18
    Many thanks I will try that Syntax

  5. #5
    Thermalmonster is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Jul 2014
    Posts
    18
    Missingling,

    Your syntax still does not solve the problem of recalling the record once the form has been shut down?

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    See post 2 for methodology. Do you know about recordsets in VBA?
    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.

  7. #7
    Thermalmonster is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Jul 2014
    Posts
    18
    Quote Originally Posted by June7 View Post
    See post 2 for methodology. Do you know about recordsets in VBA?

    No Im only just beginning my Access & Code journey :-(

  8. #8
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    Review http://allenbrowne.com/ser-29.html

    Dim rst As Recordset
    Set rst = CurrentDb.OpenRecordset("SELECT * FROM MyTable WHERE ID IN (SELECT Max(ID) FROM MyTable);")
    Me.CD1.DefaultValue = "'" & rst!CD1 & "'"
    Me.AnotherControlName.DefaultValue = "'" & rst!AnotherFieldName & "'"

    Continue the code for each control you want to set. Using ID in the query assumes latest record will have the maximum ID. Use whatever field from your table that is the equivalent to the unique ID.
    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.

  9. #9
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    Sorry, missed the part about closing then re-opening the form! In this case June7's approach is the right one, assuming that you have an ID field that is always incremented with each new record. A date/time created field would be even better! ID fields are frequently made using Autonumbers, and these can get out of order, at times. Autonumbers should only be used as unique identifiers, and even this can be problematic, at times.

    Linq ;0)>
    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

  10. #10
    Thermalmonster is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Jul 2014
    Posts
    18
    Thanks Folks I'll Give that a whirl this morning

  11. #11
    Thermalmonster is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Jul 2014
    Posts
    18
    If My Table is called TUNNEL INFO 100

    Should the code read

    Dim rst As Recordset
    Set rst = CurrentDb.OpenRecordset("SELECT *TUNNEL INFO 100 (SELECT Max(ID) TUNNEL INFO 100);")
    Me.CD1.DefaultValue = "'" & rst!CD1 & "'"
    Me.CD2.DefaultValue = "'" & rst!CD2 & "'"


    or was FROM part of the code?

    Also does this code sit in the After event location ?

    Many Thanks

  12. #12
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    Set rst = CurrentDb.OpenRecordset("SELECT * FROM [TUNNEL INFO 100] WHERE ID IN (SELECT Max(ID) AS MaxID FROM [TUNNEL INFO 100]);")

    Names with spaces must be enclosed in []. Avoid spaces and special characters/punctuation (underscore is exception) in naming convention.

    What is the name of the unique identifier field in the table? I use ID as an example.

    As noted in post 2, use form Open or Load or Current event
    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.

  13. #13
    Thermalmonster is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Jul 2014
    Posts
    18
    Worked a treat thank you

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

Similar Threads

  1. Replies: 3
    Last Post: 07-25-2013, 11:35 PM
  2. Replies: 3
    Last Post: 07-03-2013, 10:38 AM
  3. Replies: 8
    Last Post: 08-02-2012, 08:48 AM
  4. Replies: 1
    Last Post: 04-09-2012, 02:14 PM
  5. Replies: 1
    Last Post: 03-27-2010, 06:13 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