Replace OnClick Macro with VBA Code [solved]
Hello there!
I have the following problem:
I create a form with VBA (because the fields in the form are different each time the form is opened).
Also I create a control (Combobox), in which the user can select a socalled Template. After selecting a value in the combobox some fields in the form shall be filled (=execute a codesequence after raising the OnClick event).
At the moment i use this:
VB Code:
ctrl.Properties("Name") = "ComboSelectTemplate"
ctrl.Properties("OnClick") = "MacroComboSelectTemplate.OnClick"
ctrl.Properties("RowSourceType") = "Table/Query"
ctrl.Properties("RowSource") = "SELECT [" & Templates & "].[TemplateName] FROM [" & Templates & "]"
I have created a macro "MacroComboSelectTemplate" that then executes a VBA procedure.
Now I want to remove the macro-workaround and replace it by VBA code.
Anyone has an idea?
Re: Replace OnClick Macro with VBA Code
If you go to the control in design view and click into the OnClick property, you can select to either use the
custom code builder, expression builder or assign a macro to it. I think that through code you can do it by setting its
property to "[Event Procedure]". I know if you do it in design view it takes you to a module of a procedure with the
same control name _Click, but I am unsure how it links the two if the name is different from the control name.
Any possibility of just doing it through the design window?
HTH
Re: Replace OnClick Macro with VBA Code
Thanks for your ideas!
As I create the form completely in vba code there is no design view.
Also setting the OnClick event to [Event Procedure] unfortunately does not work, as Access then tries to run the macro "Event Procedure" :(
Re: Replace OnClick Macro with VBA Code
Solved it on my own; there is a function for creating procedures in modules. So I needed first to get the form's module:
VB Code:
dim mdl as module
dim result as integer
set mdl = frm.module
result = mdl.createEventProc("Click", ctrl.Name)
mdl.InsertLines result + 1, vbTab & "Msgbox ""My Code here."""
Re: Replace OnClick Macro with VBA Code [solved]
Sorry, my bad. I guess it didnt register with me on how you needed it done.
Checkout my code in CodeBank on Modifying Macro Code from VB.
Later