Solved Is there a way to delay a button from being enabled? (1 Viewer)

Sampoline

Member
Local time
Today, 14:10
Joined
Oct 19, 2020
Messages
161
Just really curious. Like I know there is no Sleep/Wait/Pause functions in VBA. But is it possible to maybe try a DoEvents with While loop to delay a button from being enabled?

Because say I had the following code which has a button disabled from another event. Say that once I click on New Record, the button enables:

Code:
Private Sub cmdNewRecord_Click
DoCmd.GoToRecord , , acNewRec
If Me.NewRecord Then
Me.cmdbutton.Enabled = True
End If
End Sub

But what if I wanted the button to be enabled on new record only after a certain amount of time. For example 5 seconds? Essentially delaying the Me.cmdButton.Enabled = False code.

I mean I know I can create a Sleep module and then say Sleep 5000 before the code.

Code:
Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Code:
Sleep 5000
Me.cmdButton.Enabled = False

But this would simply freeze the form for 5 seconds right? DoEvents would be the more logical way of doing it?

Just a thought crossing my mind for the sake of learning more about VBA. Thanks.
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 21:10
Joined
Oct 29, 2018
Messages
21,455
Have you considered using a Form Timer event?
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Yesterday, 23:10
Joined
Feb 28, 2001
Messages
27,148
There is no way to do what you want without either freezing the form for 5 seconds OR by setting a timer on the form for 5000 ('cause the form timers are milliseconds) and then have the form's timer event enable the button. You don't have to re-enable the timer within itself for a single-shot event such as you are describing. Windows and its apps are EVENT driven so if you don't want to do a full 5-second freeze, you need to give it an event to work with. And the timer event is the closest you would have to meeting your needs.
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 21:10
Joined
Oct 29, 2018
Messages
21,455
Hi, I did think of that, but how would I go about writing in the Timer event?
If you only want to enable the button for new records, you might try the following approach.

In the Current Event.
Code:
Me.ButtonName.Enabled=False
If Me.NewRecord Then
    Me.TimerInterval=5000
End if

In the Timer Event.
Code:
Me.TimerInterval=0
Me.ButtonName.Enabled=True
Hope that helps...
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 00:10
Joined
Feb 19, 2002
Messages
43,233
But what if I wanted the button to be enabled on new record only after a certain amount of time. For example 5 seconds? Essentially delaying the Me.cmdButton.Enabled = False code.
What is the point? If you tell us why you want to do this, we can quite likely provide a meaningful solution. Also, you are using the wrong event to enable the button anyway. The code to enable the button as you are describing it's use, belongs in the form's Current Event not in the click event of a button since there are multiple ways to get to a new record and they don't have to go through your button code..

If you only want the button enabled once the user has started to modify the record, then you should be using the Form's dirty event. If you only want to enable the button for new records and only after the user has started typing, then you would use the BeforeInsert event. The Access event model is rich and provides you with a fine level of control. Your task will be significantly easier if you understand the event model so you can make informed decisions on where code belongs.

We really need to know your goal to provide an answer. Your goal is not what you stated "Is there a way to delay a button from being enabled?" That is actually the solution you have decided is the correct solution and you just don't know how to implement it. You are new to the forum and have a low post count. Based on the question, I'm assuming that you don't have a great deal of experience with Access either so I'm guessing that you might not know the best solution either. For us to help you, it is best if you describe the problem you are trying to solve rather than asking how to implement the solution you have settled on.
 

Sampoline

Member
Local time
Today, 14:10
Joined
Oct 19, 2020
Messages
161
If you only want to enable the button for new records, you might try the following approach.

In the Current Event.
Code:
Me.ButtonName.Enabled=False
If Me.NewRecord Then
    Me.TimerInterval=5000
End if

In the Timer Event.
Code:
Me.TimerInterval=0
Me.ButtonName.Enabled=True
Hope that helps...
Hey thanks, will try this out.
 

Sampoline

Member
Local time
Today, 14:10
Joined
Oct 19, 2020
Messages
161
What is the point? If you tell us why you want to do this, we can quite likely provide a meaningful solution. Also, you are using the wrong event to enable the button anyway. The code to enable the button as you are describing it's use, belongs in the form's Current Event not in the click event of a button since there are multiple ways to get to a new record and they don't have to go through your button code..

If you only want the button enabled once the user has started to modify the record, then you should be using the Form's dirty event. If you only want to enable the button for new records and only after the user has started typing, then you would use the BeforeInsert event. The Access event model is rich and provides you with a fine level of control. Your task will be significantly easier if you understand the event model so you can make informed decisions on where code belongs.

We really need to know your goal to provide an answer. Your goal is not what you stated "Is there a way to delay a button from being enabled?" That is actually the solution you have decided is the correct solution and you just don't know how to implement it. You are new to the forum and have a low post count. Based on the question, I'm assuming that you don't have a great deal of experience with Access either so I'm guessing that you might not know the best solution either. For us to help you, it is best if you describe the problem you are trying to solve rather than asking how to implement the solution you have settled on.
Hi yes, I am a new user to Access. I am here to learn, so everything you experts have to say is always taken as constructive for me.

I understand your point about the code belonging in the form's current event as opposed to the button click. Will fix that up.

So I'm not looking for the button to enable whenever a user modifies the record or after the user has started typing in general. I'm wondering if it will enable when a specific field/text box is selected. But I only want it to happen once. I found that when you try to put an event on the field, that it restarts/refreshes the button. So the particular button I have made is a file audit button. To verify file indexing is identical to the imported metadata. I wanted that button to be enabled only after selecting or even filling in a specific text field. But this should only run once. I don't want the button to keep enabling every time I select/fill out the field. It should only become enabled again when a new record is created.

If this is not possible then that's fine, was just an idea. Thanks.
 

Users who are viewing this thread

Top Bottom