Full Version: control visibility of command button on a form
UtterAccess Discussion Forums > Microsoft® Access > Access Forms
montekramer
Here is what I have. A form with 2 command buttons T and P. Clicking T will open a new form that lists all the transformers related to the record on the main form. Clicking P will open a form that has a picture of the substation that relates to the record on the main form. What I would like to happen is that when the main form is opened; the P button will only be visible if it has a related record in the pictures table, otherwise it will not be visible. The picture table is not part of the query that populates the main form. I have tried something similar to this code in the On Current property.
If Me.SUB_KEY = "select SUB_KEY from SUB_PICtbl where SUB_PICtbl.SUB_KEY = Me.SUB_KEY" then
Form!SUBfrm!PictureButton.Visible = True
else
Form!SUBfrm!PictureButton.Visible = False
End IF

This not work:-) Any ideas will be welcomed. I am new to Access and am trying to modify this database to be more user friendly. Thank you for your consideration and help.
Sligo
Welcome to UtterAccess!!!!!

There are two notable things here. I'll deal with the easy one first.

You can't set a control to non-visible (or disabled) if it has focus. So, it's always a good idea to make sure you set focus to a valid control prior to changing the visible or enabled property to false.

Now the hard part...

You want to know if records exist in Sub_PicTbl when sub_key is a certain value. I suppose the easiest way to do this is as follows:

Private Sub Form_OnCurrent()
dim lngCount as long

lngcount = dcount("*","Sub_PicTbl","Sub_Key = " & me.sub_key)

if lngcount = 0 then
me.OtherButton.Setfocus ' move focus to the other button, whatever it's called
me.PictureButton.Enabled = false
else
me.PictureButton.Enabled = true
end if

End Sub

(Note: I prefer to use enabled/disabled because this way the user can still see the button, they just can't use it. However, if you prefer to use visible, just changed "Enabled" to "Visible".)

I hope this helps!

- Sligo
montekramer
Sligo. That worked just like I wanted. I took your advice and went with enabled and disabled. Some user might think something is wrong if they can't see the command button. Many thanks. Monte
Sligo
I'm glad I could help!
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.