stop a second click of a single click command button

Posted on 2009-04-17
Last Modified: 2013-11-28
How can I stop a single click command button starting the same process if it is double clicked.
Question by:mdlp
    LVL 65

    Accepted Solution

    if this button starts a process, do you know when it will stop?

    suggestion would be to creat a form level variable

    dim bClicked as boolean

    when the form loads (form_load) or a record is loaded (form_current) then initialise this

    bClicked = false

    Now on the click event, set it to true

    private sub mybtn_click()

        'Check if already set
        if bClicked then exit sub

        bClicked = true

        'now run your stuff

        'Set it back to false if you want to allow clicking again
        bClicked = false
    end sub
    LVL 11

    Expert Comment

    In the click event you can disable the command button, that way it is not available for a second click.

        sub someButton_Click ...
           someButton.enabled = false
           someButton.enabled = true
        end sub

    Author Closing Comment

    I am not opening a form just running some code and a shell command, so based on what you provided I created a public Var and I used your same logic so that the only button that could work is that one and only one time until I set it back, great job.  I will be able to use this many more times!!!!!
    CraigYellick, I tried your suggestion because it seemed easier, but it was the same as when I tried to change the visible property, you can not use it because I still have only one form, and it has focus, but thank you for your suggestion!
    LVL 65

    Expert Comment

    I would be careful if you are going to use Shell this is because process would still be running whilst shell command would of exited. There is ShellWait code around here, it is just like Shell, except it waits to be completed. Search for it here and see if that helps you


    Author Comment

    I have found a shell wait that seems to work, thanks for your concern!  Have a look, it was the only one that worked right out of the "box"
    cheers, mate
    ''' *************************************************************************
    ''' Module Constant Declaractions Follow
    ''' *************************************************************************
    ''' Constant for the dwDesiredAccess parameter of the OpenProcess API function.
    Private Const PROCESS_QUERY_INFORMATION As Long = &H400
    ''' Constant for the lpExitCode parameter of the GetExitCodeProcess API function.
    Private Const STILL_ACTIVE As Long = &H103
    ''' *************************************************************************
    ''' Module Variable Declaractions Follow
    ''' *************************************************************************
    ''' It's critical for the shell and wait procedure to trap for errors, but I
    ''' didn't want that to distract from the example, so I'm employing a very
    ''' rudimentary error handling scheme here. This variable is used to pass error
    ''' messages between procedures.
    Public gszErrMsg As String
    ''' *************************************************************************
    ''' Module DLL Declaractions Follow
    ''' *************************************************************************
    Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
    Private Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, lpExitCode As Long) As Long
    Public strIsrunning As String
      Private Function bShellAndWait(ByVal szCommandLine As String, Optional ByVal iWindowState As Integer = vbHide) As Boolean
    ''' Comments:   Shells out to the specified command line and waits for it to
    '''             complete. The Shell function runs asynchronously, so you must
    '''             run it using this function if you need to do something with
    '''             its output or wait for it to finish before continuing.
    ''' Arguments:  szCommandLine   [in] The command line to execute using Shell.
    '''             iWindowState    [in] (Optional) The window state parameter to
    '''                             pass to the Shell function. Default = vbHide.
    ''' Returns:    Boolean         True on success, False on error.
    ''' Date        Developer       Action
    ''' --------------------------------------------------------------------------
    ''' 05/19/05    Rob Bovey       Created
        Dim lTaskID As Long
        Dim lProcess As Long
        Dim lExitCode As Long
        Dim lResult As Long
        On Error GoTo ErrorHandler
        ''' Run the Shell function.
        lTaskID = Shell(szCommandLine, iWindowState)
        ''' Check for errors.
        If lTaskID = 0 Then Err.Raise 9999, , "Shell function error."
        ''' Get the process handle from the task ID returned by Shell.
        lProcess = OpenProcess(PROCESS_QUERY_INFORMATION, 0&, lTaskID)
        ''' Check for errors.
        If lProcess = 0 Then Err.Raise 9999, , "Unable to open Shell process handle."
        ''' Loop while the shelled process is still running.
            ''' lExitCode will be set to STILL_ACTIVE as long as the shelled process is running.
            lResult = GetExitCodeProcess(lProcess, lExitCode)
        Loop While lExitCode = STILL_ACTIVE
        bShellAndWait = True
        Exit Function
        gszErrMsg = Err.Description
        bShellAndWait = False
    End Function

    Open in new window

    LVL 65

    Expert Comment

    Yes, thats the one that looks right. Now your code will definitely wait for your shelled process to complete before it activates the button.
    LVL 65

    Expert Comment

    I was thinking about this, especially after Craig suggested the button.

    You could try changing the caption instead of using a variable

    public sub mybtn_Click()
        if mybtn.caption = "Waiting..." then exit sub
        mybtn.caption = "Waiting..."

        ShellAndWait code line here

        mybtn.caption = "Original Caption Here"
    end sub

    advantage of this is user is aware that the button has been clicked and is doing something

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    It took me quite some time to sort out all the different properties of combo and list boxes available from Visual Basic at run-time. Not that the documentation is lacking: the help pages are quite thorough and well written. The problem was rather wh…
    This isn't a frequent question on EE. I must have seen it three or four times (among several thousand questions). However, I use this trick quite often, most frequently as a delayed Current event. A form does not expose it's calculation dependenc…
    As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
    Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now