I have a string Application.hWndAccessApp
that appears in multiple lines of code to call a custom MsgBoxT
. Instead of writing Application.hWndAccessApp
at every instance across modules and classes, I would like it to be assigned to a public variable such as hid as string
. I know about declaring a public variable and then assigning it a value within a function or SubRoutine.
Here, I would like that value to be assigned globally so that it becomes available across all modules/classes every time I want to use MsgBoxT
function.
EDIT: I followed the tip from comments below but it gives Error # 13: Type Mismatch
.
Global Const hid = "Application.hWndAccessApp"
Then I call it using function below:
MsgBoxT hid, "Record Updated!", "Confirmation", VbInformation, 0 , 1000
My MsgBoxT function is declared publicy:
Public Declare PtrSafe Function MsgBoxT _
Lib "user32" _
Alias "MessageBoxTimeoutA" ( _
ByVal hwnd As LongPtr, _
ByVal lpText As String, _
ByVal lpCaption As String, _
ByVal wType As VbMsgBoxStyle, _
ByVal wLange As Long, _
ByVal dwTimeout As Long) _
As Long
Based on the updated question, the variable is a LongPtr
. Since it's initialized on Runtime
you have one option:
Declaring a Global LongPtr that you will assign in a second moment:
Global hid As LongPtr
hid = Application.hWnd
With the updated code from the question below, it's now running smoothly:
Global hid As LongPtr
Public Declare PtrSafe Function MsgBoxT _
Lib "user32" _
Alias "MessageBoxTimeoutA" ( _
ByVal hwnd As LongPtr, _
ByVal lpText As String, _
ByVal lpCaption As String, _
ByVal wType As VbMsgBoxStyle, _
ByVal wLange As Long, _
ByVal dwTimeout As Long) _
As Long
With these changes, it works (tested on my PC):
'On This_Workbook
Private Sub Workbook_Open()
hid = Application.hwnd
End Sub
'Anywhere in the code
Sub test()
MsgBoxT hid, "Record Updated!", "Confirmation", vbInformation, 0, 1000
End Sub
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments