Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can i make a button count? 2

Status
Not open for further replies.

LamerThanU

Technical User
Feb 1, 2007
8
US
what i need is to have the button (forms) count the number of times it has been pressed and only allow it to be pressed a certain number of times like 5 times for instance. then ill have it spit out a msgbox or something. i tried a couple things but im pretty new to VBA and didnt produce the results i needed.

thanks in advance for anyones help.
 
Have a look at the Static instruction.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
I won't pretend to be great at coding, but this works:
Code:
Private Sub CommandButton1_Click()
Static cmdCount As Integer

    ' don't forget that this variable (cmdCount) will start at zero
    ' >= 4 will get you 5 button presses
  
    If cmdCount >= 4 Then 
    MsgBox ("Don't press my button!")
    Sheet1.CommandButton1.Enabled = False
    Else
    cmdCount = cmdCount + 1
    Sheet1.CommandButton1.Enabled = True
    End If

End Sub

Hope this helps,

soLjd
 
Private Sub CommandButton1_Click()
Static x As Integer
x = x + 1
If x > 3 Then
MsgBox "you cannot click me no more", vbInformation, "test"
CommandButton1.Enabled = False
End
End If
MsgBox "You clicked me " & x & " times.", vbInformation, "Test"
End Sub
 
soLjd...

your code gave me 8 clicks but eventually did the job if you do...

static cmdCount as Integer...
 
LamerThanU,

If you click the button quickly, it does allow around 8 clicks. However, if you click slowly, the code I wrote will not allow more than 5.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top