To many forms to modify one at a time

nomojo

Registered User.
Local time
Yesterday, 18:06
Joined
Jun 22, 2012
Messages
14
My problem is simple, and i imagine it is easy to fix. I have more forms in my db than I would like to count, and I have a couple lines of code to add to the onload event of each form.

Is there an way, without actually having to edit each form myself, to add the code i need to add to all forms. Is there some code I can write that will let me modify the on load property of a form, and save it?

I know what i need to do, minus the part where i actually change the onload event.
 
It's been years since I touched VBA so I may not be of much help. However, what kind of code are you trying to add to the On Load event of the form?
 
From Access Help:- (Search Access Help for this to see it nicely formated)

Module.InsertText Method

The InsertText method inserts a specified string of text into a standard module or a class module.
Syntax

expression.InsertText(Text)

expression A variable that represents a Module object.

Parameters

Name

Required/Optional

Data Type

Description

Text Required String The text to be inserted into the module.

Return Value
Nothing

Remarks


When you insert a string by using the InsertText method, Microsoft Access places the new text at the end of the module, after all other procedures.

To add multiple lines, include the intrinsic constant vbCrLf at the desired line breaks within the string that makes up the text argument. This constant forces a carriage return and line feed.

To specify at which line the text is inserted, use the InsertLines method. To insert code into the Declarations section of the module, use the InsertLines method rather than the InsertText method.





Note

In previous versions of Microsoft Access, the InsertText method was a method of the Application object. You can still use the InsertText method of the Application object, but it's recommended that you use the InsertText method of the Module object instead.



Example


The following example inserts a string of text into a standard module:



Visual Basic for Applications

Function InsertProc(strModuleName) As Boolean
Dim mdl As Module, strText As String

On Error GoTo Error_InsertProc
' Open module.
DoCmd.OpenModule strModuleName
' Return reference to Module object.
Set mdl = Modules(strModuleName)
' Initialize string variable.
strText = "Sub DisplayMessage()" & vbCrLf _
& vbTab & "MsgBox ""Wild!""" & vbCrLf _
& "End Sub"
' Insert text into module.
mdl.InsertText strText
InsertProc = True

Exit_InsertProc:
Exit Function

Error_InsertProc:
MsgBox Err & ": " & Err.Description
InsertProc = False
Resume Exit_InsertProc
End Function


adds multiple lines of text to a standard module insert insert text Insert Text Method insert text string into a module inserttext inserttext method putting a specified string of characters into a class module
 
Last edited:
BTW....

I think you will need to open the form with code something like this:-

From Access Help (Search Help for a better looking presentation)

Form.Module Property




You can use the Module property to specify a form module. Read-only Module object.
Syntax

expression.Module

expression A variable that represents a Form object.

Remarks


The Module property also returns a reference to a specified Module object.

Use the Module property to access the properties and methods of a Module object associated with a Form or Report object.

The setting of the HasModule property of a form or report determines whether it has an associated module. If the HasModule property is False, the form or report does not have an associated module. When you refer to the Module property of that form or report while in design view, Microsoft Access creates the associated module and sets the HasModule property to True. If you refer to the Module property of a form or report at run-time and the object has its HasModule property set to False, an error will occur.

You could use this property with any of the properties and methods of the module object.


Example


The following example uses the Module property to insert the Beep method in a form's Open event.



Visual Basic for Applications

Dim strFormOpenCode As String
Dim mdl As Module

Set mdl = Forms!MyForm.Module
strFormOpenCode = "Sub Form_Open(Cancel As Integer)" _
& vbCrLf & "Beep" & vbCrLf & "End Sub"
With mdl
.InsertText strFormOpenCode
End With
 
From Access Help. You may find this example from Access Help useful:- (Search Help for a better looking presentation)

Module.CreateEventProc Method

The CreateEventProc method creates an event procedure in a class module.
Syntax

expression.CreateEventProc(EventName, ObjectName)

expression A variable that represents a Module object.

Parameters

Name

Required/Optional

Data Type

Description

EventName Required String The name of an event.
ObjectName Required String An object that has the event specified by the eventname argument. If the event procedure is being added to a Form, the word "Form" should be specified for this argument. If the event procedure is being added to a Report, the word "Report" should be specified for this argument. If the event procedure is being added to a Control, the name of the control should be specified for this argument.

Return Value
Long

Remarks


The value returned by the CreateEventProc method indicates the line number of the first line of the event procedure.

The CreateEventProc method creates a code stub for an event procedure for the specified object. For example, you can use this method to create a Click event procedure for a command button on a form. Microsoft Access creates the Click event procedure in the module associated with the form that contains the command button.

Once you've created the event procedure code stub by using the CreateEventProc method, you can add lines of code to the procedure by using other methods of the Module object. For example, you can use the InsertLines method to insert a line of code.


Example


The following example creates a new form, adds a command button, and creates a Click event procedure for the command button:

Visual Basic for Applications

Function ClickEventProc() As Boolean
Dim frm As Form, ctl As Control, mdl As Module
Dim lngReturn As Long

On Error GoTo Error_ClickEventProc
' Create new form.
Set frm = CreateForm
' Create command button on form.
Set ctl = CreateControl(frm.Name, acCommandButton, , , , _
1000, 1000)
ctl.Caption = "Click here"
' Return reference to form module.
Set mdl = frm.Module
' Add event procedure.
lngReturn = mdl.CreateEventProc("Click", ctl.Name)
' Insert text into body of procedure.
mdl.InsertLines lngReturn + 1, vbTab & "MsgBox ""Way cool!"""
ClickEventProc = True

Exit_ClickEventProc:
Exit Function

Error_ClickEventProc:
MsgBox Err & " :" & Err.Description
ClickEventProc = False
Resume Exit_ClickEventProc
End Function
 
I just found this, which you may be able to adapt to loop through the forms collection, modifying each in turn.

Code:
For Each frm In CurrentProject.AllForms
        sFrmName = frm.name
        DoCmd.OpenForm sFrmName, acDesign
       
	'Access the form module Here:-

        DoCmd.Close acForm, frm.name, acSaveYes
Next frm
 

Users who are viewing this thread

Back
Top Bottom