473,396 Members | 1,940 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to make combo box dropdown disappear

Hi all,

I have a combo box on a form which drops down once the user has typed into it. I want the dropdown to "disappear" if the user backspaces to an empty string and I thought the best way to do so would be to set the focus to a random text box and then return the focus back straight afterwards. Unfortunately, I keep getting an error saying that Access can't move the focus to the random textbox. I'm convinced it is because I am trying to set the focus back to the combo box before the text box has gained focus but I dont know which event to place the combobox.setfocus in order to switch back to the combobox.

At the moment, the barebones of the code is:

Expand|Select|Wrap|Line Numbers
  1. Private Sub cboLookup_Change()
  2.  
  3. Dim sNewLookup As String
  4.  
  5.         sNewLookup = Nz(Me.cboLookup.Text, "")
  6.         If Len(sNewLookup) <> 0 Then
  7.             Me.Text13.SetFocus
  8.             Exit Sub
  9.         End If
  10.         Me.cboLookup.RowSource = sSQL
  11.         Me.cboLookup.Dropdown
  12.     End If
  13.     bLookupKeyPress = False
  14. End Sub
Expand|Select|Wrap|Line Numbers
  1. Private Sub Text13_GotFocus()
  2. Me.cboLookup.SetFocus
  3. End Sub
It gives the error in the cboLookup_Change on the SetFocus line which makes sense - I just can't work out how to give the text box focus before returning it back to the combo box. All help is most appreciated!
Nov 6 '15 #1

✓ answered by jforbes

I remember this.

It would be nice if you could just call .Dropdown again to rollup the Dropdown list, but it doesn't work even though this would lead you to believe it would:
https://msdn.microsoft.com/en-us/lib.../ff836880.aspx
But it does say the .Dropdown is equivalent to pressing F4 on the Keyboard...
Expand|Select|Wrap|Line Numbers
  1. SendKeys "{F4}"
seems to work pretty well.

I think you could use this also:
Expand|Select|Wrap|Line Numbers
  1. Private Sub cboLookup_Change()
  2.      Dim sSQL As String
  3.      Dim sNewLookup As String
  4.  
  5.      If Not bLookupKeyPress Then
  6.          'If nothing entered, set to empty string
  7.              sNewLookup = Nz(Me.cboLookup.Text, "")
  8.  
  9.  'If length of string is not 0, do stuff
  10.          If Len(sNewLookup) <> 0 Then
  11.              Me.cboLookup.RowSource = sSQL
  12.              Me.cboLookup.Dropdown
  13.  'If length of string is 0, disable the combo box
  14.          Else
  15.              SendKeys "{F4}"
  16.          End If
  17.      End If
  18.      bLookupKeyPress = False
  19.  
  20.  End Sub

3 2742
Okay, I managed to solve it! Just in case anyone ever had the same problem, you don't need to have a separate text box, just need to disable the combo box then re-enable it e.g.

Expand|Select|Wrap|Line Numbers
  1. Private Sub cboLookup_Change()
  2.     Dim sSQL As String
  3.     Dim sNewLookup As String
  4.  
  5.     If Not bLookupKeyPress Then
  6.         'If nothing entered, set to empty string
  7.             sNewLookup = Nz(Me.cboLookup.Text, "")
  8.  
  9. 'If length of string is not 0, do stuff
  10.         If Len(sNewLookup) <> 0 Then
  11.             Me.cboLookup.RowSource = sSQL
  12.             Me.cboLookup.Dropdown
  13. 'If length of string is 0, disable the combo box
  14.         Else
  15.             Me.cboLookup.Enabled = False
  16.         End If
  17.     End If
  18.     bLookupKeyPress = False
  19.  
  20. 'Check status of combo box, re-enable if disable
  21.     If Me.cboLookup.Enabled = False Then
  22.         Me.cboLookup.Enabled = True
  23.         Me.cboLookup.SetFocus
  24.     End If
  25. End Sub
Nov 6 '15 #2
jforbes
1,107 Expert 1GB
I remember this.

It would be nice if you could just call .Dropdown again to rollup the Dropdown list, but it doesn't work even though this would lead you to believe it would:
https://msdn.microsoft.com/en-us/lib.../ff836880.aspx
But it does say the .Dropdown is equivalent to pressing F4 on the Keyboard...
Expand|Select|Wrap|Line Numbers
  1. SendKeys "{F4}"
seems to work pretty well.

I think you could use this also:
Expand|Select|Wrap|Line Numbers
  1. Private Sub cboLookup_Change()
  2.      Dim sSQL As String
  3.      Dim sNewLookup As String
  4.  
  5.      If Not bLookupKeyPress Then
  6.          'If nothing entered, set to empty string
  7.              sNewLookup = Nz(Me.cboLookup.Text, "")
  8.  
  9.  'If length of string is not 0, do stuff
  10.          If Len(sNewLookup) <> 0 Then
  11.              Me.cboLookup.RowSource = sSQL
  12.              Me.cboLookup.Dropdown
  13.  'If length of string is 0, disable the combo box
  14.          Else
  15.              SendKeys "{F4}"
  16.          End If
  17.      End If
  18.      bLookupKeyPress = False
  19.  
  20.  End Sub
Nov 6 '15 #3
Even quicker and neater than my original fix! Thanks for the input jforbes!
Nov 6 '15 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: David | last post by:
Hi, I have a dropdown box on a form in an ASP page which links to the following script. It display the next 20 Tuesdays dates + Tuesday (in words). I would like to have it list Every next...
1
by: Dale Geffs | last post by:
I have a form with a combo box and when it (combo) gets focus I issue a .dropdown. Using A2K and SP1 everything works fine and the list drops down under the control as expected. If I use a PC with...
4
by: Steve Le Monnier | last post by:
I have a textbox with a lookup button facility. What I would like to do is also offer a quick selection facility by having a dropdown list appear under the control once the user has typed a single...
2
by: G .Net | last post by:
Hi Is there a way to prevent the combo displaying its list of items when the "combo arrow" is clicked? The problem I'm trying to resolve is that depending on a Boolean value, which is set...
3
by: Cagey | last post by:
What I'm trying for: If this selection or if click on selection (highlighted line choice/ which ever selection change) w/in query's combo dropdown list box (on Switchboard), then Open in...
2
by: SF | last post by:
Hi, I am new to ASP.NET. I have started to create a new ASP.NET web. I can succesfully insert a table into a form but my form does not look good becuase some filed (from table) are foreigh key...
2
by: chupcha | last post by:
Hello all, I'm very new to java script and I need help. I have a couple of buttons on a page that I need to disappear when one of the buttons is clicked. What I found is this: ...
5
by: manishamca | last post by:
i want to make the chociebox uneditable that is the option selected shuld not change after we click a button... plz help me. Thanx in advance
0
by: PromisedOyster | last post by:
I have a datagridview. One of the database bound columns shows a code. However, when I drop down this combo, I would like to see a code + description as this may help the user select the...
2
by: vishal Rane | last post by:
I want to make combo box non-editable in visual basic. I searched on net and found that there is a property called dropdownstyle. But I searched properties of combo box and I did not get this...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.