Ivan Navratil
Ivan Navratil

Reputation: 13

Keep textbox value when changing record

I have one form where I can view bought items from customer, but when I want to put new record in that form owner_id (on my language ID_vlasnika) is set to null and I want to automatically put owner_id on same value as in record before.

enter image description here

enter image description here

Upvotes: 1

Views: 768

Answers (2)

MoondogsMaDawg
MoondogsMaDawg

Reputation: 1714

Someone else may have a more elegant fix, but here's something that will work:

As this looks like a bound form, any time you click the new record navigation button it will clear all fields. As far as I know, there aren't ways to change this behavior.

However, You can hide the navigation buttons from the form's property pane and instead use a command button to "Create New Record". Then have a macro or VBA run some actions to capture the current record values, create a new record, and then copy the values back to your controls.

Here is an example of the VBA code:

Dim a As String
'Declare as many dimensions as fields you want to copy

a = Me![Naziv uredaja]
'Set variables for all of your other fields

'Next line creates a new record on your form
DoCmd.GoToRecord , , acNewRec

'Set your field back to the previous value
Me![Naziv uredaja] = a
'Repeat for each field you want to copy.

End Sub

Upvotes: 1

Gustav
Gustav

Reputation: 56026

There is "a more elegant fix" - set the DefaultValue of the field in the OnCurrent event:

Private Sub Form_Current

    If Not Me.NewRecord = True Then
        Me!ID_vlasnika.DefaultValue = Me!ID_vlasnika.Value
    End If

End Sub

Upvotes: 2

Related Questions