Solved How to open combo box when form loads (1 Viewer)

DataAmatuer

New member
Local time
Yesterday, 18:15
Joined
Feb 15, 2021
Messages
16
My form has a combo box name cboCkInsCo. I want the box to open (i.e., dropdown) when the form form loads. I have the following code in the On Load event:
Me.cboCkInsCo.SetFocus
Me.cboCkInsCo.Dropdown
(I also have the dropdown line in the On Got Focus event.)

But the combo box still does not open (i.e., dropdown) when the form loads. I have to either click on the dropdown arrow, or tab out and shift-tab back (thus activating the On Got Focus event) to get the box to open.

How can I make the box open when the form loads?
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 17:15
Joined
Oct 29, 2018
Messages
21,473
Hi. Try using a Timer event.
 

DataAmatuer

New member
Local time
Yesterday, 18:15
Joined
Feb 15, 2021
Messages
16
How do I do that?
I did go to the On Timer event and selected [Event Procedure] and set the Timer Interval property to 1. This now DOES open the combo box, but I cannot tab or mouse click out of it??
 

isladogs

MVP / VIP
Local time
Today, 01:15
Joined
Jan 14, 2017
Messages
18,221
As suggested, move your code to a timer event.
Set the form timer interval to e.g. 10 (milliseconds =0.01s) so there is no noticeable delay.
Add the line Me.TimerInterval=0 after your existing code to disable the timer so it only runs once
 

DataAmatuer

New member
Local time
Yesterday, 18:15
Joined
Feb 15, 2021
Messages
16
Thank you. I did as you suggested, but now the combo box only stays open momentarily. Is there any way that I can get it to open when the form loads, and stay open, but behave like it does when I have clicked on it? In other words, permit me to tab or click out of it.
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 01:15
Joined
Jul 9, 2003
Messages
16,282
There is a way to do this. I posted a sample file in a previous thread, see here:-

Why Doesn't the Dropdown sustain?​

 

isladogs

MVP / VIP
Local time
Today, 01:15
Joined
Jan 14, 2017
Messages
18,221
Thank you. I did as you suggested, but now the combo box only stays open momentarily. Is there any way that I can get it to open when the form loads, and stay open, but behave like it does when I have clicked on it? In other words, permit me to tab or click out of it.
Simple example attached. It works for me
 

Attachments

  • Database4.accdb
    476 KB · Views: 235

DataAmatuer

New member
Local time
Yesterday, 18:15
Joined
Feb 15, 2021
Messages
16
Simple example attached. It works for me
Thank you very much. It now seems to work as desired. It was the "DoEvents" line after the ".SetFocus" line that made it work.

Just in case someone else is struggling with this in the future, here's how I have it set up now:
1. In the On Timer event procedure I have this code:
Private Sub Form_Timer()
Me.cboCkInsCo.SetFocus
DoEvents
Me.cboCkInsCo.Dropdown
Me.TimerInterval = 0

End Sub

2. In the Timer Interval property (on the Property Sheet for the Form), I have entered 10.

Now when the form loads, the combo box opens (drops down) but I can still tab or click out of it just as if I had opened it by clicking on the dropdown arrow.

Thanks again to isladogs! 👌
 

isladogs

MVP / VIP
Local time
Today, 01:15
Joined
Jan 14, 2017
Messages
18,221
You're welcome.
FWIW it also works without the line DoEvents for me but I added it as a 'security blanket' just in case it was needed
 

DataAmatuer

New member
Local time
Yesterday, 18:15
Joined
Feb 15, 2021
Messages
16
One more question about this:

I would have never thought of using the On Timer event to solve this problem. Can anyone explain why this event must be used instead of the On Load (or On Open) event?
 

isladogs

MVP / VIP
Local time
Today, 01:15
Joined
Jan 14, 2017
Messages
18,221
I think the form needs to completely load before you can modify the behaviour of any of the individual controls.
Using the Timer event provides the delay needed by running a short time after load is complete
 

Mike Krailo

Well-known member
Local time
Yesterday, 20:15
Joined
Mar 28, 2020
Messages
1,044
While all of this is very nice, the question remains. Why do you need the combo box to dropdown on form load? That just doesn't make any sense to me. That would really annoy me as a user.
 

isladogs

MVP / VIP
Local time
Today, 01:15
Joined
Jan 14, 2017
Messages
18,221
Mike
Would it still annoy you if the form had a listbox instead?
 

Gasman

Enthusiastic Amateur
Local time
Today, 01:15
Joined
Sep 21, 2011
Messages
14,299
I think @Mike Krailo has a point?
I can understand it if the form opens to a new record and that is the first control for data entry, but if not, yes it would annoy me as well. :)
 

