Lock a command botton linked to "Update query" (1 Viewer)

atol

Registered User.
Local time
Yesterday, 20:43
Joined
Aug 31, 2007
Messages
64
Hi,
I have a pop-up form on which there is a command buton linked to an "update query". Everytime the button is clicked, it makes a record to the table. I want to prevent from someone accidentally clicking twice (or more times) and making more than one record. I was thinking of finding a way to lock the command button after it is clicked once. Then unlocked when the form is closed and opened back on...Is this doable? How could that be done?

Any advice is greatly appreciated.

Rgds.
Atol
 

ajetrumpet

Banned
Local time
Yesterday, 22:43
Joined
Jun 22, 2007
Messages
5,638
Hi,
I have a pop-up form on which there is a command buton linked to an "update query". Everytime the button is clicked, it makes a record to the table. I want to prevent from someone accidentally clicking twice (or more times) and making more than one record. I was thinking of finding a way to lock the command button after it is clicked once. Then unlocked when the form is closed and opened back on...Is this doable? How could that be done?
Cmd's (command buttons) don't have a "lock" property. I would use .visible instead. Set it to false at the end of the code that is behind the click event of the button.
 

atol

Registered User.
Local time
Yesterday, 20:43
Joined
Aug 31, 2007
Messages
64
Adam,
Not sure if I got this right. I added in my code this:
Me!cmdButtonNameHere.Visible = False, and I got a run-time error message '2165': "You can not hide a control that has a focus"..... coding is not my strong skill, and suggestions will be very helpul and appreciated.

Best,
Atol
 

ajetrumpet

Banned
Local time
Yesterday, 22:43
Joined
Jun 22, 2007
Messages
5,638
Adam,
Not sure if I got this right. I added in my code this:
Me!cmdButtonNameHere.Visible = False, and I got a run-time error message '2165': "You can not hide a control that has a focus"..... coding is not my strong skill, and suggestions will be very helpul and appreciated.

Best,
Atol
That error message occured because you put the code in with the onclick event of the button (most likely). The error tells you what to do... ;)

If you do indeed have this code with the button's event, you'll have to move it to a different event entirely. The button always has the focus if the onclick event code is running, no matter what kind of commands are inside of the procedure...
 

atol

Registered User.
Local time
Yesterday, 20:43
Joined
Aug 31, 2007
Messages
64
I see. But is the code correct? What other event can I put it on - like on "dblClick"?...I thought afther the event was over and the entry made - end of story... No more clicks are allowed to me made by the user...perhaps I am going in a wrong direction with my design for this.
 

ajetrumpet

Banned
Local time
Yesterday, 22:43
Joined
Jun 22, 2007
Messages
5,638
I see. But is the code correct? What other event can I put it on - like on "dblClick"?...I thought afther the event was over and the entry made - end of story... No more clicks are allowed to me made by the user...perhaps I am going in a wrong direction with my design for this.
Atol,

The point is that you can't be pulling an object like a command button in two directions at the same time...and no, I don't think the code is correct. It should be this:
Code:
me.cmd.visible = false
Like I said before, if your code is behind the button itself, you cannot issue that command because all of the commands in the code block, regardless of their placement, are still part of that event. And the event was triggered with a click on that button, which means that the focus changed to the button. You'll have to put it somewhere else. It sounds like you are using the button as a confirmation mechanism, but you still want to disable the visibility after the confirmation.

Here are a couple of suggestions for you:
I would issue a message telling the user what will happen after the first click of the button. Then, if they confirm the action, I would either:

1) issue a set of commands that close the form or
2) move the focus to a different control with the last statement of the event code, and then write a separate line of code for that control's "GotFocus" event to disable the visibility of the button (you may just want to adopt this method).

You can go about this a number of ways. I have just given a couple of suggestions here. The case may be that you should redesign your entire form, but I don't know unless I see it, really...
 
Last edited:

missinglinq

AWF VIP
Local time
Yesterday, 23:43
Joined
Jun 20, 2003
Messages
6,420
Your code is correct, Atol, and in fact is identical to ajetrumpet's! And you can place it behind your command button, you simply have to, as has been suggested, move the focus elsewhere first:

Code:
Private Sub cmdButtonNameHere_Click()
  Me.AnyOtherControl.SetFocus
  Me!cmdButtonNameHere.Visible = False
  'Other code for your button
End Sub
 

ajetrumpet

Banned
Local time
Yesterday, 22:43
Joined
Jun 22, 2007
Messages
5,638
Your code is correct, Atol, and in fact is identical to ajetrumpet's! And you can place it behind your command button, you simply have to, as has been suggested, move the focus elsewhere first:

Code:
Private Sub cmdButtonNameHere_Click()
  Me.AnyOtherControl.SetFocus
  Me!cmdButtonNameHere.Visible = False
  'Other code for your button
End Sub
Linq,

I have something to say about your comment. The syntax Me! has NEVER, ever worked for any of my databases. Must I change something in order to see this work!? Why can't someone like me possibly get this to work? :eek: The situation in which I try to use it is correct, so that aint the problem! ;)
 

atol

Registered User.
Local time
Yesterday, 20:43
Joined
Aug 31, 2007
Messages
64
Hey guys,
you probably understand how much help you have provided to me with your comments! Big time ! And I like to give feedback to you too…....
I added some creativity. ..hoping not in a wrong direction...I used this:

Me.txtMyControl.SetFocus
Me!Command18.Enabled = False

I basically disabled the button....the user will just see it in gray.....not sure if there is any hidden negative consequences with this approach, but please tell me....
The visibilty also worked perfecto! ....I was thinking if the users are not aware (or just forget!) that the button "disappears" and become “invisible”, they will freak out...:D:D:D
Thanks again Adam and Linq…!

Best,
Atol
 

ajetrumpet

Banned
Local time
Yesterday, 22:43
Joined
Jun 22, 2007
Messages
5,638
I was thinking if the users are not aware (or just forget!) that the button "disappears" and become “invisible”, they will freak out...:D:D:D
Not a bad idea, because I am aware of the fact as well as you are. And I wouldn't worry about your code, it's fine. :)
 

fibayne

Registered User.
Local time
Today, 05:43
Joined
Feb 6, 2005
Messages
236
have just found and used this thread to disable a command button on a form didnt want users authorising a document more than once, Missinglinq's code and Atol's suggestion to set enable to false were a great help to my db and work perfectly ...thanks once again to this forum ...cheers Fi
 

Users who are viewing this thread

Top Bottom