Coding a toolbar command button

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello,

I have a toolbar button that opens a form that is used for notifications.
I'd like to code this button so that, when there is a notification, the
button's caption turns red.

Normally the notifications form is blank (no records). This is because, as
soon as a notification expires, we delete it. We don't keep expired
notifications in this database. Notifications are typed directly into the
form, then the record is deleted when it expires.

Is there a way for me to create code that will allow the button caption to
turn red when there is a notification? Where would I place it? At the form
level? At the table level? Or would I attach a macro to the command button?

Many thanks,
 
Rosemary,

The code to change the caption code is simply:

Me.Command1.ForeColor = vbRed

within the form's own module, or

Forms!FormName.Command2.ForeColor = vbRed

in another module.

Now, how you capture the existence of a notification, is a different
question, which cannot be answered specifically based on the info you
provided. How are notifications created? For instance, if they are
entered by users through a form, then the event to use would be some
event on that form (which one specifically depends on how the form
works). Alternatively, you could use a timer event to count the number
of records in the table where notifications are stored.
In the case of entry through a form, I would prefer an unbound form with
a Save command button which stores the notification and changes the
caption color at the same time.

HTH,
Nikos
 
Hi Nikos (and Rosemary),

I was going to respond along the lines of your post, but then noticed that
Rosemary asked about changing the caption of a TOOLBAR button, not a button
on a form. I'm not even sure if that can be done (well, anything can be
done with lots of devious API tweaking, but ...).

If the button is on a form, then the "trigger" could come via a dcount on
the table containing notifications (if that's how they're stored), with the
code being place in the form's load event. For example:

If dcount("*","tblNotifications") then
Me.Command1.ForeColor = vbRed
else
Me.Command1.ForeColor = vbBlack
End If

The choice of event to place the code in depends, as you said, on many
things.

HTH,

Rob
 
Hi Nikos and Rob,

Thank you both for your replies. The button is currently on the toolbar,
however, after reading your post, I am thinking I should use a button on a
form instead. Can I place a button on the Switchboard and use the code you
have supplied? This way, whenever anyone goes to the Switchboard, they can
see if they need to check notifications.

Many thanks,
Rosemary
 
Hi Nikos,

In response to your question about how we capture notifications:

I have created a simple form with four fields date, name, expiration date,
and notification details. The form is based on a table, which is not related
to any other table in the database.

The user enters the information by simply typing it in.

When the expiration date is reached, a user simply clicks the "delete
record" button I created, and that notification is deleted from the database.

Does this supply the information you need? The key field is called
ReservID. I was thinking of creating a query and using the criteria that the
ReservID is not "0" (I'm new to this). Does this sound possible? If not,
which event do you t hink we would use?

Many thanks,
Rosemary
 
Hi Nikos,

The dCount method worked. I placed a command button on my Switchboard next
to the Notifications menu item. I then placed your code in the OnLoad event
of the Switchboard. If there is a record in tblNotifications, it changes
color. If not, it stays black. Thanks!

Now, I need to have the code fire whenever the Switchboard is accessed, in
addition to when it loads. Do I use a Refresh? Should I put it in the
OnOpen event?

Thanks again,
Rosemary
 
Rosemary,

In light of this new information, I would put the following procedure in
a general module:

Sub Check_For_Notifications()
vCount = DCount("*", "tblNotifications")
If vCount = 0 Then
Forms!FormName.Command2.ForeColor = vbBlack
Else
Forms!FormName.Command2.ForeColor = vbRed
End If
End Sub

And call it from the following events:

Switchboard: Open
Notif. Entry Form: Current, Close
Delete Record button: Click

simply with a line of code like:

Check_For_Notifications

I believe this will capture all events, and so keep it uptodate at all
times.

If you wish, you can make a simple delete query on your tblNotifications
where expiry < Date, and run it in the Open event of the switchboard, so
it will clean up expired notifications automatically every time the
database is opened. In that case, the call to the sub in the
switchboard's Open event should be after the delete query is run.

HTH,
Nikos
 
Back
Top