Command Button

  • Thread starter Thread starter Lori
  • Start date Start date
L

Lori

Hello, I'm not sure if this is possible. I have created
a command button which opens another form. Can I have
this button change colors when data has been inputed in
that form? If so, how? Thank you in advance.
 
I haven't found a way to change the background color of a button. The forecolor (text) property of a command button is changeable by using a VBA "If Then" routine.

In order to explain in detail about how to change the color of the text, we'd need to know some details about your application.

Are you familiar with using VBA?
 
You can "simulate" a colored button by using a label (with non-default back
color) with Raised special effect. On its MouseDown event, change it to
Sunken effect. On its Click event, run code just as you would with OnClick
of a button. On its MouseUp event, change the special effect back to Raised.

--

Ken Snell
<MS ACCESS MVP>

rpw said:
I haven't found a way to change the background color of a button. The
forecolor (text) property of a command button is changeable by using a VBA
"If Then" routine.
In order to explain in detail about how to change the color of the text,
we'd need to know some details about your application.
 
That's cool! Thanks! It helps me 'cause I want to control the color of the "command buttons" so they blend in with the form regardless of the Windows settings.

Thanks again!
 
Ken, Sounds great, here's my problem. I am not very
advanced in access and sometimes need to be talked to
like a two year old. So, keeping that in mind, please
tell me, where is the MouseDown and MouseUp events? Thank
you for your help, I really appreciate it.
 
Thanks, rpw!

--

Ken Snell
<MS ACCESS MVP>

rpw said:
In Design view of the form, right-click the label then select Properties.
In the Properties window, click the Event tab. There should be five events
listed, including: On Mouse Down, and On Mouse Up.
 
Easy trick for this. Have the second form's command buttons do the following
(assuming that the second form is opened as Dialog mode):

The save button: save the record and close the form.

The cancel button: don't save the record and hide the form (set its
visible property to no).

In the first form's code, when the code starts again after the second form
is either closed or made invisible, test to see if the second form is still
open or not. (see function below). If it is open, the "cancel" button was
clicked. Close the form and do what is necessary based on a cancel. If it's
not open, then the "save" button was clicked.


Public Function IsTheFormOpen(ByVal xstrFormName As String) As Boolean
'Returns False if form is not open or is open in design view; returns
' True if it is open in form or datasheet view
On Error Resume Next
' Set default value for the function
IsTheFormOpen = False
If SysCmd(acSysCmdGetObjectState, acForm, xstrFormName) <> 0 Then
If Forms(xstrFormName).CurrentView <> 0 Then IsTheFormOpen = True
End If
End Function


--

Ken Snell
<MS ACCESS MVP>

rpw said:
Hey, you're welcome. You provided a neat little trick that I'll use in
the future, so I figure I can at least spend twenty-five minutes typing out
(two-finger hunt-and-peck) the instructions for Lori. ;-)
Stick around 'cause I'm sure that Lori will eventually want to know how to
code the "has the second form got new data?" event. I'm thinking that the
second form should have a Save and a Cancel button on it and that the first
form's button code could 'read' which button was clicked. Only I don't know
how to do that yet (if it can be done).
 
Code would be something like this:

Dim strFormName As String
strFormName = "Name of Second Form"
DoCmd.OpenForm strFormName, , , , , acDialog
If IsTheFormOpen(strFormName) = True Then
' continue with what is to be done if the 'cancel' button was clicked
End If


The IsFormOpen function needs to be put in a regular module.
--

Ken Snell
<MS ACCESS MVP>



rpw said:
Thank you for the function and the trick. Unfortunately, some of the
first form's code, like calling the function is just over my head.
Looks like the first form's code will have to declare xstrFormName as a
string, then set it equal to the second forms name.
Then a docmd to open the second form
And then an If Then uses the function?
If IsTheFormOpen = True Then Me.Label16.BackColor = 0 '(some color number)

So does the code stop when the second form opens, and then runs again when
it's visible property is set to No or it's closed? Or is there something
else to do?
Sorry that I'm not working through this and asking questions instead. I
don't have a second form with Save and Cancel buttons on it to experiment
with at this time -maybe a little later. Am I on the right track?
 
Aaahhh, I see. My imagination was very close. Two things I missed: The strFormName in parenthisis after the function and the opening of the form in acDialog mode. Why acDialog as opposed to the default acWindowNormal?
 
When you open a form in dialog mode, all other code pauses until the opened
form is closed or is made invisible. It then allows you to pop up a form
that requires user to do something before the code can continue. If you open
it in normal mode, the code that opened the form continues to run, which is
sometimes the desire in some situations, but not in all.

--

Ken Snell
<MS ACCESS MVP>

rpw said:
Aaahhh, I see. My imagination was very close. Two things I missed: The
strFormName in parenthisis after the function and the opening of the form in
acDialog mode. Why acDialog as opposed to the default acWindowNormal?
 
(revised) Code would be something like this:

Dim strFormName As String
strFormName = "Name of Second Form"
DoCmd.OpenForm strFormName, , , , , acDialog
If IsTheFormOpen(strFormName) = True Then
DoCmd.Close acForm, strFormName
' continue with what is to be done if the 'cancel' button was clicked
End If


