button question: two different commands?

S

scubadiver

Is it at all possible to use the same button for two different commands. I
want to make a subform visible and invisible with the same button.
 
S

scubadiver

I have inserted the following single line

Me.ComQry.Visible = Not Me.ComQry.Visible

but nothing is happening.
 
S

scubadiver

Private Sub CompQry_Click()

Me.ComQry.Visible = Not Me.ComQry.Visible

End Sub

in the button code on the main form
 
S

scubadiver

That is correct but still nothing.




Baz said:
Is ComQry the name of the subform control?

In the property sheet for the command button (CompQry?), Event tab, make
sure that [Event Procedure] is selected against "On Click".

scubadiver said:
Private Sub CompQry_Click()

Me.ComQry.Visible = Not Me.ComQry.Visible

End Sub

in the button code on the main form
 
S

scubadiver

I appreciate that could be a source of difficulty so I have changed it.

The button is called "CompQryButton" and the subform control is called
"comqry". The code I now have in the "on click" event is:

Private Sub CompQryButton_Click()

Me.ComQry.Visible = Not Me.ComQry.Visible

End Sub
 
B

Baz

Set a breakpoint in the event procedure to find out if it is actually
running when you click the button.
 
B

BruceM

I doubt Access was confused, but humans may become addled by similar names
<g>.
Just to make sure of a point, the CompQryButton is on the main form?
Sometimes when code doesn't work, but should, I delete the line and type it
again. Assuming you are using Intellisense in the VBA editor (I think
that's what it is called when you start typing and get a list of options),
does ComQry show up among the options after you type "Me."?
 
S

scubadiver

compqrybutton is on the main form and comqry does come up in the list of
options.

I am not too sure about break points and how I check. I've had a go but
nothing seems to be happening. Can you clarify? thanks.
 
S

scubadiver

I inserted the breakpoint at the code and inserted the message underneath.

Private Sub CompQryButton_Click()

Me.ComQry.Visible = Not Me.ComQry.Visible
MsgBox "Hello World"

End Sub


When I pressed the button the message didn't appear and the first line
turned yellow. I also tried inserting the message before the code

Private Sub CompQryButton_Click()

MsgBox "Hello World"
Me.ComQry.Visible = Not Me.ComQry.Visible

End Sub

The message appeared and the second line turned yellow.
 
B

Baz

OK, so the code is running and not throwing an error (is it?). In which
case ComQry, whatever it is, is being made visible/invisible. My hunch is
that ComQry is not what you think it is.
 
S

scubadiver

Thanks for your help.

I will see if I can resolve it but I think I may resort to having two boxes
and making them look like buttons.
 
B

BruceM

You do not need two buttons. The code should work. If it doesn't, having
two buttons (or two boxes that look like buttons) is not going to solve
anything.
Comment out the Me.ComQry line of code (remove the breakpoint first), so
that you just have the message box. Click the button. You should see the
message box. If not, go to the Event tab on the button's property sheet,
and verify that you see "Event Procedure" next to the Click event. If you
see it, click the three dots and verify that it takes you to the correct
code. If you do not see it, select Event Procedure for the Click event. If
the code appears, compile and close the VBA window. If there is no code,
that is of course the problem.
Some more things to try:

Try changing your message box line of code to:
MsgBox Me.ComQry.Visible
It should return either True or False

In form design view, there is on the Formatting toolbar a window with a
drop-down arrow. It is the leftmost item on the toolbar by default, I
believe. It lists all of the controls on the form. If ComQry is on the
list, click it and note what is selected on the form.
 
B

Baz

Yep, agree with all of that.

BruceM said:
You do not need two buttons. The code should work. If it doesn't, having
two buttons (or two boxes that look like buttons) is not going to solve
anything.
Comment out the Me.ComQry line of code (remove the breakpoint first), so
that you just have the message box. Click the button. You should see the
message box. If not, go to the Event tab on the button's property sheet,
and verify that you see "Event Procedure" next to the Click event. If you
see it, click the three dots and verify that it takes you to the correct
code. If you do not see it, select Event Procedure for the Click event. If
the code appears, compile and close the VBA window. If there is no code,
that is of course the problem.
Some more things to try:

Try changing your message box line of code to:
MsgBox Me.ComQry.Visible
It should return either True or False

In form design view, there is on the Formatting toolbar a window with a
drop-down arrow. It is the leftmost item on the toolbar by default, I
believe. It lists all of the controls on the form. If ComQry is on the
list, click it and note what is selected on the form.
 
S

scubadiver

Hoorah! I've managed it. Now I have a further question.

What my button does is change between two list boxes that hold record
information.
To the left of the button I have a label. When I press the button I want the
label to change as well
 
S

scubadiver

I have sorted out the label change using a text box instead.

Thanks for your help.
 
B

BruceM

You could have changed the label's Caption property:
Me.LabelName.Caption = "Caption Text"
 

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