Results 1 to 7 of 7
  1. #1
    Join Date
    Nov 2003
    Posts
    12

    Unanswered: remember last value

    Hi
    I wont my form to remember the last value every time when I add new reccord
    In the form I do query that give me value in the last reccord
    But when I wont add new reccord the querry not work
    help pls

  2. #2
    Join Date
    Nov 2003
    Location
    LONDON
    Posts
    238

    Re: remember last value

    Originally posted by stanimir_p
    Hi
    I wont my form to remember the last value every time when I add new reccord
    In the form I do query that give me value in the last reccord
    But when I wont add new reccord the querry not work
    help pls
    Use the defaultvalue property of each control. In the afterupdate event copy the value entered to the defaultvalue property. The new record will use the default values

  3. #3
    Join Date
    Nov 2003
    Posts
    12
    Mey I have to create Event procedure on After Update event
    what I must write in

    Me![date].DefaultValue = Me![date] ?????

    and How to see the last reccord when I open form on first time

  4. #4
    Join Date
    Nov 2003
    Location
    LONDON
    Posts
    238
    Originally posted by stanimir_p
    Mey I have to create Event procedure on After Update event
    what I must write in

    Me![date].DefaultValue = Me![date] ?????

    and How to see the last reccord when I open form on first time
    The code you have written is correct. You would need to write this for each control that you want to save its value. if you have a lot of controls you could use a function instead that would look like this:

    Dim ctl As Access.Control
    ' You will need some error code as not every control has a defaultvalue property.
    On Error Resume Next
    For Each ctl In Me.Controls
    ctl.DefaultValue = ctl.Value
    Next ctl

    You can call this code from every controls AfterUpdate event on the form.

    To see the last record when you open the form, you should use the ORDER BY clause to sort in descending order the column that will define what the last record is.

    If you meant how to get the last records values and place those as the default value for the new record when you open the form you will have to use VBA and a recordset. In VBA you would load into a recordset the last record entered into your table (however you define the last record).
    You could then use another loop to search for a control with the same control source as the field name in the recordset. So for example:

    Dim ctl As Access.Control
    Dim fld As ADODB.Field
    Dim rst As ADODB.Recordset

    Set rst = (My recordset)
    For Each fld In rst.Fields
    For Each ctl In Me.Controls
    If ctl.ControlSource = fld.Name Then ctl.DefaultValue = fld.Value
    Next ctl
    Next fld

  5. #5
    Join Date
    Nov 2003
    Posts
    12
    OK
    My field's name is date, my form's name is ttt
    That form I use to enter data in database
    I have a command buttons - add record, delete record and exit

    I have two choices

    1. I tryed that
    on After Update I have event procedure with only 1 row

    Me![date].DefaultValue=Me![date]

    but this give me #Name? error
    I try
    Me![date].DefaultValue=Me![date].value ;
    Me.date.DefaultValue=Me.date.value and others but no result

    The seccond choice - to remember the last record in recordset

    on Click event(event procedure) on Command buton "Add Record" I try

    On Error GoTo Err_Add_Record_Click
    Dim ctl As Access.Control
    Dim fld As ADODB.Field
    Dim rst As ADODB.Recordset
    DoCmd.GoToRecord , , acNewRec is this place correct
    Set rst = ttt.recordset ???
    For Each fld In rst.Fields
    For Each ctl In Me.Controls
    If ctl.ControlSource = fld.Name Then ctl.DefaultValue = fld.Value
    Next ctl
    Next fld
    Exit_Add_Record_Click:

    in result - object required or type mitcmatch or form sleeps when I clic
    Add record buton

  6. #6
    Join Date
    Nov 2003
    Posts
    12
    Me![date].DefaultValue=Me![date] is good when the data is number or string like 234234 (only numbers)
    when the value is date it has to convert whit CvDate and Cdate
    but when the string is a car number like "gh 456 tsy"
    it thasn't work

  7. #7
    Join Date
    Nov 2003
    Posts
    12
    wheh value is string
    default value can set - me.field.defaultvalue = """st7r987""" - why 3 " ?
    but default value can't set
    me.field.defaultvalue=me.field.value - it not works why?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •