Sub running twice for one click

RayH

Registered User.
Local time
Today, 00:06
Joined
Jun 24, 2003
Messages
132
Can someone test this for me?
I have created a tiny sub that increments a value on the form.
Each time the button is clicked the underlying sub runs twice causing the counter to add 2 instead of 1.

Note: I have not used the standard On_Click sub I've made my own.
Thanks
 

Attachments

It's a strange one, and I'm not sure why it's incrementing by 2's, but as a fix...

delete =incCounter(1) ... from the On_Click event

...go into the code view of the On_Click event for the command button

...enter... Call IncCounter(1)


..then it works
 
Try changing Sub to Function…that should do it.

Regards,
Chris.
 
ChrisO, why was it incrementing by 2, why did what i said above also put it right??
 
I don’t know but it has done that since at least Access 97.

So I simply use a Function which overcomes the problem. The function needs no return value, actually it’s a variant if not stated, but the return value gets trashed anyway so it doesn’t matter.

Sorry…I can’t explain it better. (Read Bug???)

Regards,
Chris.
 
If you just take out the "=Inccounter(1)" from the OnClick event and change it to Event Procedure and then put the sub call in that event, it works just fine.

Code:
Public Sub IncCounter(c As Integer)
Dim x As Integer

Let x = Val(myCounter.Caption)
Let x = x + c
myCounter.Caption = x

End Sub

Private Sub Command1_Click()
    IncCounter (1)
End Sub

At least it does for me. I'm running Access 2003.
 
Thanks for the solutions guys. They all work.
This has been one of those things I've often wondered why it happens.
I think its a bug too, unless anyone else has an explanation why it happens.
Is this documented anywhere? I couldn't find anything about it in the MS knowledgebase.
 

Users who are viewing this thread

Back
Top Bottom