--

Ken Snell
<MS ACCESS MVP>

rpw said:
Thank you for the function and the trick. Unfortunately, some of the
first form's code, like calling the function is just over my head.
Looks like the first form's code will have to declare xstrFormName as a
string, then set it equal to the second forms name.
Then a docmd to open the second form
And then an If Then uses the function?
If IsTheFormOpen = True Then Me.Label16.BackColor = 0 '(some color number)

So does the code stop when the second form opens, and then runs again when
it's visible property is set to No or it's closed? Or is there something
else to do?
Sorry that I'm not working through this and asking questions instead. I
don't have a second form with Save and Cancel buttons on it to experiment
with at this time -maybe a little later. Am I on the right track?
 
Thank you for the explanation. That answers my question about stopping and starting the code. And your other post with the closing of the second form makes sense too - it shouldn't be kept open indefinately.

Again, THANKS for all of your time and information on this thread.
 
rpw...thank you, I got it. -L
-----Original Message-----
In Design view of the form, right-click the label then
select Properties. In the Properties window, click the
Event tab. There should be five events listed,
including: On Mouse Down, and On Mouse Up.
Click into the properties field of On Mouse Down and a
drop-down will become visible - click it. Select [Event
Procedure] then click the elipsis to the right of the
field (....).
That should have opened VBA and you should see something like this in the window:

Private Sub Label16_MouseDown(Button As Integer, Shift
As Integer, X As Single, Y As Single)
End Sub

Click into the space between "Private Sub....." and "End
Sub" and hit the tab key once then type "me." without the
quotation marks:
me.

If VBA intellisense is turned on, you should see a list appear.

Scroll through the list and find the name of the label, or type the name of the label.

At the end of the label name, type a dot (just like at
the end of "me.") and another drop-down should appear.
Scroll through and find "SpecialEffect" (or type the word
without the quotation marks).
Type an equal sign (=) and the number 2.

Your code should now look something like this except
that it should have the name of your label:
Private Sub Label16_MouseDown(Button As Integer, Shift
As Integer, X As Single, Y As Single)
Me.Label16.SpecialEffect = 2
End Sub

Next, do the same for the On Mouse Up, except the special effect number is 1

Also, to change the color of the "button", go back to
the Properties window of the label and click on the
Format tab. Click the elipsis to the right of the Back
Color property and select from the colors shown. Do the
same for the Fore Color which colors the text.
 
Yes, I got it working...exactly what I needed.
-----Original Message-----
You're welcome. Did you get the rest of the stuff about
determining if the second form is open/closed etc?
--
rpw


Lori said:
rpw...thank you, I got it. -L
-----Original Message-----
In Design view of the form, right-click the label
then
select Properties. In the Properties window, click the
Event tab. There should be five events listed,
including: On Mouse Down, and On Mouse Up.
Click into the properties field of On Mouse Down and
a
drop-down will become visible - click it. Select [Event
Procedure] then click the elipsis to the right of the
field (....).
That should have opened VBA and you should see
something
like this in the window:
Private Sub Label16_MouseDown(Button As Integer,
Shift
As Integer, X As Single, Y As Single)
End Sub

Click into the space between "Private Sub....."
and "End
Sub" and hit the tab key once then type "me." without the
quotation marks:
me.

If VBA intellisense is turned on, you should see a
list
appear.
Scroll through the list and find the name of the
label,
or type the name of the label.
At the end of the label name, type a dot (just like
at
the end of "me.") and another drop-down should appear.
Scroll through and find "SpecialEffect" (or type the word
without the quotation marks).
Type an equal sign (=) and the number 2.

Your code should now look something like this except
that it should have the name of your label:
Private Sub Label16_MouseDown(Button As Integer,
Shift
As Integer, X As Single, Y As Single)
Me.Label16.SpecialEffect = 2
End Sub

Next, do the same for the On Mouse Up, except the special effect number is 1

Also, to change the color of the "button", go back to
the Properties window of the label and click on the
Format tab. Click the elipsis to the right of the Back
Color property and select from the colors shown. Do the
same for the Fore Color which colors the text.
--
rpw


:

Ken, Sounds great, here's my problem. I am not very
advanced in access and sometimes need to be talked to
like a two year old. So, keeping that in mind, please
tell me, where is the MouseDown and MouseUp events? Thank
you for your help, I really appreciate it.

-----Original Message-----
You can "simulate" a colored button by using a label
(with non-default back
color) with Raised special effect. On its MouseDown
event, change it to
Sunken effect. On its Click event, run code just
as
you
would with OnClick
of a button. On its MouseUp event, change the special
effect back to Raised.

--

Ken Snell
<MS ACCESS MVP>

(e-mail address removed)...
I haven't found a way to change the background color
of a button. The
forecolor (text) property of a command button is
changeable by using a VBA
"If Then" routine.

In order to explain in detail about how to
change
the
color of the text,
we'd need to know some details about your application.

Are you familiar with using VBA?
--
rpw


:

Hello, I'm not sure if this is possible. I have
created
a command button which opens another form. Can I
have
this button change colors when data has been inputed
in
that form? If so, how? Thank you in advance.



.


.
.
 
Back
Top