Me in Public Function

Drand

Registered User.
Local time
Tomorrow, 05:50
Joined
Jun 8, 2019
Messages
179
Hi

I have an application that has many forms. I am trying to standardise the appearance (formatting) of each form.

Rather than run the new code in each form I have created a table with the new formatting for each object. (I did this to allow for any future changes in just one location).

I then created a public function as follows:


Public Function ChangeFormColours()


Dim Header As Long
Dim Detail As Long
Dim TextBox As Long
Dim TextFont As Long
Dim tb As Control
Dim cbo As Control



Header = DLookup("Header", "tblFormColours")
Detail = DLookup("Detail", "tblFormColours")
TextBox = DLookup("TextBox", "tblFormColours")
TextFont = DLookup("TextFont", "tblFormColours")

For Each tb In Me.Controls
If TypeOf tb Is TextBox Then
tb.BackColor = TextBox
tb.ForeColor = TextFont
End If
Next

For Each cbo In Me.Controls
If TypeOf cbo Is ComboBox Then
tb.BackColor = TextBox
tb.ForeColor = TextFont
End If
Next

Me.FormHeader.BackColor = Header
Me.Detail.BackColor = Detail

End Function

Obviously, the use of me is inappropriate and I want to be able to use the form name (as it loads) in this code where the me exists, but am stumped.

I am calling the function on Form Load.

Would appreciate any assistance.

Thank you

David
 
Add a form object as a parameter:-

Public Function ChangeFormColours(frmPassed as Form)

Replace Me. With frmPassed.

Call ChangeFormColours like this:-

Call ChangeFormColours(Me)
 
You are correct that Me outside of the class (form module) is invalid - is that where this code resides? You can either modify your Function (which could just as well be a Sub) to accept the form name or the form itself. I guess it depends on whatever else you're doing since I don't see where you indicate which form you're expecting to modify. Maybe you intend to open these forms in design view and modify their properties?
 
Uncle G's method is what I always use.
 
Replace Me. With frmPassed.
you might want to run a test on this, Drand. If I remember correctly , loooping through controls *without* a trailing collection identifier like ".controls" is not going to work. I'm almost certain the following will work instead, because vba will know exactly what you want due to the literal specification:
Code:
frmPassed.controls
so, to use the code you posted, and UG (tony's solution), combined they would look like this (or something similar):
Code:
Public Function ChangeFormColours(frm as form)

Dim Header As Long
Dim Detail As Long
Dim TextBox As Long
Dim TextFont As Long
Dim tb As Control
Dim cbo As Control

Header = DLookup("Header", "tblFormColours")
Detail = DLookup("Detail", "tblFormColours")
TextBox = DLookup("TextBox", "tblFormColours")
TextFont = DLookup("TextFont", "tblFormColours")

For Each tb In frm.Controls  '<<<<<<<<<<<<<<<<<<<<<<<<<<<< THIS is the change I'm talking about.
        If TypeOf tb Is TextBox Then
            tb.BackColor = TextBox
            tb.ForeColor = TextFont
        End If
    Next

     For Each cbo In frm.Controls  '<<<<<<<<<<<<<<<<<<<<<<<<<<<< THIS is also the change I'm talking about.
        If TypeOf cbo Is ComboBox Then
            tb.BackColor = TextBox
            tb.ForeColor = TextFont
        End If
    Next

frm.FormHeader.BackColor = Header  '<<<<<<<<<<<<<<<<<<<<<<<<<<<< and yet again!
frm.Detail.BackColor = Detail   '<<<<<<<<<<<<<<<<<<<<<<<<<<<< here too!

End Function
you could also do it another way by setting the form object you pass at the beginning of the routine to the active form:
Code:
your function(frm as form)
    set frm = screen.activeform
' <<<<<<<<<<< more code
Hopefully i've got that right! =)
 
If I remember correctly , loooping through controls *without* a trailing collection identifier like ".controls" is not going to work
I explain in detail in thread 19 why this most certainly works

Default properties are never required.
SomeCollection.item(0) is almost always simply SomeCollection(0)
Me.somecontrol.value is usually simply me.somecontrol
Me.Controls.item(0) can be Me.controls(0) because item is default property of a collaction
and further Me(0)

So
for each ctl in me.controls
can be simply
for each ctl in me since controls is default property of a form.
 
very nice, Maj. I stand corrected. Or maybe rather, I stand as "an addition to the obvious". are you aware of the web-based equivalent? "THIS". gotta love those little nuances! =)
 
I stand as "an addition to the obvious".
I do not know if I would call it obvious, VBA did not do anyone favors with default properties and bang and dot notation. Because of these there are an infinite amount of permutations for coding the same thing. It confuses a lot of people.
The first time I saw someone write Me("ControlName") I could not believe it worked. I tried to address some of this in that thread.

So here is a challenge. How many different permutations can you come up with to return the value of textbox1 on form1.
Example
No defaults and dot notation. No one ever does this but that is fully qualified.
Forms.item("forms1").controls.item("textBox1").value

This one is a little exaggerated beyond issues related to default properties and bang vs dot notation. Controls are a special case where they are added to the controls collection and uniquely made properties of a form. Gives you even more ways to refer to them.
 
Last edited:
"THIS". gotta love those little nuances! =)
In C# it is This and I think in Java it is Mine (can not remember). Not so nuance.
 
You could use my style manager in my sig but it does require a nameing convention

Mick
 
So here is a challenge. How many different permutations can you come up with to return the value of textbox1 on form1.
Example
No defaults and dot notation. No one ever does this but that is fully qualified.
Forms.item("forms1").controls.item("textBox1").value
you wanna pick my brain on that one, wise one? ;) check out the attached text files called "Form and Subform Reference Syntax" and "Form Control Referencing". Feel free to correct any errors you see, because those FAQs were written many years ago.
In C# it is This and I think in Java it is Mine (can not remember). Not so nuance.
THIS is used in a lot of places, but most famously with Javascript.
 

Attachments

I tend to use
Code:
    Screen.ActiveForm

A full Function could look like this:

Code:
Function SetHoverColor()

    Dim HoverColor As Long

    On Error Resume Next
    Select Case True
        Case Screen.ActiveForm.Name = "frmHome"
            If Not HoverColor = 16758883 Then HoverColor = 16758883
        Case Screen.ActiveForm.Name = "frm1"
            If Not HoverColor = 16758883 Then HoverColor = 16758883
        Case Screen.ActiveForm.Name = "frm2"
            If Not HoverColor = 16758883 Then HoverColor = 16758883
        Case Screen.ActiveForm.Name = "frm3"
            If Not HoverColor = 16758883 Then HoverColor = 16758883
        Case Screen.ActiveForm.Name = "frm4"
            If Not HoverColor = 16758883 Then HoverColor = 16758883
    End Select
    
    TempVars.Add "HoverColor", HoverColor
End Function

With that method, you could have forms appear the same or different. Example of the use of TempVars as well.
 

Users who are viewing this thread

Back
Top Bottom