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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

create control with an event procedure? 1

Status
Not open for further replies.

lanelouna

Programmer
Dec 19, 2002
71
GB
Hello all
I am trying to create a form dynamically, based on a selection of certain parameters in the first form
if the user chooses 2 values in a list box for example in the first form he will have a 2nd form with box entries for those two calues, each with an optiongroup, that do certain events when selected
so the code is similar to :

for j = 1 to numberOfClasses
Set ctlOptionGroupRATIO = CreateControl(frm.Name, acOptionGroup, , "", "", intgroupX, intgroupY)
With ctlOptionGroupRATIO
.Name = "Group_" & RATIOClasses(j)
.DefaultValue = 1
.BorderStyle = 1
.Height = 200
.Width = 2000
.OnClick = "[Event Procedure]"
End With
Next j

my pb is the following: how can u let the On Click point to the function in form module?
, i don t know how to deal with the event procedure, where should i define it?and should i make it dynamic, that is its name shoulddepend of class selected
for example
Private Sub Group_D_AfterUpdate()
bla bla bla
End Sub
and well can a function do the same job, instead of an event procedure?
if you can help me i would be very greatfull


 
It is possible to insert code into a form module, but it won't work in your case. The problem is that whenever you make a change to a code module, VBA resets the entire project, which makes any variables and objects you've got revert to zero/empty string/Nothing. The built-in support for building module code will only work if the module you're building is in another project from the module that's building it. In effect, that means it only works from a library, wizard, or add-in.

But you can probably get around this by pre-writing all possible event procedures, and then associating the appropriate one with each option group or control you build. You could make the association in two ways:

1. Each control has an EventProcPrefix property. When Access raises an event, it looks at the On<Event> property. If the property contains &quot;[Event Procedure]&quot;, Access concatenates the EventProcPrefix property with &quot;_&quot; and the name of the event, and then tries to call a procedure by that name. You could thus set the EventProcPrefix property to select one of several <prefix>_AfterUpdate() procedures you've already written in the form module.

2. Alternatively, if the On<Event> property contains a string starting with &quot;=&quot; and ending with &quot;()&quot;, Access calls the specified function, which must be defined in a standard module. In this case, however, your procedure won't receive any parameters that would be passed to the event procedure. You can still cancel the event by calling CancelEvent. Rick Sprague
Want the best answers? See faq181-2886
To write a program from scratch, first create the universe. - Paraphrased from Albert Einstein
 
hello Rick
thanks for your answer
i can see where you are getting at
myquestion is
if i do write several event procedures in the form module called form_Form1, such as
Private Sub Group_D_AfterUpdate()
bla bla bla
End Sub
Private Sub Group_B_AfterUpdate()
bla bla bla
End Sub

would they be deleted, if i am doing the following:
each time a user semects certain classes
the exisitng form1 is deleted
then a new form callef form1 is created (it is opened in design view)
Set frm = CreateForm
With frm
.....
end with
then saved
then closed
then opened it in normal mode, ready to be used

thank you in advance


 
That's a problem. If you're creating the form from scratch, any attempt to add code to its module will stop you dead.

I was talking about creating a &quot;template&quot; form containing any invariant controls plus all the code. You can execute a DoCmd.CopyObject to make a temporary version to customize, then open the temporary version and make your changes. It would be something like this:
Code:
    Dim strNewName As String, frm As Form

    strNewName = &quot;Temporary Form&quot;
    DoCmd.CopyObject , strNewName, acForm, &quot;Template Form&quot;
    DoCmd.OpenForm strNewName, acDesign
    Set frm = Forms(strNewName)
The rest of your code would be unchanged. Rick Sprague
Want the best answers? See faq181-2886
To write a program from scratch, first create the universe. - Paraphrased from Albert Einstein
 
thanks Rick
i will try to do like you said and will tell you if it worked
i hope it will
thanks again
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top