DataAmatuer

New member
Local time
Yesterday, 18:15
Joined
Feb 15, 2021
Messages
16
I think the form needs to completely load before you can modify the behaviour of any of the individual controls.
Using the Timer event provides the delay needed by running a short time after load is complete
Thank you, isladog, for explaining why the On Timer event is needed here. (And my apologies for tardiness in responding. Yesterday, shortly after posting the question about why the On Timer event was needed, I had to go offline and am just now getting back on.)

Your explanation, along with a post by Uncle Gizmo in this same thread, helped me to resolve a problem that started occurring when using the On Timer event to open the combo box when the form loads. The problem was that the box would open on form load, but it would close shortly thereafter. This behavior was not consistent; sometime the box would stay open, other times it wouldn’t. I tried changing the Timer Interval from 0 to 10, and then to 100, and then to 1000, and even to 10000, all to no avail. Sometimes the box would stay open, other times it would close after just a few moments. Seemed strange to me.

But Uncle Gizmo’s post earlier in this tread addressed the question “Why Doesn’t the Dropdown sustain?” And your comment about about the form needing to completely load, and Uncle Gizmo’s dB example of using a public function, led me to create a public sub (within the Class Module for the form) named “Dropdown” and then put a Call to that public sub as the last line of code in the On Load event procedure. So far, this seems to open the combo box when the form loads and the box stays open.

Here is the relevant code from the On Load sub:
Code:
Me.cboCkInsCo.SetFocus
Call Dropdown

And here is the public sub “Dropdown”:
Code:
Public Sub Dropdown()
   Me.cboCkInsCo.Dropdown
End Sub

My thought is that by separating the “.Dropdown” command from the “.SetFocus” command in the On Load sub gives Access the time to completely load the form before opening the combo box as per your suggestion.

Thanks again!.
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 01:15
Joined
Jul 9, 2003
Messages
16,282
problem that started occurring when using the On Timer event to open the combo box when the form loads.

I seldom use the timer event because I find it causes problems. I also recall that when I first started programming the general consensus was not to use the Timer. I have no idea what the latest advice is.
 

isladogs

MVP / VIP
Local time
Today, 01:15
Joined
Jan 14, 2017
Messages
18,221
Whichever method you use, the dropdown will only appear for the first record.
When you move to the next record, it won't appear unless you add further code.
Which, in a way, comes back to the point raised by @Mike Krailo
 

DataAmatuer

New member
Local time
Yesterday, 18:15
Joined
Feb 15, 2021
Messages
16
While all of this is very nice, the question remains. Why do you need the combo box to dropdown on form load? That just doesn't make any sense to me. That would really annoy me as a user.
For most situations, I think you are right that it would be an annoyance to have the combo box dropdown on form load. But in this case, the combo box is on a form (named “frmInsCoAddEdit”) that is used for only one of two purposes: (1) to add a new insurance company name to a table of names, or (2) to edit the name of an insurance company already in the table. The form is opened by a command button captioned: “Add/Edit Ins Co”. The only reason for activating the button (and thus opening the form) is to do one of those two actions.

Once the form is open, the label next to the combo box explains what to do: ” ENTER new company or SELECT company to EDIT from list”

Since a combo box combines the features of a text box with those of a list box, the user can either immediately start typing the name of a new company into the text box portion of the combo box, or s/he can select an existing name from the list portion of the combo box (which is why I want it to dropdown when the form loads). Then as soon as you leave the box, the list portion closes thus making more space available on the form to see other controls.

Code in the After Update event of the combo box tests to see if a new name was added in the text box portion of the combo, or if an existing name was selected from the list. If a new name was entered, then the code line DoCmd.RunCommand acCmdRecordsGoToNew sets up a text box bound to the table of insurance company names (txtInsName) for entering a new record. But if an existing name is selected from the list, then the code puts that name into txtInsName and unlocks it for editing. In other words, depending on the action done in the combo box, the form is either set up to add a new record or to edit an existing one.

I also thought about other alternatives such as having two buttons – one for adding a name and one for editing an existing name. Or, I could have put an Option Group on the Add/Edit form to choose between adding a new record or editing an existing one. But that just adds the step of selecting an action before getting to the primary purpose of the form – adding or editing a name.

It all seems pretty efficient to me. The form opens, I either enter a new name or select an existing one, and the code in AfterUpdate then sets up the form for adding or editing depending on the action that I did in the combo box.
 

Users who are viewing this thread

Top Bottom