Class modules?? for common operations (1 Viewer)

databasedonr

Registered User.
Local time
Today, 16:00
Joined
Feb 13, 2003
Messages
163
Can I write a procedure in a Class module that I can call from any form? What I am trying to do is create a button that I can set as active based on the state of a field. But I want to use this on all my forms!

What I am trying to do is:

If flag = True
Then set btnSave.Visible = True
Else set btnSave.Visible = False

I assume I can do this on one form, and I can write the code that if my recordset includes updated records, set the flag to true. But, can I set this as a Class module and call it from multiple forms? And if so, how do I call it from the form?
 

jfgambit

Kinetic Card Dealer
Local time
Today, 21:00
Joined
Jul 18, 2002
Messages
798
You could add code to the On Current Event of the forms that you want to utilize.

If me.checkboxname = True then
me.btnSave.Visible = Ture
Else
End if

If you set the Visible Property of the command button to No, then you don't need anything after ELSE.

HTH
 

RichMorrison

Registered User.
Local time
Today, 15:00
Joined
Apr 24, 2002
Messages
588
Yes, you can create a Class module with a "Public" sub to set a value on and off.

Something like this:

Public Sub VisibleOnOff(myControl As Control, mySetting As Boolean)
myControl.Visible = mySetting
End Sub

In a form you would write

VisibleOnOff(btnSave, True)

or

VisibleOnOff(btnSave, False)

I think that will work.

RichM
 

informer

Registered User.
Local time
Today, 22:00
Joined
May 25, 2016
Messages
75
Hi databasedonr

An other way

In a module
Code:
 Public Function VisibleOnOff(formName As String, controlName As String, flag As Boolean)
   Forms(formName).Form.Controls(controlName).Visible = mySetting
End Function

In a form you would write
Code:
 Private Sub cmdButtonVisible_Click()
   Call VisibleOnOff(Me.Form.Name, Me.cmdVisible.Name, False)
End Sub
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 04:00
Joined
May 7, 2009
Messages
19,246
that post was 13 year ago!
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 04:00
Joined
May 7, 2009
Messages
19,246
never too late?! every minutes counts. we don't even know if the guy's still around.
 

informer

Registered User.
Local time
Today, 22:00
Joined
May 25, 2016
Messages
75
But It could be interesting for others community members :D
 

NauticalGent

Ignore List Poster Boy
Local time
Today, 16:00
Joined
Apr 27, 2015
Messages
6,402
Arnelgp, you are an accomplished programmer; do you agree with Informer's code? I would definitely add this to my library for it would come in handy...
 

informer

Registered User.
Local time
Today, 22:00
Joined
May 25, 2016
Messages
75
Hi

A little mistake replace mySetting with flag. I certify this code 100% free bugs

Code:
Public Function VisibleOnOff(formName As String, controlName As String, flag As Boolean)
    Forms(formName).Form.Controls(controlName).Visible = flag 
End function
 

databasedonr

Registered User.
Local time
Today, 16:00
Joined
Feb 13, 2003
Messages
163
I'm still around, although I have to admit I don't remember what I was trying to do 13 years ago....

I still appreciate the answer! And it may come in handy - it'll go in my code libary.

Thanks again!
 

Galaxiom

Super Moderator
Staff member
Local time
Tomorrow, 06:00
Joined
Jan 20, 2009
Messages
12,859
In a module
Code:
 Public Function VisibleOnOff(formName As String, controlName As String, flag As Boolean)
   Forms(formName).Form.Controls(controlName).Visible = mySetting
End Function
In a form you would write
Code:
 Private Sub cmdButtonVisible_Click()
   Call VisibleOnOff(Me.Form.Name, Me.cmdVisible.Name, False)
End Sub

Rather than passing the formname and controlname then reassembling the reference in the code, you can pass a reference to the control itself. BTW Since there is no return value it makes sense to use a Sub unless you want to use the function directly in the Event property without having a procedure in the module.

Code:
 Public Sub VisibleOnOff(ByRef ctrl as Control, ByVal flag As Boolean)
   ctrl.Visible = flag
End Sub
In a form you would write
Code:
 Private Sub cmdButtonVisible_Click()
   VisibleOnOff Me.cmdVisible, False
End Sub
Good practice to always declare how a parameter will be passed. VBA default is ByRef so without a declaration, any change to the value of the parameter inside the procedure will be fed back to the calling procedure.

Sometimes we do pass a parameter ByRef to support more than one return value. So we declare the argument ByRef so that anyone maintaining the code will immediately know that there is potentially a value coming back through the parameter.

Note that objects and arrays are always passed ByRef regardless of the declaration.
 

Users who are viewing this thread

Top Bottom