disable command button after click

apprentice

Registered User.
Local time
Today, 15:03
Joined
Jan 13, 2015
Messages
27
I have a form that are filled by end users and the supervisor verifies the data by ticking a "Completed" checkbox then click on a command button (Post) to post the data into another table.

However, i would like the Post button to be disable after each record is posted and remains disabled. My codes does not allow my Post button to remain disabled. Whenever the form is reopened the button is enabled automatically.

I will greatly appreciate if anyone can assist me.

Codes used;



Private Sub Command297_Click()

On Error GoTo err_handler
If Me.chkCompleted Then
DoCmd.RunCommand acCmdSaveRecord
DoCmd.SetWarnings (False)
DoCmd.OpenQuery "InspecUpdate", acViewNormal
Me.Requery
DoCmd.SetWarnings (True)
MsgBox "Post Successful!"
Me.Command297.Enabled = False
Else
Me.Command297.Enabled = True

MsgBox "You cannot transfer this record until "Completed is checked"
End If
Exit Sub
err_handler:
DoCmd.SetWarnings (True)
MsgBox Err.Description, vbExclamation, "Error Number: " & Err.Number
Exit Sub
End Sub
 
You'd have to set focus elsewhere to disable it. You can't disable a control that has focus.
 
Fine point to understand... you CANNOT disable the command button in that context without other actions. You see, when you clicked it, you gave it focus, and you cannot disable the control that currently still has focus.

You COULD try to give focus to another control (via controlname.setfocus if nothing else). However, the way I did this was to prevent the button from being enabled until it was primed for action by other control steps.

I usually wrote a subroutine that was easily called from control.LostFocus routines to decide if the control contained a usable value. If ALL contributing controls had values then the routine would enable the "SAVE" button. Then in the AfterUpdate routine I would include a [HELP].SetFocus (because I had a HELP button that never went away no matter what else you did) followed by resetting the SAVE and UNDO buttons. Of course there was nothing to do or to undo after the save occurred.

Once you have this button-control subroutine, you can call it from the Form_Current routine, the Form_Load routine, or anyplace else you think you need it. OK, it is a pain in the toches to do this once. But once you do it, (A) you can call it from any event on the form and (B) it is trivial to clone the routine to customize it for every form you've got, regardless of the specific criteria needed for enabling your command buttons.
 
Thank you for the reply but i am a bit new to access programming and i am lost.
 
Is it possible to allow the command button to be clicked once and anytime the form is reopened and the user click the button again it prompts the user that the button has been clicked already. example, this record have been submitted already.
 
I think the problem you are trying to solve needs to be triggered from two possible events,
  • a button click
  • a new record being loaded into the form
Imagine the current record has a field called IsSubmitted. When you click the button you set the value of the field to True, and possibly reset the enabled state of the button, but you also need to set the enabled state of the button for each record you load. See how in the following code there is one routine that sets the button enabled state, and it is called from both the button click and the current event...
Code:
private sub cmdSetSubmitted_Click()
[COLOR="Green"]   'set the value of the field[/COLOR]
   me.IsSubmitted = True
[COLOR="Green"]   'ensure button enabled state accurately matches the state of the record[/COLOR]
[COLOR="Blue"]   SetButtonState[/COLOR]
end sub

private sub Form_Current()
[COLOR="Green"]   'ensure for each new record loaded the button state accurately matches record[/COLOR]
[COLOR="Blue"]   SetButtonState[/COLOR]
end sub

private sub [COLOR="Blue"]SetButtonState[/COLOR]
[COLOR="Green"]   'check state of IsSubmitted field[/COLOR]
   if me.IsSubmitted then
[COLOR="Green"]      'ensure the focused control is not the button[/COLOR]
      if me.activecontrol Is me.cmdSetSubmitted then me.someothercontrol.setfocus
[COLOR="Green"]      'disable the control[/COLOR]
      me.cmdSetSubmitted.Enabled = false
   else
      me.cmdSetSubmitted = True
   end if
end sub
This way both of your actions that may alter the enabled state of the button are handled.

