Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


stop a second click of a single click command button

Posted on 2009-04-17
Medium Priority
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
  • 4
  • 2
LVL 65

Accepted Solution

rockiroads earned 2000 total points
ID: 24170523
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

by:Craig Yellick
ID: 24170526
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

ID: 31571578
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!
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 65

Expert Comment

ID: 24171013
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

ID: 24173608
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.
''' 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

ID: 24174138
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

ID: 24174149
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Code that checks the QuickBooks schema table for non-updateable fields and then disables those controls on a form so users don't try to update them.
A Case Study of using the Windows API to provide RS232 communications capability in Access without the use of Active-X controls.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

564 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