Command button VBA code that refers to a box in another form. (1 Viewer)

Cosmos

New member
Local time
Today, 23:42
Joined
Aug 10, 2023
Messages
8
Hi,

I wonder if I could use command button vba code to change a box's backcolor in another form.

I created a command button (I named: Close, to close example: form1 that is open) and using vba code I want it also to change the backcolor of a box (I named: LED1yellow, to imitate a Light Emitting Diode) in another form (example form2).

I'm using:

LED1yellow. BackColor = vbYellow

To turn the box yellow. But how do I refer to this box that is in form2 from the Close button I did in form1?

Could this be done?

I built two databases and am learning along the way.

Any help please...

Cosmos
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 17:42
Joined
May 21, 2018
Messages
8,529
Access has a FORMS collection which holds a reference to every open form.
Forms("FormNameHere")
or in Bang Syntax
Forms!FormNameHere

So to reference a control on another form
Forms("Form2").LED1yellow. BackColor = vbYellow
or
Forms!Form2.led1yellow.backcolor = vbYellow

VBA has two syntaxes. One is called Bang (!) the other is Dot (.). Two ways to do the same thing in certain cases. And you can mix and match.
 

Cosmos

New member
Local time
Today, 23:42
Joined
Aug 10, 2023
Messages
8
I will try that, thanks 👍
 

Cosmos

New member
Local time
Today, 23:42
Joined
Aug 10, 2023
Messages
8
Access has a FORMS collection which holds a reference to every open form.
Forms("FormNameHere")
or in Bang Syntax
Forms!FormNameHere

So to reference a control on another form
Forms("Form2").LED1yellow. BackColor = vbYellow
or
Forms!Form2.led1yellow.backcolor = vbYellow

VBA has two syntaxes. One is called Bang (!) the other is Dot (.). Two ways to do the same thing in certain cases. And you can mix and match.
I will try, thanks 👍
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 17:42
Joined
Feb 19, 2002
Messages
43,284
I wonder if I could use command button vba code to change a box's backcolor in another form.
You can, as the others have provided code do do it, but that doesn't mean that this is the correct solution. Do you expect the background color on the "other form" to be what it was set to by the close button the next time formA opens?

Having formB change a value in a formA control can be done but here you are asking to change a property of the control which is not the same thing as changing the value in a bound control. Control properties are not saved in the table the way they are with Excel.

There may be a better solution but we need to know more about why formA can't set the background for the control all by itself.
 

Cosmos

New member
Local time
Today, 23:42
Joined
Aug 10, 2023
Messages
8
Access has a FORMS collection which holds a reference to every open form.
Forms("FormNameHere")
or in Bang Syntax
Forms!FormNameHere

So to reference a control on another form
Forms("Form2").LED1yellow. BackColor = vbYellow
or
Forms!Form2.led1yellow.backcolor = vbYellow

VBA has two syntaxes. One is called Bang (!) the other is Dot (.). Two ways to do the same thing in certain cases. And you can mix and match.
Yepee!
Thank you MajP it worked...
I don't know VBA much I'm still learning.

I used:
Private Sub btnClose_( )
Forms! Switchboard.
 

Cosmos

New member
Local time
Today, 23:42
Joined
Aug 10, 2023
Messages
8
Access has a FORMS collection which holds a reference to every open form.
Forms("FormNameHere")
or in Bang Syntax
Forms!FormNameHere

So to reference a control on another form
Forms("Form2").LED1yellow. BackColor = vbYellow
or
Forms!Form2.led1yellow.backcolor = vbYellow

VBA has two syntaxes. One is called Bang (!) the other is Dot (.). Two ways to do the same thing in certain cases. And you can mix and match.
Yepee!

Thank you MajP it worked...
I don't know VBA much I'm still learning.

I used:
Private Sub btnClose_( )

Forms!Switchboard.LED1yellow.BackColor = RGB(196, 145, 0)
DoCnd.Close

End Sub

The RGB values being to obtain a very dim yellow (from vbYellow in 'Category_List' form) to make the LED as if in an off state as soon as I close the present form 'Switchboard'.

You replied so early you must be an expert!

Cosmos
 

Cosmos

New member
Local time
Today, 23:42
Joined
Aug 10, 2023
Messages
8
Yepee!

Thank you MajP it worked...
I don't know VBA much I'm still learning.

I used:
Private Sub btnClose_( )

Forms!Switchboard.LED1yellow.BackColor = RGB(196, 145, 0)
DoCnd.Close

End Sub

The RGB values being to obtain a very dim yellow (from vbYellow in 'Category_List' form) to make the LED as if in an off state as soon as I close the present form 'Switchboard'.

You replied so early you must be an expert!

Cosmos
Sorry... DoCmd not DoCnd
 

Cosmos

New member
Local time
Today, 23:42
Joined
Aug 10, 2023
Messages
8
Sorry... DoCmd not DoCnd
Sorry I've made mistakes, this is what I meant:

The RGB values being to obtain a very dim yellow (from vbYellow in 'Switchboard' form) to make the LED as if in an off state as soon as I close the present form 'Category_List'.
 

Users who are viewing this thread

Top Bottom