How to add a module to a form when you use the create form method

abowden

New member
Local time
Today, 05:21
Joined
Aug 23, 2021
Messages
4
i am trying to add an module to a form when i press a button

here is my code so far

Dim db As DAO.Database
Dim frm As Form







Dim mdl As Module



Dim lngReturn As Long


Dim ctr1 As Control
Dim ctr2 As Control



Dim frmResize As New clsFormResize
Set frmResize = New clsFormResize


Set db = CurrentDb
Set frm = Application.CreateForm







RunCommand acCmdFormHdrFtr



frm.Section(acHeader).BackColor = RGB(84, 246, 225)
frm.Section(acFooter).BackColor = RGB(84, 246, 225)









Set mdl = frm.Module

lngReturn = mdl.CreateEventProc("Resize", ("Form"))
mdl.InsertLines lngReturn + 2, vbTab & "'Place Holder"

With mdl




Set mdl = frm.Module
lngReturn = mdl.CreateEventProc("Load", ("Form"))
mdl.InsertLines lngReturn + 2, vbTab & "frmResize.initme.Form1"


End With




frm.RecordSelectors = False
frm.NavigationButtons = False
frm.Moveable = True
frm.Caption = "Test Form:"
frm.PopUp = True




'Create Control Buttons

Set ctr1 = CreateControl(frm.Name, acCommandButton, acFooter, , "newbutton", 100, 100, 1000, 600)

With ctr1

ctr1.Name = "cmdclose"
.Tag = "MoveY"


Set mdl = frm.Module
lngReturn = mdl.CreateEventProc("Click", ctr1.Name)
mdl.InsertLines lngReturn + 1, vbTab & "DoCmd.close , Form1, acSaveNo"



ctr1.Caption = "&Close:"


End With

Set ctr2 = CreateControl(frm.Name, acCommandButton, acFooter, , "newbutton", 1100, 100, 1000, 600)

With ctr2

ctr2.Name = "cmdhelp"
.Tag = "GrowX0.5 MoveY"






Set mdl = frm.Module
lngReturn = mdl.CreateEventProc("Click", ctr2.Name)
mdl.InsertLines lngReturn + 1, vbTab & "DoCmd.MoveSize Me.Form.WindowLeft * 0.9, Me.Form.WindowTop * 0.9, Me.Form.WindowWidth * 1.1, Me.Form.WindowHeight * 1.1"



ctr2.Caption = "&Help:"


End With



DoCmd.OpenForm "Form1", acNormal




End Sub


please help
thanks andy
 
Welcome to the forums! We are the most active Microsoft Access community on the internet by far, with posts going back over 20 years!

To get started, I highly recommend you read the post below. It contains important information for all new users to this forum.

https://www.access-programmers.co.uk/forums/threads/new-member-read-me-first.223250/

We look forward to having you around here, learning stuff and having fun!
 
Hi. Welcome to AWF!

Just FYI, I moved your thread out of the Introduction forum.
 
To create a class module for a form that doesn't have one, find the form's properties and set "HasModule" to TRUE. It will create an empty class module for the form. Then you can populate it to your heart's content.
 
Or just create an event procedure from the properties sheet. The form's class module is created automatically in order to include the event procedure created.
 
Please tell me you are doing this to help with YOUR development tasks and not to put into a production database to be used by others.
 
thanks pat
yes not being sold for any commercial purposes

thanks andt
 
OK FOUND A SOULTION HERE IT IS

Dim db As DAO.Database
Dim frm As Form

Dim mdl As Module


Dim strFormOpenCode As String

Set db = CurrentDb
Set frm = Application.CreateForm

Set mdl = Forms!Form1.Module

strFormOpenCode = "Option Explicit" & vbCrLf & "Dim frmResize As New clsFormResize" & vbCrLf & vbCrLf

With mdl
.InsertText strFormOpenCode

End With

THANKS ALL
 
Thanks for providing the solution though it would be appreciated if you used the code tags as below (</> button on the post toolbar)

Code:
Dim db As DAO.Database
Dim frm As Form
Dim mdl As Module
Dim strFormOpenCode As String

Set db = CurrentDb
Set frm = Application.CreateForm
Set mdl = Forms!Form1.Module

strFormOpenCode = "Option Explicit" & vbCrLf & "Dim frmResize As New clsFormResize" & vbCrLf & vbCrLf

With mdl
     .InsertText strFormOpenCode
End With

Anyway, I agree that will work, but can I ask what the point is.
Why exactly do you want to create a form & its module in code?
 
ok i have a lot of forms with just 2 fields in i want to createte the forms as and when they are need
hence the create form clsForm Resize Module is to try and resize the controls through Tag Proptey
have not quite got that working yet

the code goes where it should be but it apears not to activate the resize text boxs and cmdButtons but if i put the form in to design view and re view it it works as it should

any Thourghts
be very much appritated

thanks Andy
 
ok i have a lot of forms with just 2 fields in i want to createte the forms as and when they are need
That's what I was afraid of.

If you have so many identical tables that you don't want to make forms for each of them, you have a design problem and fixing that will give you a better solution. Even if you don't fix your tables, you can still use a single form and just replace it's RecordSource.
 
ok i have a lot of forms with just 2 fields in i want to createte the forms as and when they are need
A database that requires design mode at runtime is a flawed design.

I suggest you post back describing the structure of your tables as your post is ringing normalization alarms.
 
Good morning and welcome to AWF!

As a self-proclaimed "Advanced Novice", perhaps it might sound different coming from me.

I do not know your background so please excuse me if I mis-speak.

It appears that your VBA skills are not at the level of the code you posted but are not afraid to get dirty. And although that is a good thing, you should be asking yourself "do I really need code?"

All that being said, you are making this far to complicated. If a form is needed, simply make the form. Doing so costs you nothing in the way of overhead and eliminates a few "moving parts".

It has been my experience that moving parts are opportunities for things to go wrong.
 
ok i have a lot of forms with just 2 fields in i want to createte the forms as and when they are need
I too am not following what it is your trying to do. What do you mean by "as and when needed"? Are you also deleting the forms when done with them?

the code goes where it should be but it apears not to activate the resize text boxs and cmdButtons but if i put the form in to design view and re view it it works as it should

I would venture a guess that your code is not activating your clsFormResize as there is no event calling it until you unload the form and load it again (design view to form view)

Could you use a custom class to instaniate a new instance of a template form and manipulate it from the class?
 

Users who are viewing this thread

Back
Top Bottom