Is there a way to trigger an event only once and then disable it until a new record is created? (1 Viewer)

Sampoline

Member
Local time
Today, 19:44
Joined
Oct 19, 2020
Messages
161
So I have a particular button that starts as disabled when a New Record is created and also when the form is opened. The button only becomes enabled once a certain text field has been updated. So I added an AfterUpdate event on the text field to enable the button. But obviously doing this means, the button will keep refreshing every time that text field is updated. Which I don't want. I just want this event to happen once, and then never again, until a new record is created (I have code for the button to be disabled on New Record created, so this is fine, just the other thing is confusing me).

How would I go about doing this? My head's in a bit of a spin. Any help appreciated.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 01:44
Joined
Oct 29, 2018
Messages
21,357
You could try something like:
Code:
Me.ControlName.AfterUpdate=""
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 16:44
Joined
May 7, 2009
Messages
19,169
so you only enable it once, on Each Record (but not on New Record)?
or if continuous/datasheet form, once in the entire form?
 

Sampoline

Member
Local time
Today, 19:44
Joined
Oct 19, 2020
Messages
161
so you only enable it once, on Each Record (but not on New Record)?
or if continuous/datasheet form, once in the entire form?
Hello, yes, enable once on each record but not on new record
It is a single form
 

theDBguy

I’m here to help
Staff member
Local time
Today, 01:44
Joined
Oct 29, 2018
Messages
21,357
Hi, sorry where am I inserting this particular code?
You could try the AfterUpdate event first. If it doesn't work, you could try a different event.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 16:44
Joined
May 7, 2009
Messages
19,169
you want the command button enabled, when not in new record.
do you want it disabled after pressing that button?
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 16:44
Joined
May 7, 2009
Messages
19,169
does the functionality of the demo same as you want.
 

Attachments

  • enableButton.zip
    31.9 KB · Views: 201

Sampoline

Member
Local time
Today, 19:44
Joined
Oct 19, 2020
Messages
161
does the functionality of the demo same as you want.

Sorry I did a bad job of explaining my last reply.

I want the button to be disabled on form open and new record creation. But when I update a certain text field, the button should be enabled.

So the way I've set up the button so far, is to become enabled when a new record is created. Once the button is clicked, it will disable and only become enabled on new record creation. But I don't want it to run this way. I want the button to remain disabled on new record creation and it should only become enabled when a text field of my choice has been updated. After this text field has been updated ONCE, updating the text field again shouldn't continue attempting to enable the button. Only after a new record is created, should this process be repeated.

