Disable Command Button After Click

  • Thread starter Thread starter ridgerunner
  • Start date Start date
R

ridgerunner

Many thanks to Crystal, I have a form that will add records with field data
from a table when a Command Button is clicked. What I need to do is disable
the buttom once it has been clicked and the records are added. Otherwise the
user can keep adding the same records. Can someone please tell me how to do
that?
 
Could I use code to move the cursor to the data entry field immediatley after
the records are added. If so, how would that read?
 
Thanks so much for your help. Here is the code.


Private Sub AddQsts_Click()
'save record if changes have been made
If Me.Dirty Then Me.Dirty = False

'if we are on a new record, give user a message
If Me.NewRecord Then
MsgBox "You must complete the store and date" _
, , "Cannot create questions"
Exit Sub
End If

If IsNull(Me.InspID) Then
MsgBox "You must fill out the store and date" _
, , "Cannot create questions"
Me.InspID.SetFocus
Exit Sub
End If

Dim strSQL As String

strSQL = "INSERT INTO tblDMInspecDet (InspID, DMCatID, QstID) " _
& " SELECT " & Me.InspID _
& ", DMCatID " _
& ", QstID " _
& " FROM tblQuestions;"


CurrentDb().Execute strSQL

'make the new records show up on the subform
Me.subfrmDMInspDet.Requery

End Sub


ruralguy via AccessMonster.com said:
Yes. Can you post the code you have in the click event of the CommandButton
please.
Could I use code to move the cursor to the data entry field immediatley after
the records are added. If so, how would that read?
You can not disable the control with the focus so you have no choice but to
move to another control and then disable the CommandButton. You might set a
[quoted text clipped - 7 lines]
user can keep adding the same records. Can someone please tell me how to do
that?

--
RuralGuy (RG for short) aka Allan Bunch MS Access MVP - acXP WinXP Pro
Please post back to this forum so all may benefit.

Message posted via AccessMonster.com
 
Is this going to cause a problem?
IF YOU HAD A DIRTY NEW RECORD THEN IT IS NO LONGER A NEW RECORD BECAUSE OF
THE CODE ABOVE.

The code below:
Me.OtherControlName.SetFocus '-- Use YOUR OtherControlName of course.
Me.AddQsts.Enable = FALSE

is causing an error message "Run-time error '2164' " " You can't disable a
control while it has the focus."

I set the focus to my other control name and the cursor "lands" there but I
cannot add the score.

Thanks.
ruralguy via AccessMonster.com said:
Notes in line:
Thanks so much for your help. Here is the code.

Private Sub AddQsts_Click()
'save record if changes have been made
If Me.Dirty Then Me.Dirty = False

IF YOU HAD A DIRTY NEW RECORD THEN IT IS NO LONGER A NEW RECORD BECAUSE OF
THE CODE ABOVE.
'if we are on a new record, give user a message
If Me.NewRecord Then
MsgBox "You must complete the store and date" _
, , "Cannot create questions"
Exit Sub
End If

If IsNull(Me.InspID) Then
MsgBox "You must fill out the store and date" _
, , "Cannot create questions"
Me.InspID.SetFocus
Exit Sub
End If

Dim strSQL As String

strSQL = "INSERT INTO tblDMInspecDet (InspID, DMCatID, QstID) " _
& " SELECT " & Me.InspID _
& ", DMCatID " _
& ", QstID " _
& " FROM tblQuestions;"

CurrentDb().Execute strSQL

THE ABOVE LINE SHOULD BE:
CurrentDB.Execute strSQL, dbFailOnError

AND YOU SHOULD HAVE ERROR HANDLING CODE IN THIS FUNCTION.
'make the new records show up on the subform
Me.subfrmDMInspDet.Requery

ADD THE FOLLOWING:

Me.OtherControlName.SetFocus '-- Use YOUR OtherControlName of course.
Me.AddQsts.Enable = FALSE
End Sub
Yes. Can you post the code you have in the click event of the CommandButton
please.
[quoted text clipped - 7 lines]
user can keep adding the same records. Can someone please tell me how to do
that?

--
RuralGuy (RG for short) aka Allan Bunch MS Access MVP - acXP WinXP Pro
Please post back to this forum so all may benefit.

Message posted via AccessMonster.com
 
I moved the 'Me.AddQsts.Enable = FALSE' to the 'on enter' property for the
AddQsts field and that works; the button is greyed out and unavailable. I
can add the score to the first record but only after I use my cursor to move
down to the second record and back up to the first record. Can you please
tell me how to fix this?

Many thanks!


ridgerunner said:
Is this going to cause a problem?
IF YOU HAD A DIRTY NEW RECORD THEN IT IS NO LONGER A NEW RECORD BECAUSE OF
THE CODE ABOVE.

The code below:
Me.OtherControlName.SetFocus '-- Use YOUR OtherControlName of course.
Me.AddQsts.Enable = FALSE

