Modern Message Boxes in Access
If you are looking to modernize the look and feel of your Access applications, sprucing up the message boxes that you use will have a big impact.
With Windows Vista (remember when that was still new?), Microsoft introduced task dialogs as part of the Common Controls library (comctl32.dll). Here's an example of a task dialog from the Microsoft documentation:
As this is a COM library, its properties and methods are available to us via API calls in VBA. Here are the two primary functions that allow us to create these forms:
Private Declare PtrSafe Function TaskDialog Lib "comctl32.dll" ( _
ByVal hwndParent As LongPtr, _
ByVal hInstance As LongPtr, _
ByVal pszWindowTitle As LongPtr, _
ByVal pszMainInstruction As LongPtr, _
ByVal pszContent As LongPtr, _
ByVal dwCommonButtons As Long, _
ByVal pszIcon As LongPtr, _
pnButton As Long) As Long
Private Declare PtrSafe Function TaskDialogIndirect Lib "comctl32.dll" ( _
pTaskConfig As TASKDIALOGCONFIG, _
pnButton As Long, _
pnRadioButton As Long, _
pfVerificationFlagChecked As Boolean) As Long
To say the above code is just the tip of the iceberg, though, is a major understatement.
The full code required to get even a simple task dialog working requires declaration of tons of constants, user-defined types, enums, and additional API function calls. Plus, you need to make sure that everything works in both 32-bit and 64-bit versions of VBA.
Whew, that sounds like a lot of work!
Work Smarter Not Harder
As it turns out, it IS a lot of work!
The wonderful news, though, is that someone has already done all of that hard work. Actually, some ones have done all that hard work.
- Jon Johnson (a.k.a., fafalone) posted the original VB6 code at vbforums
- Kevin Bell (a.k.a., Kevin Bell) converted the code for VBA use at accessui.com
- Rob Cooper and Shane Groff helped Kevin Bell along the way
"If I have seen further it is by standing on the shoulders of giants."
- Sir Isaac Newton
Sample Code
The only code you really need to worry about is contained in a single, easy-to-work-with class module, cTaskDialog
.
The sample code below can be used to generate the task dialog that follows:
Dim TaskDialog As cTaskDialog
Set TaskDialog = New cTaskDialog
With TaskDialog
.Init
.Flags = TDF_USE_COMMAND_LINKS
.MainInstruction = "Command Buttons"
.Content = "Bet you can't do this with a MsgBox!"
'.CommonButtons = TDCBF_CANCEL_BUTTON
.IconMain = TD_SHIELD_WARNING_ICON
.Title = "TaskDialog Samples"
.AddCustomButton 201, "Press Me!"
.AddCustomButton 202, "No... No... Press Me!!!" & vbLf & "You can also have sub text by adding a line feed."
.AddCustomButton 203, "With a Command Link you don't have to have any buttons!"
.ShowDialog
Debug.Print "User pressed: "; .ResultMain
End With
Requirements
To run the code, you will need a reference to the DAO library (Tools > References > "Microsoft Office 16.0 Access database engine Object Library").
You will also need to import the following code modules into your project:
cTaskDialog
(class module)basTaskDialogGlobal
basGDIPlus_Lib
basDialogWrapper
(optional)
All of these modules are in the sample database available for download on Kevin Bell's site, https://accessui.com/Products/VBATaskDialog
Kevin's database also has a ton of other custom dialog samples along with convenient buttons that jump you right to the code used to generate each sample.
The cost for all this goodness? FREE!!! (Just be sure to leave the copyright notices in place 😉)