VB Script - Global Variables

VB Script - Global Variables

CMM-Manager's VB Script engine does not currently support Global Variables.  Each time the program is run all variables are re-created / set based on current program execution.  It is however possible to write data to an external location such as Windows File System or Windows Registry.  The example below shows the process to create a Windows Registry entry, then read or write to / from this entry.  This could be used to store a value that should be incremented automatically with each program execution.  

Place this Script at or near the beginning of program:
  1. 'Resume Script if Error Occurs
  2. On Error Resume Next

  3. ' Create a WSH Shell object:
  4.  Set wshShell = CreateObject( "WScript.Shell" )
  5.  
  6.  ' Create a new key:
  7.  wshShell.RegWrite "HKCU\TestKey\", ""

  8.  ' Read the values if already exists:
  9. CurrentState = (wshShell.RegRead( "HKCU\TestKey\StringTestValue" ))

  10. 'Optional Error Handling
  11. If Err.Number <> 0 Then
  12.   MsgBox("Variable does not yet exist - creating new Global Variable")
  13. End If

  14. 'Prompt for String - Use Previous Value if Available
  15. MyString = Cint(InputBox("Please Serial Number", "Enter Value", CurrentState))

  16.  ' Write string value:
  17.  wshShell.RegWrite "HKCU\TestKey\StringTestValue", MyString, "REG_SZ"

  18.  ' Release the object
  19.  Set wshShell = Nothing

Place this Script at the end of program:

  1. ' Create a WSH Shell object:
  2.  Set wshShell = CreateObject( "WScript.Shell" )

  3. ' Read the value from Registry:
  4. MyString = (wshShell.RegRead( "HKCU\TestKey\StringTestValue" ))

  5. MySerial = MyString   'write current value to variable used in Report Header
  6. MyString = MyString + 1  'increment current value for use in next program execution

  7.  'Write string value:
  8.  wshShell.RegWrite "HKCU\TestKey\StringTestValue", MyString, "REG_SZ"

  9.  ' Release the object
  10.  Set wshShell = Nothing

On first program run, the operator sees a message letting them know a new Windows Registry Entry will be created, then an InputBox() prompts for starting value.




On subsequent program run(s), the prompt contains automatically the next integer value:




Idea
The entry in this example is created in the HKCU (Current User) section of the Windows Registry.  This value written to Registry will be unique for each Windows Login.  You may decide to write / read to a Registry entry in HKLM (Local Machine) instead where the value will be shared amongst all Windows users.