is causing an error message "Run-time error '2164' " " You can't disable a
control while it has the focus."

I set the focus to my other control name and the cursor "lands" there but I
cannot add the score.

Thanks.
ruralguy via AccessMonster.com said:
Notes in line:
Thanks so much for your help. Here is the code.

Private Sub AddQsts_Click()
'save record if changes have been made
If Me.Dirty Then Me.Dirty = False

IF YOU HAD A DIRTY NEW RECORD THEN IT IS NO LONGER A NEW RECORD BECAUSE OF
THE CODE ABOVE.
'if we are on a new record, give user a message
If Me.NewRecord Then
MsgBox "You must complete the store and date" _
, , "Cannot create questions"
Exit Sub
End If

If IsNull(Me.InspID) Then
MsgBox "You must fill out the store and date" _
, , "Cannot create questions"
Me.InspID.SetFocus
Exit Sub
End If

Dim strSQL As String

strSQL = "INSERT INTO tblDMInspecDet (InspID, DMCatID, QstID) " _
& " SELECT " & Me.InspID _
& ", DMCatID " _
& ", QstID " _
& " FROM tblQuestions;"

CurrentDb().Execute strSQL

THE ABOVE LINE SHOULD BE:
CurrentDB.Execute strSQL, dbFailOnError

AND YOU SHOULD HAVE ERROR HANDLING CODE IN THIS FUNCTION.
'make the new records show up on the subform
Me.subfrmDMInspDet.Requery

ADD THE FOLLOWING:

Me.OtherControlName.SetFocus '-- Use YOUR OtherControlName of course.
Me.AddQsts.Enable = FALSE
End Sub

Yes. Can you post the code you have in the click event of the CommandButton
please.
[quoted text clipped - 7 lines]
user can keep adding the same records. Can someone please tell me how to do
that?

--
RuralGuy (RG for short) aka Allan Bunch MS Access MVP - acXP WinXP Pro
Please post back to this forum so all may benefit.

Message posted via AccessMonster.com
 
Thanks. This is a tremendous help.

ruralguy via AccessMonster.com said:
In Line:
Is this going to cause a problem?

Only you can know. A record is no longer *new* when it has been saved!
IF YOU HAD A DIRTY NEW RECORD THEN IT IS NO LONGER A NEW RECORD BECAUSE OF
THE CODE ABOVE.

The code below:
Me.OtherControlName.SetFocus '-- Use YOUR OtherControlName of course.
Me.AddQsts.Enable = FALSE

is causing an error message "Run-time error '2164' " " You can't disable a
control while it has the focus."

Let's go with the Public variable idea.
LockTheButton = True
Me.OtherControlName.SetFocus '-- Use YOUR OtherControlName of course.

And then in the OtherControlName GotFocus event:

If LockTheButton Then
Me.AddQsts.Enable = FALSE
End If

YOU WILL NEED TO DEFINE THE VARIABLE BEFORE ANY FUNCTIONS IN THE CODE MODULE.

Dim LockTheButton As Boolean

...and then depending on how your form works you may want to set it false in
the Current event of the form.
I set the focus to my other control name and the cursor "lands" there but I
cannot add the score.

Thanks.
Notes in line:
[quoted text clipped - 51 lines]
user can keep adding the same records. Can someone please tell me how to do
that?

--
RuralGuy (RG for short) aka Allan Bunch MS Access MVP - acXP WinXP Pro
Please post back to this forum so all may benefit.

Message posted via AccessMonster.com
 
I continued to work with it, because the SetFocus to the 'Score' field did
not disable the command button until the Score field was actually clicked,
and the entire set of records could be added over and over, if a user so
desired to create some mischief. Also, the SetFocus to the Score field would
move the cursor to the field but edit mode was not entered until the field
was clicked or the cursor was moved. I added a line of code to set focus to
the subform before setting the focus to the Score field and it works
beautifully. The Add button is immediately disabled and the cursor is
sitting on the Score field, blinking and waiting for data entry.
Thanks for your help.

ruralguy via AccessMonster.com said:
Can I assume all is working correctly now?
Thanks. This is a tremendous help.
[quoted text clipped - 39 lines]
user can keep adding the same records. Can someone please tell me how to do
that?

--
RuralGuy (RG for short) aka Allan Bunch MS Access MVP - acXP WinXP Pro
Please post back to this forum so all may benefit.

Message posted via AccessMonster.com
 
hi friend how are you, i am making a library database and i am struggiling with a problem, in the manager switchboard i added a form that enables manager to edit record but the problem is that i want to make a comman that enables the field to be edited and then another command that disables the fields after editing, (it deal with vba i could be gratefull if u send me the code or teach me how to use it) can u plz help me in any means, i really need help to pass my ITGS subject in IB and get a good grade, i would really appreciate ur help

thank you
 
Back
Top