Command Button Visibilty

G

gillman54

I'm new to Access I have designed a small database and want to make a command
button (on my mainform) visible only when the contents of an adjacent text
box reads "Yes". Be gentle with me, I'm not very conversant with code.

Gillman54
 
K

Keith Wilby

gillman54 said:
I'm new to Access I have designed a small database and want to make a
command
button (on my mainform) visible only when the contents of an adjacent text
box reads "Yes". Be gentle with me, I'm not very conversant with code.

Gillman54

Try putting this code in your form's On Current event:

If Me.txtMyTextBox = "Yes" Then
Me.cmdMyCommandButton.Visible = False
Else
Me.cmdMyCommandButton.Visible = True
End If

Substitute "txtMyTextBox" and "cmdMyCommandButton" with the names of your
objects

Keith.
www.keithwilby.co.uk
 
D

Douglas J. Steele

In the module associated with your form, create a routine along the lines
of:

Function ControlVisibility()

If Screen.ActiveControl = "NameOfCommandButton" Then
Me!NameOfAdjacentField.SetFocus
End If

Me!NameOfCommandButton.Visible = (Me!NameOfAdjacentField & vbNullString =
"Yes")

End Function

(replace NameOfAdjacentField and NameOfCommandButton with the appropriate
names)

If you have no other code in your form, you can simply call that function
for the form's Current event and for the text box's AfterUpdate event by
putting =ControlVisibility() (including the equal sign and parentheses) for
the event property. If you do have other code, call that function in your
form's Current event, as well as in the AfterUpdate event of the "adjacent
field":

Private Sub Form_Current()

Call ControlVisibility

' other code goes here...

End Sub

Private Sub
 
C

Chegu Tom

You might also wish to put that code in the AfterUpdate event of the
adjacent text box so that the button will appear if the user changes that
value
 
K

Keith Wilby

Chegu Tom said:
You might also wish to put that code in the AfterUpdate event of the
adjacent text box so that the button will appear if the user changes that
value

I'm not so sure, what happens if the user presses ESC to undo the change
before the record is committed?

Keith.
 
G

gillman54

Doug
Thanks for your suggestion. I have tried but cannot get it to work.
I omitted to say in my oiriginal post that the form I want the button
visibility control on is a subform, would this prevent the code from working
correctly?
 
D

Douglas J. Steele

If you're trying to manage the visibility of a control on a subform, that
code would go in the form being used as a subform, not the parent form.
 
G

gillman54

Doug / All

Thanks very much for your assistance all working now, most grateful.

regards

Chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top