I have created form dynamically in VBA. The below query will create the form with MouseupEvent.But it doesnt contain any code inside the event. I want to know how to insert VBA code for this particular event. Do we need to use any Modules or Functions or Macro ??
and Event Procedure should be a public function in a module (rather than the module associated with the form). It can probably be a macro instead, but I don't use macros so can't advise whether this is correct or not.
if your event procedure needs to refer to objects within your form then look at using screen.activeform which is the form which triggered the event
Not sure, but the mouseup event also has parameters so you may need to include those in your call (they are button, shift, X and Y)
The easiest way to do this is to create the new form from a template, and make sure the template contains the features you want, including any code, but also controls or other features.
Another slightly more complex, but more flexible solution, is write a custom wrapper or host class that receives a reference to the form, and then that class handles the form's events. Consider the following code in a class module, and note that we declare the form using WithEvents, so we register to handle the form's events . . .
Code:
Private [B]WithEvents[/B] m_frm as Access.Form
Function Load(YourForm as Access.Form)
set m_frm = YourForm
m_frm.AllowAdditions = False
m_frm.AllowDeletions = False
m_frm.AllowEdits = False
m_frm.OnMouseUp = "[Event Procedure]"
Set Load = Me
End Function
Private Sub m_frm_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
[COLOR="Green"] 'this code runs on the MouseUp event of YourForm[/COLOR]
End Sub
So you declare a WithEvents variable, and take initialization steps in a Load() method, and then your form's events can be raised and handled in the custom host or wrapper class, not in the form itself. This class could even create the form, so you essentially invert the solution, where you write code that contains a form rather than write a form the contains code.
Give me a bit, and I'll write a simple database example.
My mistake. CreateFrom does not copy controls or code from the template form to the new form. To create that kind of behaviour you can use DoCmd.CopyObject instead.
This class could even create the form, so you essentially invert the solution, where you write code that contains a form rather than write a form the contains code
Just out of curiosity, can you still create a form if the db is a mde/accde?
I have a routine which does something similar (mine has code which assigns functions to events after the form is loaded as outlined in my post - the hasmodule property is set to false), but the form needs to exist with its controls prior to making the mde/accde.
Appreciate the code could be in an mde as a library file and run from an mdb to create a form in the mdb
I think there are far too many errors and assumptions in post #1 that we should be asking questions and not trying to answer the specific question asked in post #1.
1. Where is the query in post #1? I mean that a person wanting to create Forms and code on the fly should know the difference between a query and code. If it is simply a typo then why was the typo not edited to fix it?
2. frm.OnMouseUp = "[Event Procedure]" does not create a Mouse Up event at all. All is does is to put [Event Procedure] in the property sheet, that’s it. The code stub is not created until the ellipsis, at the end of the property line, is pressed. So it is not correct to say that you have an empty Mouse Up stub, you do not even have the stub.
I say all this to try and save you a lot of time and effort. Many people have tried this in the past only to find out the hard way that it doesn’t work as intended.
It was not intended to allow people to create Forms and code at runtime in a production environment. The intention was to allow people to create wizards but they were not intended to run in a production environment, they were intended to run at design time.
The big difference is this. You can do a lot of work, and consume a lot of time of others in the process, only to find it will not work in an MDE or ADE file format. Those file formats can not go into design mode and so will fail when you try to run it. You will forever force the use of your database in MDB format.
I would suggest you give this whole project some serious thought before wasting your time, and maybe the time of others, going in this direction.
Thank you much for spending your valuable time to solve my queries. Chriso you are absolutely correct. Anyway the above methods are not at all working. my access db is file ACCDB, not MDE,ACCDE. I have accomplished this issue using the Macro. Now my acccess form working as i expected. Thanks guys..