|
Business
Solutions: MS Access: Controlling the Controls
By James D. Duncan, CPA September
2005
Ever wonder how the grayed-out selections got
that way? Ever notice that on entry screens with save buttons or void
buttons that they were not available all the time? And I am sure you have
noticed that when you clicked on the buttons they performed some task (save,
void, clear, etc.) and then went to an unavailable state. These buttons are
just a control with a fancy cover on them.
Let me show you how these buttons work and how
you can control them. First open a new form. Choose a command button and put
it on the form. Then either duplicate (copy and paste) it 2 more times or
put 2 more command buttons on the form. If the wizard is turned on just
press the cancel button when Access wants to help you with the command
button.

Next let’s us change the names of the command
buttons to cmdButton1, cmdButton2 and cmdButton3. Do this by right-clicking
on either of the buttons, selecting the name field on the All tab and keying
in cmdButton1 or what ever name you like. I used the prefix cmd so when I
see this name I know this is a command button. While you are there change
the captions to Enable Command3, Disable Command 3, and Command 3.
Now, on cmdButton1 open the properties, click on
the Event tab, select On Click, select the Event Procedure from the drop
down and double click on the 3 ellipses.
This will take you to the VBA code section. Access will create a subroutine
called cmdCommand1_Click. Enter the following line of code in the routine.
cmdCommand3.Enabled = False
Do the same thing for the cmdButton2, except put
“true” where we put “false” the first time. Now run the form. Click on the
Disable Command 3 button and notice that the button grays out and you cannot
click on it. It has been disabled.

Click on the Enable Command 3 and notice that now
the button is available. Nothing to it, when you know how.
If the Disable Command 3 button were a Save
button, it would run code to save a record and then disable itself.
Somewhere in your code when you wanted the form to be able to save again you
would enter the code that is on the Enable Command 3 button. I am showing
you the code; you will have to do the logic. There are a lot of sites that
help you and give you sample code. I am thinking of writing an article on
just sites I use or have used for help and sample code.
One more thing, if you save this form, get out
and get back in you will notice that Command 3 button is not disabled.
Right-click on the button in the design view, select the Data tab, change
the Enabled from yes to no. This is how the control is when the form opens.
On a save button you would not want it enabled when the form opens. You
could set this in the form open subroutine. but this does it and requires no
code. However, the advantage to doing it in code is that you can see the
code and do not forget where the property for this is hidden. I will give
you three guesses why I say that.
Practice your code. A good place to practice is
at a charity of your choice: Your church, your scout troop, or any
organization that needs some help. If you do not feel comfortable
programming for them, just donate some of your time. Trust me, you will love
it.

James is a member of
the CCOKC and an expert in Access and Excel.
Click here
to reach James.
|