Solved How to Undo Button Press (1 Viewer)

Bean Machine

Member
Local time
Today, 16:12
Joined
Feb 6, 2020
Messages
102
Hi everyone. This is a super generic and frankly probably a very dumb question but I'll ask it for the sake of finding out. I have a button that enables editing on a form and when clicked the text within will change to "Editing". What I would like is once a user is done editing, for them to click the button and have it return to it's default state again. Here is my code:

Private Sub btn_Edit_Click()
Me.AllowEdits = True
Me.btn_Edit.Caption = "Editing"
End Sub
Private Sub Form_Load()
Me.AllowEdits = False
Me.btn_Edit.Caption = "Edit"
End Sub

Fairly simple function, I just don't know how to get it to revert to it's initial state (that is to say what it was on form load). Thanks for any help provided!
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 13:12
Joined
Aug 30, 2003
Messages
36,125
You can test the caption and set the values one way or the other depending on its current value.
 

zeroaccess

Active member
Local time
Today, 15:12
Joined
Jan 30, 2020
Messages
671
I do the same thing - those with privileges will be able to click "Edit" which enables edits and additions. I don't bother with a way to change them back to disabled because that is the default state that the form opens in - it will open with edits and additions disabled. Is there a reason why you need this?
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 15:12
Joined
Feb 28, 2001
Messages
27,175
I usually kept a Boolean variable in the declaration area of the form's Class Module and used that to remember the current state of the button. Then I just flip-flopped it at the beginning of the OnClick routine and use an IF-THEN-ELSE-END IF structure to control which way it acted. This included changing captions, changing form settings, whatever else was implied by whatever that button was enabling or disabling. Some folks prefer to look at a label. Others could look at the form's current AllowEdits flag and flip-flop that state. As long as nothing else would diddle with the form's abilities like that, almost any choice would work.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 13:12
Joined
Oct 29, 2018
Messages
21,469
Hi. Another option is to use a Toggle Button. Just a thought...
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 15:12
Joined
Feb 28, 2001
Messages
27,175
Good idea, TDBG. I rarely used them because my buttons were multi-function. They cycled through as many as four different states sometimes. Toggles won't do that.
 

Bean Machine

Member
Local time
Today, 16:12
Joined
Feb 6, 2020
Messages
102
How should I go about using the toggle button? I've looked up a few things and its still not as clear as I would have hoped. In my form Current I set the caption for the button to be "Edit" and allowEdits = False. I tried using AfterUpdate to change the caption and editing permissions like so:

If Me.btn_Edit.Caption = "Edit" Then
Me.btn_Edit.Caption = "Editing"
Me.AllowEdits = True
Else
Me.btn_Edit.Caption = "Edit"
Me.AllowEdits = False
End If

I don't know what the issue is but I can't get it to work the way I had hoped. Any help would be greatly appreciated!
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 16:12
Joined
May 21, 2018
Messages
8,527
Here is the problem you are having. To lock it back down you need a line of code
me.dirty = false.
If you want to prevent the user from making changes to a record (AllowEdits is No) that you need to edit programmatically, save the record after any programmatic changes; the AllowEdits property setting will be honored once again after any unsaved changes to the current record are saved.
Code:
Private Sub cmdEdit_Click()
  If cmdEdit.Caption = "Edit" Then
    cmdEdit.Caption = "Editing"
  Else
    cmdEdit.Caption = "Edit"
    Me.Dirty = False
  End If
   Me.AllowEdits = (cmdEdit.Caption = "Editing")
End Sub

Private Sub Form_Current()
 Me.AllowEdits = False
 Me.cmdEdit.Caption = "Edit"
End Sub
 

Bean Machine

Member
Local time
Today, 16:12
Joined
Feb 6, 2020
Messages
102
Here is the problem you are having. To lock it back down you need a line of code
me.dirty = false.

Code:
Private Sub cmdEdit_Click()
  If cmdEdit.Caption = "Edit" Then
    cmdEdit.Caption = "Editing"
  Else
    cmdEdit.Caption = "Edit"
    Me.Dirty = False
  End If
   Me.AllowEdits = (cmdEdit.Caption = "Editing")
End Sub

Private Sub Form_Current()
Me.AllowEdits = False
Me.cmdEdit.Caption = "Edit"
End Sub

This seems like it should work but it just didn't. I even changed the name of the button to what you have but to no avail. I will post a copy of my database if you would like to take a look. The button is found on the Add or Remove Users form. Let me know what you find! Thanks!
 

Attachments

  • Loans Database Sample.zip
    935.9 KB · Views: 88

theDBguy

I’m here to help
Staff member
Local time
Today, 13:12
Joined
Oct 29, 2018
Messages
21,469
Hi Bean. I have a quick question. Is your original button working as you have described in your original post? You said the form opens basically as "read only," and when the user "clicks" on your button, then they can make changes to the data on the form. Is that really what is currently happening with your form? Just trying to clarify something...
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 13:12
Joined
Aug 30, 2003
Messages
36,125
I changed to a regular button and your code appears to work. I suspect since a toggle button has a value, it can't be changed when the form doesn't allow edits.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 13:12
Joined
Oct 29, 2018
Messages
21,469
I changed to a regular button and your code appears to work. I suspect since a toggle button has a value, it can't be changed when the form doesn't allow edits.
Makes sense. Thanks! 👍
 

zeroaccess

Active member
Local time
Today, 15:12
Joined
Jan 30, 2020
Messages
671
I find it easier to use Select Case statements with toggle buttons.
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 13:12
Joined
Aug 30, 2003
Messages
36,125
Glad you got it sorted. A toggle button is a good tool to have, I just don't think it works here because of the edit status. A toggle button can be bound, and it has a value of True or False depending on it's toggled state. That's why I think that when the form doesn't allow edits, the toggle button can't be changed and doesn't even respond to clicks.
 

zeroaccess

Active member
Local time
Today, 15:12
Joined
Jan 30, 2020
Messages
671
Glad you got it sorted. A toggle button is a good tool to have, I just don't think it works here because of the edit status. A toggle button can be bound, and it has a value of True or False depending on it's toggled state. That's why I think that when the form doesn't allow edits, the toggle button can't be changed and doesn't even respond to clicks.
I thought I was using a toggle button, but it's actually an Option Group using regular-looking buttons rather than radio buttons. It can do more than just True/False. Here's mine:

SQL:
Private Sub frameInspectionFilter_Click()
    Select Case Me.frameInspectionFilter.Value
        Case 1
            Me.RecordSource = "qryA"
        Case 2
            Me.RecordSource = "qryB"
        Case 3
            Me.RecordSource = "qryC"
    End Select
    Me.Requery
End Sub

Each "Case" is when one of the 3 buttons is clicked. Only one may be depressed at a time.

The onclick event is on the frame surrounding the buttons.

You could take a similar approach to enable/disable properties.
 
Last edited:

pbaldy

Wino Moderator
Staff member
Local time
Today, 13:12
Joined
Aug 30, 2003
Messages
36,125
I've used option groups many times, but I don't think one would work here. It would have the same limitation as the toggle button, not being editable when AllowEdits was False.

By the way, in your code the requery is probably not necessary. I never include it when setting the RecordSource property.
 

Users who are viewing this thread

Top Bottom