Consulting

Results 1 to 7 of 7

Thread: Solved: form load event not working

  1. #1

    Solved: form load event not working

    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:

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

    [vba]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[/vba]
    I have been trying this in the code section for the Conclusion form, but it's not working...the text box is just empty:

    [vba]
    Private Sub Conclusion_Load()
    FlowRateBox = FlowRate
    End Sub[/vba]
    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

    [vba]Private Sub Conclusion_Load()

    MsgBox "Hello"

    End Sub[/vba]

    the message box doesn't pop up. Is there some trick to getting the load event to work?
    Last edited by franzkafka; 12-06-2008 at 07:56 PM.

  2. #2
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    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

  3. #3
    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.

  4. #4
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    Try to avoid public variables.

    In this method, notice how I displayed the 2nd userform before closing userform1.
    [VBA]'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[/VBA]

  5. #5
    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?

    [VBA]
    'userform1
    Private Sub UserForm1_Initialize()
    MsgBox "Hello"
    End Sub
    [/VBA]

  6. #6
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    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.

  7. #7
    Quote Originally Posted by Kenneth Hobs
    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.

Posting Permissions

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