Makse sense?
 
Markk,

On what event do i have to insert the last trunk of codes?
 
No event. See the blue lines of code? They both call the Sub in question. This way you can run the SetButtonState routine from two different event handlers, like I was saying. You need to run the button enabler after you click the button AND when you load a new record in the in the form, so we call the single routine from both events.
 
Last edited:
Markk,

I am getting an error for the On_Current event:
"Identifies under cursor is nor recognized"
 
What is the code? What line causes the error, or how do you cause the error?
 
Ok, and what is the code for SetButtonState? Did you just copy the code I posted? If so, you need to rename everything to work with your system, like it seems unlikely you have a field in your table called IsSubmitted, or a button on your form named cmdSetSubmitted.

The code I posted expresses the pattern of what the solution will look like. It is not a turn-key solution for your situation.
 
I did tailored it to reflect my DB. I am not sure why i am getting that error message.
 
OK. So what is the code? What line causes the error?
 
Below is the codes i have for my post/submit command button:

Private Sub Command297_Click()

On Error GoTo err_handler
'Check completed check box
If Me.chkCompleted Then
SetButtonState
DoCmd.RunCommand acCmdSaveRecord
DoCmd.SetWarnings (False)
'runs append query to post data
DoCmd.OpenQuery "InspecUpdate", acViewNormal
Me.Requery

DoCmd.SetWarnings (True)
MsgBox "Post Successful!"

MsgBox "You cannot transfer this record until "Completed is checked"
End If
Exit Sub
err_handler:
DoCmd.SetWarnings (True)
MsgBox Err.Description, vbExclamation, "Error Number: " & Err.Number
Exit Sub
End Sub


Below is the codes i have for On Current event:

[FONT=&quot]Private Sub Form_Current( ) "Here is where i am getting the error"[/FONT]
[FONT=&quot]SetButtonState[/FONT]
[FONT=&quot]End sub[/FONT]


This is what i have as a module:

[FONT=&quot]Private Sub SetButtonState[/FONT]
[FONT=&quot] If Me.ChkComplete then[/FONT]
[FONT=&quot] If Me.activecontrol Is Me.[/FONT][FONT=&quot]Command297 then me.txtshift.Setfocus[/FONT]
[FONT=&quot] [/FONT][FONT=&quot][FONT=&quot]Me.[/FONT][FONT=&quot]Command297[/FONT].Enabled = False[/FONT]
[FONT=&quot] else[/FONT]
[FONT=&quot] me.[/FONT][FONT=&quot][FONT=&quot]Me.[/FONT][FONT=&quot]Command297[/FONT]d = True[/FONT]
[FONT=&quot] End If[/FONT]
[FONT=&quot]End Sub[/FONT]
 
The SetButtonState subroutine needs to be in the same module as the other routines.

There also appear to be a bunch of spelling problems, like in the button click handler you refer to Me.chkCompleted, but the SetButtonState you refer to Me.ChkComplete.

There is this...
Code:
Me.Command297.Enabled = False
else
[COLOR="Blue"]me.[/COLOR]Me.Command297[COLOR="Blue"]d [/COLOR]= True

Also, I don't see in you button click handler where you set the value of the Me.chkCompleted. Theoretically that routine should set that value to true. Possibly your InspecUpdate query does that job???

Hope this helps,
 
Please confirm that the procedure SetButtonState is in the form's module and not in its own module.

Sent from my SM-T530NU using Tapatalk
 
For progress tracking, it's usually better/easier to a have a single 'status' field.

0 = start of process
1 = checked
2 = authorised

Authorisers don't see 0 status records
checkers don't see level 1 or 2 records

Since the records are filtered there's no need to lock anything.

To accept and pass to the next level it's simply status=status+1 or -1 to reject and send it back.

Your scenario might use slightly different terms, but you should get the idea.
 
I finally got through. On the open form event i set the focus to another field and on the current event i let the codes check to see if the check box (completed) is checked then disable the command button....

Thank you everyone for your time and effort!
 

Users who are viewing this thread

Back
Top Bottom