Solved Displaying values in button captions (1 Viewer)

autumnnew

Member
Local time
Today, 05:06
Joined
Feb 4, 2013
Messages
44
Hi, is it possible to display values in button captions? I would like to have a button that says [ProjectNumber] & ", " & [Project Name]. The database only works on one project at a time, so there's only one button for one project being displayed. I was wondering if it's possible to type a formula into the caption property, but as you would expect, it gets displayed as text. I'm hoping that I'm just typing it wrong, and that there's a way to do this, but I'm guessing the only way to do it is in VBA.

My current solution is to use a text box to serve this purpose, but I want it to change colors when my cursor hovers over it. I can't seem to make that work with the Mouse Move event, because there doesn't seem to be a way to get the color to change back. This is the reason I was trying to use a button, so I can set the Hover color, but then there's the issue of displaying the values in the caption.

If someone could please help me come up with a solution for either of these, I'd greatly appreciate it. Thanks!
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 05:06
Joined
Feb 28, 2001
Messages
27,140
You can programmatically display any text you want in a button caption as long as you have room for it. You can do something as simple as

Code:
<<button-name>>.Caption = "Arbitrary text"

I am suspicious of the question about "typing a formula into the caption." Since you are new here, it is possible that we have a simple bit of confusion over nomenclature. As you literally asked the question, It is perfectly legal to type some formula into a caption, but only in design mode. If you want that in any other mode (like Form View), you would need to have the caption altered by VBA code at run-time. And that formula text can be any simple thing you want but it will be only an echo of the formula, sitting there and looking pretty. Also, you wouldn't be able to have an intermix of alphanumerics and special characters because there is only one font for the caption and I don't recall that it has the "rich text" option. I think only a very few controls actually allow rich text.

The trick with Mouse Move events to change colors on hover AND THEN CHANGE BACK is usually that the coordinates of the move have to be compared with the coordinates of the controls EVERY TIME THE MOUSE MOVES. I.e. very tedious. Not at all impossible. Just a pain in the toches and a potential drain on the CPU.
 

autumnnew

Member
Local time
Today, 05:06
Joined
Feb 4, 2013
Messages
44
Where (what event) would I put the code to program the text? You said at run-time.. does that mean the OnLoad event for the form?

You are right about the nomenclature. I apologize for the confusion. I was referring to Design Mode, where you can adjust the properties in the Property Sheet to the right. By "typing a formula into the caption property", I meant something such as =[ProjectNumber] & ", " & [Project Name], not some mathematical formula. So no matter what "formula" I type in design mode, it'll always be displayed as text?

1660456937187.png


I figured as much about the MouseMove. I tried a solution that I found somewhere where it changed the colors back in the MouseMove event for the Detail section, but that doesn't work well enough for my form.
 

June7

AWF VIP
Local time
Today, 02:06
Joined
Mar 9, 2014
Messages
5,466
I don't really understand the desire to have a dynamic caption on a button but if you must, VBA is the only way. Yes, OnOpen or OnLoad is appropriate. Did you try? Caption is just text, not calculatable expression as in a textbox ControlSource.

Otherwise stick with a textbox that acts as a dynamic title header on form. Put it in 20-pt bold red if you think users really need a strong reminder of the project number and name.

Exactly what does this button do?
 
Last edited:

autumnnew

Member
Local time
Today, 05:06
Joined
Feb 4, 2013
Messages
44
I don't really understand the desire to have a dynamic caption on a button but if you must, VBA is the only way. Yes, OnOpen or OnLoad is appropriate. Did you try? Caption is just text, not calculatable expression as in a textbox ControlSource.

Otherwise stick with a textbox that acts as a dynamic title header on form. Put it in 20-pt bold red if you think users really need a strong reminder of the project number and name.

Exactly what does this button do?
The button opens a Projects form where users can select a different project to work on. The Projects form has a dynamic search bar to quickly find projects. Click on the project in a list box and it closes and loads the project into the Main form in the screenshot below.

Currently, I'm using a text box that does this and it works fine. It displays the current project and name in the text box (which is what I want the button to do). The only problem is, there's nothing that visually indicates that text box is where a user is supposed to click to change to another project. The users just know that's where you're supposed to click because they've been using the database for years, but a new user wouldn't know that just by looking at the form. There's nothing that visually indicates anywhere or anyhow that there is an interactive object to click to change projects.

This is why I'm trying to determine if I can use a button instead, because unlike a text box, the button can change colors on Hover, but I didn't know how to get it to display project info. I'll try your VBA suggestion. Thanks for that!

I guess a last option would be to simply add a small button, with no text, specifically for switching to another project record, but I'm trying to keep my form from looking more cluttered.

1660458249993.png
 
Last edited:

June7

AWF VIP
Local time
Today, 02:06
Joined
Mar 9, 2014
Messages
5,466
Why would you have a button with no text? Why not a caption something like: "Change Project"
 

autumnnew

Member
Local time
Today, 05:06
Joined
Feb 4, 2013
Messages
44
Oh, I meant no Project-related text. Either 'change project' or some icon that indicates record selection/change.

The VBA works perfectly. This is just what I needed! Thank you!
 
Last edited:

Users who are viewing this thread

Top Bottom