Programmatically assign VBA code to MouseupEvent in access? (1 Viewer)

gvenkat

New member
Local time
Today, 04:52
Joined
Mar 22, 2014
Messages
4
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 ??

Set frm = CreateForm()
frm.AllowAdditions = False
frm.AllowDeletions = False
frm.AllowEdits = False
frm.OnMouseUp = "[Event Procedure]"

Waiting for your valuable response...
 

CJ_London

Super Moderator
Staff member
Local time
Today, 00:22
Joined
Feb 19, 2013
Messages
16,618
your code should be

frm.OnMouseUp = "=[Event Procedure]"

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)
 

MarkK

bit cruncher
Local time
Yesterday, 16:22
Joined
Mar 17, 2004
Messages
8,181
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.
 

pbaldy

Wino Moderator
Staff member
Local time
Yesterday, 16:22
Joined
Aug 30, 2003
Messages
36,126
FYI, moved your thread from the introductions forum.
 

MarkK

bit cruncher
Local time
Yesterday, 16:22
Joined
Mar 17, 2004
Messages
8,181
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.
 

MarkK

bit cruncher
Local time
Yesterday, 16:22
Joined
Mar 17, 2004
Messages
8,181
Here's an example of how to handle form events in a custom class.
 

Attachments

  • FormWrapper.mdb
    256 KB · Views: 143

CJ_London

Super Moderator
Staff member
Local time
Today, 00:22
Joined
Feb 19, 2013
Messages
16,618
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
 

ChrisO

Registered User.
Local time
Today, 09:22
Joined
Apr 30, 2003
Messages
3,202
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.

Chris.
 

ChrisO

Registered User.
Local time
Today, 09:22
Joined
Apr 30, 2003
Messages
3,202
19th. November 2005: Post #17 here:-
http://www.access-programmers.co.uk/forums/showthread.php?t=97434&page=2

The actual file was written on the 8th. July 2002 in Access 97 file format but attached is an Access2003 version (A2000 file format)

The point being is that we don’t want to write something like that only to find out it will not convert to an MDE file.

Chris.
 

Attachments

  • db5.mdb
    176 KB · Views: 128

gvenkat

New member
Local time
Today, 04:52
Joined
Mar 22, 2014
Messages
4
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..
 
Last edited:

Users who are viewing this thread

Top Bottom