How to assign value to a Global Variable for global use

Lone

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
Louis

Based on the updated question, the variable is a LongPtr. Since it's initialized on Runtime you have one option:

  1. 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

enter image description here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to Assign value to global variable and use it in other functions in jquery/javascript?

How to use the value of a global variable?

how to get the assign the queried value to global variable?

How to assign value to global variable in PHP

XSLT : How to assign a value to global variable

How to assign a value to an imported global?

Rxjs assign value in observable to global variable

Assign value to a global variable in c causes error

C++: Assign value to global class variable

use require to dynamically assign variable to global scope

Is it possible to assign a variable to use in functions but it should not be global

assign global variable into index

How to assign an argument of a callback to a global variable?

How to use a Global Variable in Jest

how to eliminate global variable use

How to use Global Variable in Python

How to use global variable in JavaScript?

How to assign global enum as Tag value in XAML?

How to store a global value (not necessarily a global variable) in jQuery?

angular 4 assign promise return value to global variable

sas, simplest task: assign input value to a global variable

Unable to assign value to global variable inside function, Python 3.8.10

Assign a global variable in a subclass and use it in the onCreate method - Android

Assign Global Variable to Function Parameter

Assign tuple value to a global var

How to create map in Golang with global variable as value?

How to get global variable value to ajax

How to get the global variable value in Perl?

How to store a value from firebase in a global variable?