Here is how the process should run:
1-Open Form and the button is already disabled (I've already done this on the Form's On-Open event as Me.cmdButton.Enabled = False)
2-A new record is created, button remains disabled
3-When a specific text field is updated, the button is enabled
4-Once the text field has been updated once, the button should not continue running that enabling function over and over again (my form's update events are triggered by a save button that I have already setup)
5-Eventually when I need to use that button for a task on my form, I click that button and it runs the function I have coded it for and then disables
6-Only when a new record is created, the process repeats from step2-

Does that make a bit more sense? I know it's a roller coaster.

P.S. New Record is created from a button I've already made as well
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 16:44
Joined
May 7, 2009
Messages
19,169
another attempt.
initially the button is disabled.
when you change "enable/disable button" textbox, the button is enabled on it's afterupdate event.
it will remain enabled until you click on it.
 

Attachments

  • enableButton.zip
    33 KB · Views: 211

Sampoline

Member
Local time
Today, 19:44
Joined
Oct 19, 2020
Messages
161
another attempt.
initially the button is disabled.
when you change "enable/disable button" textbox, the button is enabled on it's afterupdate event.
it will remain enabled until you click on it.
Thanks for persisting Arnel, this is almost what I needed. I think I can figure it out from here, but you've pretty much solved my issue. Thankyou!
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 04:44
Joined
Feb 19, 2002
Messages
42,970
If you only want the button to be enabled for new records, then incorporate that in your control's AfterUpdate code.
Code:
    If Me.NewRecord Then
        Me.cmdSomename.Enabled = True
    End If

I'm not sure that it should matter if the button is enabled multiple times for a new record but we don't know what the button code does so I can't say for certain. The code above will only enable the button for new records but if the user changes the control multiple times, it would re-enable the button so do you need to prevent that also? If so, you need to create a variable that can be set and check that as well. You would add code to the form's CURRENT event to set the variable to "Allow" if Me.NewRecord is true. Then you would set the value to "Disallow" in the button click event. Then the code in the control's AfterUpdate event would check the variable rather than the new record status. So, you actually need code in THREE events to control the enable status if you only want it to be enabled ONCE for even a new record. If it can be enabled multiple times for a new record, then you don't need the variable. You can just use the NewRecord property and code in TWO events.
 

Sampoline

Member
Local time
Today, 19:44
Joined
Oct 19, 2020
Messages
161
If you only want the button to be enabled for new records, then incorporate that in your control's AfterUpdate code.
Code:
    If Me.NewRecord Then
        Me.cmdSomename.Enabled = True
    End If

I'm not sure that it should matter if the button is enabled multiple times for a new record but we don't know what the button code does so I can't say for certain. The code above will only enable the button for new records but if the user changes the control multiple times, it would re-enable the button so do you need to prevent that also? If so, you need to create a variable that can be set and check that as well. You would add code to the form's CURRENT event to set the variable to "Allow" if Me.NewRecord is true. Then you would set the value to "Disallow" in the button click event. Then the code in the control's AfterUpdate event would check the variable rather than the new record status. So, you actually need code in THREE events to control the enable status if you only want it to be enabled ONCE for even a new record. If it can be enabled multiple times for a new record, then you don't need the variable. You can just use the NewRecord property and code in TWO events.
The button is a start timer. I have the button disabled on Form_Open and will only enable on creating a new record. So I have that part sorted. No issues. I have set button to be only clicked once and then disable after clicked. Then it is re-enabled when a new record is created. I've got this part all okay.

My query here is, instead of the button being enabled on New Record, is there is a way to control the button being enabled from a field like a text box or combo box. Not necessarily choosing anything from the combo box. Even if say, the field is selected, then the button is enabled. I know how to enable it. But the button keeps restarting every time I click the combo box or text field. This means the timer restarts again and again. I just want it to enable once.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 01:44
Joined
Oct 29, 2018
Messages
21,357
The button is a start timer. I have the button disabled on Form_Open and will only enable on creating a new record. So I have that part sorted. No issues. I have set button to be only clicked once and then disable after clicked. Then it is re-enabled when a new record is created. I've got this part all okay.

My query here is, instead of the button being enabled on New Record, is there is a way to control the button being enabled from a field like a text box or combo box. Not necessarily choosing anything from the combo box. Even if say, the field is selected, then the button is enabled. I know how to enable it. But the button keeps restarting every time I click the combo box or text field. This means the timer restarts again and again. I just want it to enable once.
I think somebody already said it that one way to try is to use a flag, so you can tell if the button was already enabled once; and if so, don't enable it anymore.
 

Sampoline

Member
Local time
Today, 19:44
Joined
Oct 19, 2020
Messages
161
I think somebody already said it that one way to try is to use a flag, so you can tell if the button was already enabled once; and if so, don't enable it anymore.
Sounds familiar, but I'm not too accustomed to flags. Is that something to do with the on-dirty event?
 

theDBguy

I’m here to help
Staff member
Local time
Today, 01:44
Joined
Oct 29, 2018
Messages
21,357
Sounds familiar, but I'm not too accustomed to flags. Is that something to do with the on-dirty event?
No. It would be more like a global or form level variable. I would recommend using a TempVars, actually. So, if you want to enable the button, you would check if the flag is set, if not, set it and enable the button, if it's set, then disable the button.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 04:44
Joined
Feb 19, 2002
Messages
42,970
In #13, I gave you directions on how to use a flag.

My questions though are:
1. Why are you making the user press a button at all?
2. Why are you using a Timer event?

You can easily identify the start of a new record by putting code in the Form's BeforeInsert event. Simply create two form level variables named StartTime and EndTime defined as Variant so that you can set them to null.

In the Form's Current event clear the variables:
StartTime = Null
EndTime = Null

In the BeforeInsert event populate the StartTime
StartTime = Now()

In the Form's AfterUpdate event, check to see if StartTime is a valid date
If IsDate(StartTime) Then
EndDate = Now()
Me.TimeInMinutes = DateDif( "n", StartTime, EndTime)
Else
Me.TimeInMinutes = Null
End If
 

Users who are viewing this thread

Top Bottom