Access vba listbox double click

  1. New Member
    Good morning fellow VBA developers,

    In my Access database, I currently have two events set up for one of my listboxes. One is a Click event which updates some labels and a subform. The other is a DblClick event that opens a modal form. The two events work fine when alone, but the DblClick event does not fire when both events are in place. I understand that Access is reading the click event twice, rather than triggering the DblClick. So my question is... what is a good way to identify a double click has occurred and separating the two events?

    Here's the code:

    DblClick Event

    Private Sub lstBusinessUnits_DblClick[Cancel As Integer] 'reload controls Call LoadControls On Error GoTo ErrorHandler 'populate global variable intBusinessUnitID = Me.lstBusinessUnits 'open form DoCmd.OpenForm "frmBusinessUnitDetails" ErrorHandler: Dim strProc As String strProc = "lstBusinessUnits_DblClick" Call RecordError[Err.Number, Err.Description, strProc, Me.Name] Exit Sub End Sub

    Click Event
    Private Sub lstBusinessUnits_Click[] On Error GoTo ErrorHandler 'reload controls Call LoadControls ErrorHandler: Dim strProc As String strProc = "lstBusinessUnits_Click" Call RecordError[Err.Number, Err.Description, strProc, Me.Name] Exit Sub End Sub
  2. Re: Conflict between Click and DblClick Event

    You could have a global counter variable and increment it upon each click. When it hits 2, fire the activity you want tied to the double click, then reset it to zero.
  3. Re: Conflict between Click and DblClick Event

    i tested on a msforms listbox [which may not be the same as an access listbox]
    when double clicking, a click event fires first then the dblclick event
    any other events initiated from the click event could affect the dblclick event
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  4. Re: Conflict between Click and DblClick Event

    does indeed fire both events

    if you arent using the timer event, put your click event code in there and use something like the following to trigger it

    Private Sub Form_Timer[] Me.TimerInterval = 0 txtD = txtD + " Tclick " End Sub Private Sub Lista0_Click[] Me.TimerInterval = 500 End Sub Private Sub Lista0_DblClick[Cancel As Integer] Me.TimerInterval = 0 txtD = txtD + " doubleclick " End Sub
    In my test txtd is a textbox
    lista0 is a listbox

    Dont know if that will help...
    Alternatively, put a timer event and hold a form variable 'action' string.
    The timer event fires after the 500 [single click] and sets the string then calls the sub
    the double click sets the string and calls the sub


    Feeling like a fly on the inside of a closed window [Thunk!]
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  5. Re: Conflict between Click and DblClick Event

    does vba have a timer control?

    i did think of having an application.wait in the click event to see if the double click fired, it seems to work, but as the minimum value is 1 second, probably have to use an api timer, which would be much more efficient anyway

    note also if the mouse is over a empty area in the list box the click event does not fire, only the double_click, the click event only fires when list item is clicked

    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  6. New Member

    Re: Conflict between Click and DblClick Event

    Well, this has been an interesting week in learning new lessons. So I learned that you can indeed have a click event and dblclick event on the same control, however you have to implement a timed delay [I'm doing it for 1 second]. This delay, using a WindowsAPI, somehow gets the event to fire. I snagged this code from another forum and put it to the test. It's a little clunky though, as the delay is for 1 second. Perhaps I can use milliseconds instead of seconds?
    Private Declare Sub Sleep Lib "kernel32" [ByVal lngMilliSeconds As Long] Public Sub WaitSeconds[intSeconds As Integer] ' Comments: Waits for a specified number of seconds ' Params : intSeconds Number of seconds to wait ' Source : Total Visual SourceBook On Error GoTo PROC_ERR Dim datTime As Date datTime = DateAdd["s", intSeconds, Now] Do ' Yield to other programs [better than using DoEvents which eats up all the CPU cycles] Sleep 100 DoEvents Loop Until Now >= datTime PROC_EXIT: Exit Sub PROC_ERR: MsgBox "Error: " & Err.Number & ". " & Err.Description, , "modDateTime.WaitSeconds" Resume PROC_EXIT End Sub
  7. New Member

    Re: Conflict between Click and DblClick Event

    Originally Posted by westconn1

    does vba have a timer control?

    i did think of having an application.wait in the click event to see if the double click fired, it seems to work, but as the minimum value is 1 second, probably have to use an api timer, which would be much more efficient anyway

    note also if the mouse is over a empty area in the list box the click event does not fire, only the double_click, the click event only fires when list item is clicked


    VBA does have an intrinsic function, application.wait. However, it is only for Excel and not access [go figure!]
  8. Re: Conflict between Click and DblClick Event

    i would think an api timer would be better than application.wait or sleep as they both suspend all processing, a timer with callback, should allow other processing to continue, while the timer suspends some specific code for a few milliseconds
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Forum Rules

Click Here to Expand Forum to Full Width

Video liên quan

Chủ Đề