Results 1 to 2 of 2
  1. #1
    benthamq is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Aug 2011
    Posts
    32

    Easy Solution to Prevent Combobox Drop-down

    I struggled for some time attempting to find a way to prevent a combobox drop-down menu from deploying when clicked without disabling the combobox or automatically setting the focus to another control when the combobox enter event is raised. (Most solutions I found by searching involve things like setting the focus on another control anytime the combobox's enter or click events occur, or more complicated alternatives involving additional hidden controls and/or disabling the combobox in some way.)
    In the particular case I was working on, none of these options was acceptable because I needed the control to remain enabled and retain the focus, yet, when a certain condition was met, I still didn't want the combobox to deploy even when the down-arrow was clicked.

    I stumbled upon an extremely simple solution, which I have copied below and explained.



    I imagine that this is not a very common requirement and also that this solution or something similar might be old news to more experienced users but, even so, I didn't find anything quite like it by searching so I am sharing it and I do hope someone finds it useful in the future.

    Code:
    'PURPOSE: prevent a combobox drop-down menu from deploying when the drop-down square/arrow is clicked, but without
    'disabling the combobox and still enabling it to retain focus when the "text part" of the combobox to the
    'left of the drop-down square is clicked
    
    'EXPLANATION: The combobox in this example is called, cmbVendor.  The parent form contains a
    'hidden (but not invisible) textbox called, txtH.  This tiny piece of code is placed in the combobox's mouseDown event.
    'When the drop-down arrow (or anywhere in the small square that contains the dropdown arrow) is clicked,
    'the drop-down will NOT deploy and the focus will automatically switch to the hidden textbox, txtH.
    'However, if the user clicks in the text part of the combobox to the left of the drop down, the combobox
    'will still get the focus.
    'The value of the parameter X is the horizontal coordinate in twips of where you click, every time the combobox mousedown
    'event occurs.  All that is necessary is to figure out the X coordinates of the leftmost and rightmost extremes
    'of just the drop-down square and then test if X is > the leftmost edge or <= the rightmost and switch the 
    'focus if it is.  In the example below, the leftmost is 5745 twips and the rightmost is 6000.
    '-----------------------------------------------------------------------------------------------------------
    
    
    
    
    Private Sub cmbVendor_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    
    If X >= 5745 And X <= 6000 Then
        Me.txtH.SetFocus
    
        'You can use the line below to determine the coordinates.  Remove the comment, go to Access and switch the form to form view. 
        'carefully click around the left and right edges of the drop-down square to determine its left and right extremes
        'Msgbox X
    End If
    
    End Sub

  2. #2
    jfhwalker is offline Novice
    Windows 7 64bit Access 2003
    Join Date
    Jan 2013
    Posts
    1
    Code:
    'PURPOSE: to detect when a combobox drop-down square/arrow is clicked, before the drop down list is displayed
    'without having to 'guess' the width of the combobox arrow
    
    Private Declare Function WindowFromPoint Lib "user32" (ByVal xPoint As Long, ByVal yPoint As Long) As Long
    Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTL) As Long
    Private Declare Function apiGetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassname As String, ByVal nMaxCount As Long) As Long
    Private Const ACC_CBX_EDIT_CLASS = "OKttbx" '  class name for Edit controls in Access
    Private Type POINTL
        X As Long
        Y As Long
    End Type
    
    Private WithEvents mCB As Access.ComboBox
    
    Public Sub SetControl(ByVal oComboBox As Access.ComboBox)
        
        Set mCB = oComboBox
        mCB.OnMouseDown = "[Event Procedure]" 'Enable combobox events
            
    End Sub
    
    Private Sub mCB_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
        If fCBDownArrowClicked Then
            'Do Something here
        End If
    End Sub
    
    Private Function fCBDownArrowClicked() As Boolean
    Dim lRet As Long
    Dim hwnd As Long
    Dim pt As POINTL
    
        'Find current Cursor position
        lRet = GetCursorPos(pt)
        
        'Get this point's Window
        hwnd = WindowFromPoint(pt.X, pt.Y)
        
        'Class Name is 'oFormChild' if CB down arrow was clicked in a single or continuous form
        '                or 'oGrid" if CB down arrow was clicked in a datasheet form
        'or 'OKttbx' if CB Edit Box (the bit to the left of the arrow) was clicked
        If fGetClassName(hwnd) <> ACC_CBX_EDIT_CLASS Then cbDownArrowClicked = True
    
    End Function
    
    Private Function fGetClassName(hwnd As Long)
    Dim strBuffer As String
    Dim lngLen As Long
    Const MAX_LEN = 255
        strBuffer = Space$(MAX_LEN)
        lngLen = apiGetClassName(hwnd, strBuffer, MAX_LEN)
        If lngLen > 0 Then fGetClassName = Left$(strBuffer, lngLen)
    End Function

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 3
    Last Post: 12-30-2011, 01:20 PM
  2. Replies: 3
    Last Post: 11-29-2011, 07:01 AM
  3. Prevent Blank Subreports (possible solution)
    By gopherking in forum Reports
    Replies: 0
    Last Post: 10-18-2011, 11:18 AM
  4. Replies: 0
    Last Post: 03-29-2011, 09:37 AM
  5. Best solution
    By geeka in forum Access
    Replies: 1
    Last Post: 12-04-2006, 01:12 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums