Default text in a bound combobox (1 Viewer)

chacham

Member
Local time
Today, 19:56
Joined
Oct 26, 2020
Messages
45
Just thought i'd share and perhaps even ask for comment. I am beginning to work on someone else's Access program and have a lot to learn.

Anyway, the request was to remove the label from a couple comboboxes and place default text in them instead. After some searching it seems this cannot be done easily because the comboboxes get their data from a query, limited to list, and have multiple columns of which the first is an id and not displayed. So, using one suggestion, i put a second combobox with the default text directly atop the first one, and in its MouseDown event, turned its visibility off, gave focus to the "real" combobox, and fired its DropDown event. Works like a charm.

To be specific: The new combobox gets the same Width, Height, Top, and Left values as the real one. ForeColor is set to #ABABAB, Row Source Type is Value List, and the Row Source and Default Value are set to the default text. The On Mouse Down Event is set to Event Procedure:

Code:
Private Sub cmbNH_default_text_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
'Just acting as default text. Let's go away and pass that click on to the "real" control.
cmbNH.SetFocus
cmbNH_default_text.Visible = False
cmbNH.Dropdown
End Sub
 
Last edited:

theDBguy

I’m here to help
Staff member
Local time
Today, 16:56
Joined
Oct 29, 2018
Messages
21,358
Just thought i'd share and perhaps even ask for comment. I am beginning to work on someone else's Access program and have a lot to learn.

Anyway, the request was to remove the label from a couple comboboxes and place default text in them instead. After some searching it seems this cannot be done easily because the comboboxes are bounded, limited to list, and have multiple columns of which the first is an id and not displayed. So, using one suggestion, i put a second combobox with the default text directly atop the first one, and in its MouseDown event, turned its visibility off, gave focus to the "real" combobox, and fired its DropDown event. Works like a charm.

To be specific: The new combobox gets the same Width, Height, Top, and Left values as the real one. ForeColor is set to #ABABAB, Row Source Type is Value List, and the Row Source and Default Value are set to the default text. The On Mouse Down Event is set to Event Procedure:

Code:
Private Sub cmbNH_default_text_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
'Just acting as default text. Let's go away and pass that click on to the "real" control.
cmbNH_default_text.Visible = False
cmbNH.SetFocus
cmbNH.Dropdown
End Sub
Interesting that you could hide a control that has the focus. Thank for sharing.
 

chacham

Member
Local time
Today, 19:56
Joined
Oct 26, 2020
Messages
45
I didn't realize that would be an issue. I just set a second combobox to act the same way, and it worked on testing as well. However, clicking both causes the second one to give me an error about it having focus. Good catch!

I just changed the code to put the Visible line second and the issue went away. Now to change the code in the top post.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 16:56
Joined
Oct 29, 2018
Messages
21,358
I didn't realize that would be an issue. I just set a second combobox to act the same way, and it worked on testing as well. However, clicking both causes the second one to give me an error about it having focus. Good catch!

I just changed the code to put the Visible line second and the issue went away. Now to change the code in the top post.
Glad to hear you got it sorted out. Cheers!
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 19:56
Joined
Feb 19, 2002
Messages
42,981
Are you sure this is working? I think if the control doesn't get the focus, this won't work and it might not even then.

If you want to have a default for a combo, you can use the default property in the table OR you can use the default property in the form. HOWEVER, the default is NOT the string. The default is the hidden ID value.
 

chacham

Member
Local time
Today, 19:56
Joined
Oct 26, 2020
Messages
45
It seems to be working.

The default is not in the list. It is a text, "Search Facility", for example.
 

June7

AWF VIP
Local time
Today, 15:56
Joined
Mar 9, 2014
Messages
5,423
Do you have code that prevents this 'clue' text from saving to record?
 

CJ_London

Super Moderator
Staff member
Local time
Today, 23:56
Joined
Feb 19, 2013
Messages
16,553
another way is to use the rowsource which does not necessarily require any code

SELECT ID, lastname, firstname from mytable union select 0,"default text","" from mytable

and set the default value of the combo to 0

