PDA

View Full Version : Solved: form load event not working



franzkafka
12-06-2008, 07:44 PM
Hello,

I need to populate a text box once the form it is on loads. Its form loads when the user clicks a button on a different form. I want to populate it with a global variable (a double).

The program calculates a flow rate. The user enters some parameters on one form (VariableEntry), clicks "calculate", which gives the variable FlowRate a value and loads the next form (Conclusion). I want that form to contain a text box (FlowRateBox) with the variable FlowRate displayed.

In a module, I have:

Global FlowRate As Double
In the code thing for the first form (VariableEntry), I have:

Private Sub Calculate_Click()
If Not ReynoldsNumberBox.Text = "" And Not PressureBox.Text = "" And Not DiameterBox.Text = "" And Not ViscosityBox.Text = "" And Not PipeLengthBox.Text = "" Then
P = Val(PressureBox.Text)
D = Val(DiameterBox.Text)
V = Val(ViscosityBox.Text)
L = Val(PipeLengthBox.Text)
Const PI As Double = 3.14159265358979
FlowRate = (PI * P * D ^ 4) / (128 * V * L)
Unload VariableEntry
Conclusion.Show
Else
MsgBox "All fields must contain data before flow rate can be calculated"
End If
End Sub
I have been trying this in the code section for the Conclusion form, but it's not working...the text box is just empty:


Private Sub Conclusion_Load()
FlowRateBox = FlowRate
End Sub
I hope I've provided enough information/been clear enough. Thanks. =)

edit:

I think the problem is with the Load() event. Even if I do something like

Private Sub Conclusion_Load()

MsgBox "Hello"

End Sub

the message box doesn't pop up. Is there some trick to getting the load event to work?

GTO
12-06-2008, 08:09 PM
Greetings,

I may well be speaking out of turn, please forgive if this is the case...

Your code: is it in Excel, and if so, what year?

If yes, and after (I think?) 95, then try: declare FlowRate as Public. Also, instead of Private Sub Conclusion_Load(), try the form's initialize event, like Private Sub UserForm_Initialize().

Hope this helps,

Mark

franzkafka
12-06-2008, 08:23 PM
I am working with VBA 6.5 in Excel 2007. My code is in VBA but it is for a button in Excel...perhaps Excel wasn't the right forum for this question since it's more of a strictly VBA thing.

I tried changing FlowRate to Public and using Initialize(), it didn't work. I haven't been able to get Activate() to work either. But like I said, I think the problem may be with how I am using the event, because even if I try to get a message box to show up when the form loads (or initializes or activates), it doesn't.

Kenneth Hobs
12-06-2008, 08:25 PM
Try to avoid public variables.

In this method, notice how I displayed the 2nd userform before closing userform1.
'userform1
Private Sub UserForm_Initialize()
TextBox1.Value = 1
End Sub

'userform1
Private Sub CommandButton1_Click()
UserForm2.Show
Unload Me
End Sub

'userform2
Private Sub UserForm_Initialize()
TextBox1.Value = UserForm1.TextBox1.Value + 1
End Sub

'userform2
Private Sub CommandButton1_Click()
Unload Me
End Sub

franzkafka
12-06-2008, 09:19 PM
Kenneth, that code looks good, but something is just not working for me. If I do something like the following code, no message box shows up. I place this in the UserForm1 code area, and yet no message box pops up when the form comes up. Any idea why the event wouldn't work?


'userform1
Private Sub UserForm1_Initialize()
MsgBox "Hello"
End Sub

Kenneth Hobs
12-06-2008, 09:46 PM
The suffix 1 in your UserForm1 is the problem.

All event code for Userforms have Sub names as UserForm. It is why I commented before each to show you which one I was demonstrating.

The Userform Initialize event and CommandButton's Click event Subs go in separate Userforms as I commented.

Tip: Doubleclick a blank space in the Userform to paste or write the code for each.

franzkafka
12-06-2008, 10:43 PM
The suffix 1 in your UserForm1 is the problem.

All event code for Userforms have Sub names as UserForm. It is why I commented before each to show you which one I was demonstrating.

The Userform Initialize event and CommandButton's Click event Subs go in separate Userforms as I commented.

Tip: Doubleclick a blank space in the Userform to paste or write the code for each.

Kenneth, this works great! Thanks so much for your help, I really appreciate it. I think I'll also go through and try to avoid using Public variables. :beerchug: