Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Form Initialization 2

Status
Not open for further replies.

HerbAndEdnaWeinstein

Technical User
Apr 30, 2003
104
0
0
US
Hi, I'm a beginner designing a form in VBA and I understand how to invoke some code when a certain event happens: simply go to the code view and start a procedure called something like FooButton_Click(). Then, whatever code is under that heading will be executed when FooButton is clicked.

However, I have a series of listboxes that I want populated automatically (based on information from the application) before anything else happens. Thus, there is no "event" I'm waiting for. I tried going to code view and creating a procedure called FooListBox and then invoking the population code, but i get the error that FooListBox already exists (which it does, because that's the name of the list box in the form view.)

Sorry for the verbosity, I just wanted to be clear. Thanks in advance for any help.

Herb Weinstein
 
You were very close in your title. If you go to your form in the VBE and right click, choose View Code
From the right side dropdown box, you will see a list of events for forms - one of which is the initialize event. However, I think you would be better off using the ACTIVATE event to handle this - just because a form is generally only initialized once - after that, it is activated or deactivated

Rgds
Geoff
"Some cause happiness wherever they go; others whenever they go."
-Oscar Wilde
 
Hi Geoff,

I looked for what you said in the right dropdown box. However, neither Activate nor Initialize was one of the options. (Examples of some of those available are Enter, MouseDown, Keypress.)

So, i tried generating a procedure called FooDropDown_Initialize (I tried one called FooDropDown_Activate as well.) Neither one worked; nothing happens when the form is started.

Any other ideas? I'm probably missing something simple.
Herb Weinstein
 
You can put this code in the form's initialize or activate event. But is that what you want, .. the list boxes getting populated when the form is loaded ?
 
That might work too, Shyamala.

Would I do that by adding FooForm_Initialize() to the Code view of the form?

rs
 
You dont have to add the form_initialize to the code.
Just double-click on the form. And you go directly to the Private Sub UserForm_Click. All you have to do is click on the combo box to the right. You can see many events like Click, DblClick, Deactivate etc. Find the "Initialize" and click on it. This itself creates a procedure called :

Private Sub Form_Initialize()
End Sub

Just enter the code to fill up the listboxes before the End Sub Line.(Eg. ListBox1.Additem "Item1",1 ). This code is automatically execured whenever your form is initialized. You dont have to do anything to activate this code.

I hope you can understand what Im trying to say. Im sorry if I was giving too much details you aready know.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top