idunnowhatimdoin
idunnowhatimdoin

Reputation: 59

Access 2013 - Save Value from Textbox and display again when form is opened after close

I want to save a value from textbox in a string for example and display it again when the form get's openend.

I have two textboxes PriceRangeOne and PriceRangeTwo .. The user enter here for example 20 and 40

The problem i have is that when the user switches between Form and Report the values in this textboxes are beeing deleted. How can i save them?

I tried adding a sourcecontrol to the fields but had name errors eventhough i used different names.

I tried adding this to on change and retrieve it in an onload

Dim eingabe As String = textBox1.Text or .Value

Still didn't worked. Does anyone know a way to do this?

Upvotes: 2

Views: 3379

Answers (3)

ivan_pozdeev
ivan_pozdeev

Reputation: 35998

Courtesy of How to save the last value of a textbox. | Access World Forums:

Private Sub Form_Close()
'Set the default value for textbox
DoCmd.SetWarnings False
DoCmd.RunSQL "UPDATE table SET table.field = [Forms]![FormName]![Textbox] " & vbCrLf & _
"WHERE (((table.ID)=1));"
DoCmd.SetWarnings True

End Sub

Private Sub Form_Load()
'Load the default value for textbox
Me.Textbox.Value = DLookup("[field]", "[table]", "[ID]=1")
End Sub

Upvotes: 0

idunnowhatimdoin
idunnowhatimdoin

Reputation: 59

Solved it with variables.

I declared global variables in my standart module

For example

Public PriceOne As Double
Public PriceTwo As Double

Than i did this in my form in Close() and Open():

Private Sub Form_Close()
PriceOne = Me.Field
PriceTwo = Me.FieldTwo
End Sub

Private Sub Form_Open(Cancel As Integer)
Me.Field = PriceOne
Me.FieldTwo = PriceTwo
End Sub

Works perfect!

Upvotes: 1

Johnny Bones
Johnny Bones

Reputation: 8402

Typically, the most efficient and reliable way to do this is to have some form auto-open when the database is opened. It could be a dashboard, or just some form with nothing else on it. Whatever you use, launch it when the database opens and then minimize it. Now you have a form that's always open, as long as the application is open. Add a couple of textboxes to this form/dashboard.

When you close your form referenced in this question, write the values of PriceRangeOne and PriceRangeTwo to the textboxes on the form I described above. Then, when you open a new form or report, you can reference the values in those textboxes. Since the form is always open, you can reference these values at any time from any form or report until you close your database.

Upvotes: 1

Related Questions