Solved How do I create and use a global variable later? (1 Viewer)

Mylton

Member
Local time
Yesterday, 16:42
Joined
Aug 3, 2019
Messages
124
Good morning
This function performs


'The GenerateLicense function receives the processor and motherboard numbers as input,
'calculate a SHA-256 hash from the concatenation of these numbers
' and returns the last 10 digits of that hash as a license.
'Used to generate a one-time license based on hardware specifications.

Function GerarLicense(NumeroProcessador As String, NumeroPlacaMae As String) As String
Dim key As String
Dim hash As String

' Concatenates the processor and motherboard numbers
key = Processor Number & Motherboard Number

' Create SHA-256 hash object
Dim objSHA256 As Object
Set objSHA256 = CreateObject("System.Security.Cryptography.SHA256Managed")

' Calculate the SHA-256 hash of the concatenated key
Dim bytes() As Byte
bytes = StrConv(key, vbFromUnicode)
bytes = objSHA256.ComputeHash_2(bytes)

' Convert the SHA-256 hash to a hexadecimal string
hash=""
Dim i As Integer
For i = LBound(bytes) To UBound(bytes)
hash = hash & Right("0" & Hex(bytes(i)), 2)
Next i

' Keep only the last 10 digits of the hash
GerarLicenca = Right(hash, 10)
End Function

I created a global variable declaration like this:
Option Compare Database
Public keyLicencaGlobal As String



When I try to use it to be demonstrated in a FrmAny text field with this code:
Private Sub Form_Load()
' Sets the value of the label to the value of the global variable
Me.txtChaveLicenca.value =chaveLicencaGlobal
End Sub


It is

Private Sub Form_Open(Cancel As Integer)
' Assign a value to the global variable
globalLicensekey = GlobalLicensekey
End Sub

The keyword LicencaGlobal appears.
I would like it to be shown what was stored in it.

Any tips...
Thanks
 
Correcting the code translation


This function performs
'The LicenseKey function takes the processor and motherboard numbers as input,
'calculate a SHA-256 hash from the concatenation of these numbers
' and returns the last 10 digits of that hash as a license.
'Used to generate a one-time license based on hardware specifications.

Function LicenseKey(ProcessorNumber As String, MotherboardNumber As String) As String
Dim Key As String
Dim hash As String

' Concatenates the processor and motherboard numbers
Key = ProcessorNumber & MotherboardNumber

' Create SHA-256 hash object
Dim objSHA256 As Object
objSHA256 = CreateObject("System.Security.Cryptography.SHA256Managed")

' Calculate the SHA-256 hash of the concatenated Key
Dim bytes() As Byte
bytes = StrConv(Key, vbFromUnicode)
bytes = objSHA256.ComputeHash_2(bytes)

' Convert the SHA-256 hash to a hexadecimal string
hash=""
Dim i As Integer
For i = LBound(bytes) To UBound(bytes)
hash = hash & Right("0" & Hex(bytes(i)), 2)
Next i

' Keep only the last 10 digits of the hash
LicenseKey = Right(hash, 10)
End Function

I created a global variable declaration like this:

Option Compare Database
PublicKeyGlobalLicense As String

When I try to use it to be demonstrated in a FrmAny text field with this code:

Private Sub Form_Load()
' Sets the value of the label to the value of the global variable
Me.TxtLicenseKey.value = GlobalLicenseKey
End Sub

It is

Private Sub Form_Open(Cancel As Integer)
' Assign a value to the global variable
GlobalLicenseKey = GlobalLicenseKey
End Sub

The keyword Global License appears.
I would like it to be shown what was stored in it.

Any tips...
Thanks
 
You don't seem to be using keyLicencaGlobal , the name that you dimmed.
Code:
Public keyLicencaGlobal As String
 
You don't seem to be using keyLicencaGlobal , the name that you dimmed.
Code:
Public keyLicencaGlobal As String
Thinking about programming is already complicated. translate even more.... in my language it's the same. When I translated it I did it wrong.

in the variable this
PublicKeyGlobalLicense As String

should have written
GlobalLicenseKey. Staying like this....

Public GlobalLicenseKey As String.

So... just like my language, it's correct. and it does not show in the text field. Any tips or examples I can learn from...
 
A "global" variable is merely a PUBLIC variable. But depending on how it is defined, it might or might not "hang around." In a class module (form or report), the declaration area CAN have variables that are PUBLIC - but they persist only for as long as the class module exists. That module will cease to exist if the form or report associated with the module closes. If the form re-opens, and if that variable isn't reset to its desired value then it will be reset to blank or zero as appropriate.

To make it persistent across multiple forms, you must define your PUBLIC variables (that you want to be global) in a GENERAL module, one that is not associated with a short-lived "parent" object like a form or report.

Note also that if you ever have an unhandled error (also called "unhandled exception") that you will probably end up having to hit a "RESET" button on the error message notice and that will reset all variables to their defaults, usually either blank or zero depending on variable type.

Because of the possibility of tripping over an unhandled error, some folks use TEMPVARS as a way to hold numeric or string values.


They persist until you remove them, delete the TempVars collection "holder", or until the Access application exits (quits).
 
good afternoon
apologies for the delay
I've been late these days.
But with your help, what needed to be resolved was resolved.
thanks.
good week to everyone
 

Users who are viewing this thread

Back
Top Bottom