Control buttons - stay visible (1 Viewer)

Mahall906

New member
Local time
Today, 22:33
Joined
Apr 11, 2014
Messages
29
On my form I have a checkbox called "owner" and when it is checked a button is shown. The code I use is:

Private Sub Owner_AfterUpdate()
If owner = True Then
Command2162.Visible = True
Else
Command2162.Visible = False
End If
End Sub

This works; however, when I leave the form, and that record, then go back into it, the button does not stay visible. How can I make that button stay visible when that checkbox remains checked?

I have the "Visible" control set to "No".

Any help as always would be appreciated ;-)
 

Gasman

Enthusiastic Amateur
Local time
Today, 22:33
Joined
Sep 21, 2011
Messages
14,277
Give your controls some decent names :( Command 2162 ?
You would need to use the same code, or make it a sub and call it from the afterupdate of the Owner control and the Forum Current event I would expect.
 

CJ_London

Super Moderator
Staff member
Local time
Today, 22:33
Joined
Feb 19, 2013
Messages
16,609
you can simplify your code

Code:
Private Sub Owner_AfterUpdate()

    Command2162.Visible = owner = True

End If

and as Gasman says, in the form current event

Code:
Private Sub Form_Current()
  
  Owner_AfterUpdate

End Sub
 

Mahall906

New member
Local time
Today, 22:33
Joined
Apr 11, 2014
Messages
29
Perfection! Thank you both!

As simple as that. This forum it excellent! ;-)
 

Mahall906

New member
Local time
Today, 22:33
Joined
Apr 11, 2014
Messages
29
Me again!
Now the visible options are working great; however, it will not allow me to open a new blank form to enter information. It allows be to open a form already completed.
Am I missing something obvious?

I get:
Run-time error '94':
Invalid use of Null

Thanks again
 

Miki13

New member
Local time
Today, 23:33
Joined
May 25, 2022
Messages
10
You need to put that code in OnCurrent event of the form.
Now you will have this code in two event: AfterUpdate event of the control and OnCurrent event of the form.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 17:33
Joined
Feb 19, 2002
Messages
43,266
I would not recommend calling event procedures from other places. When you need to use code from multiple events, it is better to just make a separate procedure and call that common procedure from both places.

If you ever wonder why when we get new versions of Access released by MS we find bugs, think about unexpected reuse of a procedure as the probable cause. Event procedures are expected to be executed when an event happens. No one expects them to be called from other procedures. Even you will forget some day.

As the others have mentioned, pay attention to control names. ALWAYS give them a proper name immediately even if you don't expect to reference them or add any code now because doing it later is a PITA. When you rename the control after you have added event code or other references, orphans the code and causes compile errors for references so you need to be diligent in cleaning up what you broke. That doesn't mean don't fix the names. It just means, be sure to clean up completely.

Also, I don't like
Command2162.Visible = owner
I much prefer the If Then Else because everyone will understand what it is doing. The Reader's Digest version of the code relies on a trick and when you rely on a trick (Any non-zero value except Null acts like "True" in certain situations), Access always finds a way to come back and burn you in some later release or you may misunderstand the trick and not use it correctly in a different situation.
 

CJ_London

Super Moderator
Staff member
Local time
Today, 22:33
Joined
Feb 19, 2013
Messages
16,609
Owner is a Boolean but each to their own
 

CJ_London

Super Moderator
Staff member
Local time
Today, 22:33
Joined
Feb 19, 2013
Messages
16,609
it will not allow me to open a new blank form to enter information. It allows be to open a form already completed.
Am I missing something obvious?

I get:
Run-time error '94':
Invalid use of Null

depends on the code you are using to open the form
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 17:33
Joined
Feb 19, 2002
Messages
43,266
@CJ_London Thanks, I didn't notice that.

@Mahall906 You need to check to see if this is a new record first.

Code:
If Me.NewRecord Then
    Me.Command2162.Visible = False
Else
    Me.Command2162.Visible = owner
End If
 

Mahall906

New member
Local time
Today, 22:33
Joined
Apr 11, 2014
Messages
29
@CJ_London Thanks, I didn't notice that.

@Mahall906 You need to check to see if this is a new record first.

Code:
If Me.NewRecord Then
    Me.Command2162.Visible = False
Else
    Me.Command2162.Visible = owner
End If
Thanks Pat Hartman, may seem a daft question, but where would I need to put this bit of code, in the Form_Current?
 

Mahall906

New member
Local time
Today, 22:33
Joined
Apr 11, 2014
Messages
29
You need to put that code in OnCurrent event of the form.
Now you will have this code in two event: AfterUpdate event of the control and OnCurrent event of the form.
Thanks, I did this and works great. Its just the new record bit now.
 

Mahall906

New member
Local time
Today, 22:33
Joined
Apr 11, 2014
Messages
29
I've only gone and done it! Spent a while, worked it out and happy! Again, thank you!! (y)
 

Users who are viewing this thread

Top Bottom