Wiz(): Never Forget to Set the WizHook Magic Key

This convenience function handles setting the WizHook Key property with the required magic value once per session.

Wiz(): Never Forget to Set the WizHook Magic Key

The first time I played around with the hidden WizHook function in Access, I could not get it to work.

I was using one of the sample functions from JasonM's WizHook PDF reference, but it did not seem to be doing anything.  No error message.  No error code.  Just nothing.  I spent more time than I care to admit before I eventually gave up and reread the whole manual.  That's when I realized I'd made a critical oversight by skimming through the documentation.  

I missed the most important note on the first page of JasonM's reference:

The biggest thing to know: WizHook.Key = 51488399. Some methods can be called without this key, but most of them require it. The key only needs to be set once per application session, to save space I'm not including it in any sample functions.

So, to save you from a similar fate, I put together this simple function that returns a WizHook object only after it has been properly initialized.  

The Approach

Since the key only needs to be applied once per session, I use a static boolean variable inside the function to keep track of whether the WizHook object has been initialized with the key.  

The first time through the function, the boolean variable has its default value of False.  This lets the execution pass into the If block.  Inside the If block, the Key property gets set to the required "magic" value and the boolean variable gets set to True.  On subsequent calls to the function, the boolean variable being set to True prevents the execution from passing into the If block marginally boosting performance.

The final step is to set the function's return value to the Application.WizHook object.

The Wiz() Function

' ----------------------------------------------------------------
' Procedure : Wiz
' Date      : 10/26/2022
' Author    : Mike Wolfe
' Source    : https://nolongerset.com/wiz-wizhook-helper/
' Purpose   : Convenience function that sets the WizHook Key once per session.
' Notes     - Many WizHook functions fail *silently* if this key is not set.
' Usage     : Simply call `Wiz` anywhere you would normally call `WizHook`
' ----------------------------------------------------------------
Public Function Wiz() As Access.WizHook
    Static WizHookInitialized As Boolean
    
    If Not WizHookInitialized Then
        'Most WizHook methods only work after this key has been applied:
        Const MagicWizHookKey As Long = 51488399
        WizHook.Key = MagicWizHookKey
        
        'The key only needs to be applied once per session
        WizHookInitialized = True
    End If
    
    Set Wiz = WizHook
End Function

Sample Usage

Simply call the Wiz function anywhere that you would normally refer to the WizHook object directly.  

And don't worry about setting (or forgetting to set!) that .Key property ever again.

Cover Image: That's Elaine's one-time boyfriend, Jack, the fact-checker (aka, The Wiz...and nobody beats him!)

All original code samples by Mike Wolfe are licensed under CC BY 4.0