Enable CommandButton from another Form (1 Viewer)

luzmen

Registered User.
Local time
Today, 12:22
Joined
Jul 6, 2017
Messages
10
Hello...

The situation is this:
I have 2 Forms (let say frmBox1 and frmBox2). In frmBox1 i have CommandButton1 that I set the Properties>Data>Enabled = No.
In frmBox2, i have CommandButton2 to open frmBox1.

I want to insert a VBA code in CommandButton2 to Enabled=Yes the CommandButton1 before the code to open frmBox1.

I tried this code:
Code:
Forms("frmBox1").CommandButton1.Enabled = Yes
.. but i have run-time error '2450': Microsoft Access cannot find the referenced form 'frmBox1'.

Hoping there is a kind heart to help me out this small problem.

Thank you in advance...
 

JHB

Have been here a while
Local time
Today, 21:22
Joined
Jun 17, 2012
Messages
7,732
You need to open the form before you can do anything in it.
 

CJ_London

Super Moderator
Staff member
Local time
Today, 20:22
Joined
Feb 19, 2013
Messages
16,607
the form needs to be open for access to be able to see it - put the code after you have opened the form.
 

Minty

AWF VIP
Local time
Today, 20:22
Joined
Jul 26, 2013
Messages
10,371
The form would have to be open for you to change a controls state.

If the form can be opened from different forms then I would use the OpenArgs property to pass the calling forms name and enable /disable the control based on that.

So to open it

Code:
Docmd.OpenForm "frmBox1", acNormal, , , , , "frmBox2"

And then in the on load property of frmBox1 put

Code:
If Me.OpenArgs = "frmBox2" Then 
       CommandButton2.enabled = True
Else 
       CommandButton2.enabled = False
End If
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 20:22
Joined
Jul 9, 2003
Messages
16,280
I have used the technique shown here, for many years. I find it is very flexible, also, quick and easy to copy from one Form to another.

I have also done a YouTube video demonstrating this technique.

You can find a link to the YouTube video and the Sample Database used in the video on my website here:-

Passing Form Values (See Video 1)

In the sample database I use this code:-

Code:
Private Sub btnOpenForm2_Click()
Dim strFrmName As String
strFrmName = "Form2"
   
    DoCmd.OpenForm strFrmName
        With Forms(strFrmName)
        '.btnOnForm2.Enabled = True
        '.fSetUp ("Sample Parameter")
        '.Caption = "I CAN CHANGE THE CAPTION"
        End With

End Sub

... to open the form and directly and access the properties of the command button.

I have deliberately left items (the commented out lines) in the code so you can get an idea of what's possible by using this method.

For example this line here '.fSetUp calls the function "fSetUp" which is a public function within the form you are opening. If you "un comment" that line it will calls a function and you can see how it works.

This is a very powerful way of controlling what happens when you open another form....

You can also use function parameters which provide one method of passing values through to the form you are opening…
 
Last edited:

luzmen

Registered User.
Local time
Today, 12:22
Joined
Jul 6, 2017
Messages
10
Thank you so much guys...

I just put the code to enable the CommandButton1 after the code to open the form, and change the =Yes to =True.

Once again thank you so much.
 

CJ_London

Super Moderator
Staff member
Local time
Today, 20:22
Joined
Feb 19, 2013
Messages
16,607
not sure you have changed anything. Booleans have the value of -1 and 0 which can also be displayed as True/False, Yes/No

So True and Yes are the same thing
 

Users who are viewing this thread

Top Bottom