How to update/refresh a textbox?

I have a small problem.
On the first form I have coded some arithmetical operations. When the operations are done you can open the results form with a cmdButton. You can go back and change some inputvalues on the first form and make new arithmetical operations, but when you open the results form again, the textboxes haven't been updated.

In the main form the results are stored in different public variables. In the results form the textboxes are set like this:

txtSupportingForce.txt = varSF

It works like it should the first time but when I make new calculations the textboxes have the same values as the first time.

Is there some code to write in the form_load function to get the textboxes updated?
Who is Participating?
EnladeConnect With a Mentor Commented:

Opps, that shouldn't be Me.Unload but rather:

Unload Me

Sorry about that.
HLEAAuthor Commented:
Oops, it's really spelled:            txtSupportingForce.Text = varSF
I suppose you have 2 options:
1. If the 2nd form is valid while you calculate...
    update the 2nd form's textbox when the calculation ends with something like...Form2.Text1.Text = varResult.

2. Declare a public variable/private variable+public function in the 1st form and when you open the 2nd form_load call that function/variable to retreive the result.

Text1.Text = Form1.varResult     or
Text1.Text  = Form1.GetResult()

Public varResult as Variant    
--------------------------------  or
Private varResult as Variant
Public Function GetResult() as Variant
    GetResult = varResult
End Function
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Richie_SimonettiIT OperationsCommented:
shouldn't you use:
hi HLEA, it is not nessesary to update any textbox, but try this

in the command button to load the result form use this at the beginning:

Private Sub Command1_Click()
' result form is the form called when the cmdButton is pressed
Unload ResultForm
Load ResultForm
ResultForm.txtSupportingForce.Text = varSF 'make sure VERY sure varSF has the right value
End Sub

it's a forced way to reset all in the form


I think what you have done is to put the update to your results form in the form load event.  However, you probably did not unload the form after the user closed it.  You probably just hide the form.  Then when you call show form for the second time since the form is already loaded it will not exicute the form load event.  Now, I'm not exactly sure what would be the best solution for what you are doing, but just to get it working for you make sure that you do a Me.Unload on the exit of the results form.  Then when you do the "frmResults.Show Me, Modal" or whatever it will cause the load event to be exicuted and thus update your text boxes properly.


Note that there are other ways to manage secondary forms.  For instance, you could create a public FormSetup function (call it whatever you want) and then call that function deliberatly from the main form.  For instance, lets say your results form is called frmResults then you would do something like this on the click event of your results button on your main form.

Load frmResults
frmResults.Show Me, vbModal

Even though you call the load function above that does not mean that the load event of frmResults is called.  The load event only occurs if the form is actually loaded.  If instead the form is already loaded it will not initiate the load event.  Still, if you move you load event code over to a seperate public function (say FormSetup) then you will be assured that the code is exicuted because you call the FormSetup function deliberately prior to the Show function.

Like I said above you can avoid all this and just make sure that you unload the form properly before you try to show it again, but this is just another way to deal with a loaded form.  Hope that helps.
or do it my way i anint good but i will say this is prob a long way lol

use a timer

i use loads of them lol

set the timer to what eva you want and you well away

Private Sub Timer1_Timer()
txtSupportingForce.Text = varSF
Timer1.Enabled = False
End Sub
HLEAAuthor Commented:
Thanks to you all!
Enlade, I'm giving you the points because Unload Me did it. The shortest and easiest way is best way for a beginner like me.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.