Command Button Visibilty

  • Thread starter Thread starter gillman54
  • Start date Start date
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
 
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
 
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
 
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
 
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.
 
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?
 
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.
 
Doug / All

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

regards

Chris
 
Back
Top