The built-in MsgBox function doesn't allow you to
automatically close it after a specified duration. A workaround is to create a form
and close it through it's Timer event.
Another alternative is to build the form on the fly by using the CreateForm
and CreateControl methods. This function uses these
methods to create a form and automatically close it after a specified interval.
Sub mxbPopupMessage(ByVal message As String, _
Optional ByVal title As Variant, _
Optional ByVal duration As Single, _
Optional strFontName As String, _
Optional intFontSize As Integer)
Dim f As Form
Dim lbl As Label
Dim dblWidth As Double
Dim myName As String
Dim savedForm As Boolean
savedForm = False
On Error GoTo ErrorHandler
Application.Echo False
Set f = CreateForm
myName = f.Name
f.RecordSelectors = False
f.NavigationButtons = False
f.DividingLines = False
f.ScrollBars = 0
f.PopUp = True
f.BorderStyle = acDialog
f.Modal = True
f.ControlBox = False
f.AutoResize = True
f.AutoCenter = True
If IsMissing(title) Then
f.Caption = "Info"
Else
f.Caption = title
End If
Set lbl = CreateControl(f.Name, acLabel)
lbl.Caption = message
lbl.BackColor = 0
lbl.ForeColor = 0
lbl.Left = 100
lbl.Top = 100
If strFontName <> "" Then lbl.FontName = strFontName
If intFontSize > 0 Then lbl.FontSize = intFontSize
lbl.SizeToFit
dblWidth = lbl.Width + 200
f.Width = dblWidth - 200
f.Section(acDetail).Height = lbl.Height + 200
DoCmd.Close acForm, myName, acSaveYes
savedForm = True
DoCmd.OpenForm myName
DoCmd.MoveSize , , dblWidth
DoCmd.RepaintObject acForm, myName
Application.Echo True
If duration <= 0 Then duration = 2
Dim startTime As Single
startTime = Timer
While Timer < startTime + duration
Wend
DoCmd.Close acForm, myName, acSaveNo
DoCmd.DeleteObject acForm, myName
Exit Sub
ErrorHandler:
Application.Echo True
Dim i As Integer
For Each f In Forms
If f.Name = myName Then
DoCmd.Close acForm, myName, acSaveNo
Exit For
End If
Next f
If savedForm Then
DoCmd.DeleteObject acForm, myName
End If
End Sub
|