Link to home
Start Free TrialLog in
Avatar of MurphyPH
MurphyPH

asked on

in ACCESS VBA how to create a popup message that just closes after one second.

I need to have an after update event that opens a popup message that just fades away after a second or two without the need to click a button in the control.

All the msgbox items need to have a click response to close the popup.
Avatar of Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger)
Flag of Canada image

A message box is a modal form, which means that it blocks your code when you call it. So there is no way to do what you want with a message box.

You need to create a simple form with the single purpose of displaying your message.

And  since VBA does not have a built-in sleep function or timer as some other environments, you will need to call the Windows API Sleep command.

To do that, add the following in the declarations of your Form:

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

Open your form

Call Sleep(2000) is you want 2 seconds

Close your form
Avatar of MurphyPH
MurphyPH

ASKER

I am not certain where I put:
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

I tried to put it in the on-load event but it was highlighted as invalid.

Private Sub Form_Load()
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) <<this was highlighted in red as invalid.

End Sub
Here is what got accepted in the main form as far as declarations goes:
Option Compare Database
Option Explicit
Private Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

I put this in the declarations of the form that holds the message:
Private Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

And this is the code that is in the after update on the main form:

Private Sub cbxSoilMat1_AfterUpdate()
If Me.SoilMatPct1 < 100 Then
Parent.Page3SoilSubform.Form.frmRedoxMessage.Visible = True
Call Sleep(2000)
Parent.Page3SoilSubform.Form.frmRedoxMessage.Visible = False
End If
End Sub

The message form does not show up.

If I leave out the lines:
Call Sleep(2000)
Parent.Page3SoilSubform.Form.frmRedoxMessage.Visible = False

The message form does show up.
I have a 64-bit version of MS Office with Access.
ASKER CERTIFIED SOLUTION
Avatar of Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger)
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That worked.  I was investigating an alternative way to do the same thing.  What about a form timer.
I set the form timer to 2000, but I don't know how to close the form with the on-timer event.

Any ideas on that?
I will assign the points to you now.
Thanks.
I would stay with the Sleep if it works.

For the form timer, if memory is good, you set the form TimerInterval in milliseconds, same as for Sleep.

There is an event triggered when the delay expires. You hide your message in there with the same code that you used before if the event is in the same form as the one you used to display your message.

You should remember to reset the TimerInterval at 0 however because as I remember it, the timer in Access repeats itself if you do not stop it.