to sort, if necessary include an extra field at the end to ensure the default text appears first

SELECT ID, lastname, firstname, 1 as sort from mytable union select 0,"default text","", 0 from mytable ORDER BY sort, lastname


if necessary you can have a bit of code in the combo gotfocus event to modify the rowsource so user cannot select the default text
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 19:56
Joined
Feb 19, 2002
Messages
42,981
t seems to be working.
Does the correct data get saved to the table when you save the form even if you never put focus in the field in question?
 

chacham

Member
Local time
Today, 19:56
Joined
Oct 26, 2020
Messages
45
Does the correct data get saved to the table when you save the form even if you never put focus in the field in question?

The comboboxes are used as parameters in the form query which gets refreshed when they lose focus. So, no saving here.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 19:56
Joined
Feb 19, 2002
Messages
42,981
You said the combos were bound in your original post.

If they are unbound then they don't affect saved data.
 

isladogs

MVP / VIP
Local time
Today, 23:56
Joined
Jan 14, 2017
Messages
18,186
As suggested above, there is a much easier solution that you could try.....

Use the format property for the combo.
For text strings, there are just 2 sections, not null and null
The last (null) is very useful to provide a prompt to the user in a textbox/combobox e.g. "Enter Last Name"

To do so, set the format property to: @;"Enter Last Name"
To get grey prompt text for this example, first set the control forecolor to grey, then the format property could be: [Black]@;"Enter Last Name"
The prompt text will be grey but the text entered by the user will be black

1644396758347.png
 

chacham

Member
Local time
Today, 19:56
Joined
Oct 26, 2020
Messages
45
You said the combos were bound in your original post.

If they are unbound then they don't affect saved data.
My mistake. I apologize, and thank you for correcting me.

I meant they get their data from a query. That's why i used the word "bound" without thinking it through. I'll correct the first post right now.
 

chacham

Member
Local time
Today, 19:56
Joined
Oct 26, 2020
Messages
45
As suggested above, there is a much easier solution that you could try.....

Use the format property for the combo.
For text strings, there are just 2 sections, not null and null
The last (null) is very useful to provide a prompt to the user in a textbox/combobox e.g. "Enter Last Name"

To do so, set the format property to: @;"Enter Last Name"
To get grey prompt text for this example, first set the control forecolor to grey, then the format property could be: [Black]@;"Enter Last Name"
The prompt text will be grey but the text entered by the user will be black

View attachment 98126
From searching and trying i saw that comboboxes do not support the format field in that way. That was the first thing i tried.

Okay, so trying again. I created a combobox and added that format property and it worked. I changed rowsource type to value list, and added "a" as a value, and it still worked. I changed it to Table/Query and chose a query and it did not work.
 

isladogs

MVP / VIP
Local time
Today, 23:56
Joined
Jan 14, 2017
Messages
18,186
Sorry to disagree but it does work using a table/query. The screenshot was using a query.
The only caveat is that the first column has to be visible as its the first field that is formatted with the prompt text.
 

chacham

Member
Local time
Today, 19:56
Joined
Oct 26, 2020
Messages
45
Sorry to disagree but it does work using a table/query. The screenshot was using a query.
The only caveat is that the first column has to be visible as its the first field that is formatted with the prompt text.
Don't be sorry. If i'm wrong, i'm wrong, and i want to be corrected. How else will i learn?

I tried it simple by connecting it to a query and it did not work. If there are rules, so be it, but it does not seem to work in my case. I'm happy to change things if it is easier than what i did and does not change the displayed data.
 

isladogs

MVP / VIP
Local time
Today, 23:56
Joined
Jan 14, 2017
Messages
18,186
Actually, it works even if the first field is hidden

1644429171093.png


1644429229851.png


1644429276957.png


As soon as you click in the combobox the default text disappears automatically
 

isladogs

MVP / VIP
Local time
Today, 23:56
Joined
Jan 14, 2017
Messages
18,186
Oops. My bad ....
Just went back to that combo and realised I'd changed it by mistake when testing moving the bound column. It SHOULD be 4
 

Users who are viewing this thread

Top Bottom