Applecore Pages on Microsoft Access

Right Justifying Text in a MsgBox

The standard Access message box does not allow you to right justify text, although there is confusingly a constant defined, vbMsgBoxRight, that indicates that you can!. You can try to get around this by padding with blank characters, or even create your own modal pop-up form to do this, but there is an API call, MessageBoxA, that does allow you to right justify text. Just copy and paste the code below into a module, and then you can call this in exactly the same way that you call the standard Access MsgBox function.

Option Compare Database
Option Explicit

Private Declare Function apiMessageBox Lib "user32" Alias "MessageBoxA" _
    (ByVal hwnd As Long, ByVal lpText As String, ByVal lpCaption As String, ByVal wType As Long) As Long

Private Const MB_RIGHT = &H80000

Function fRMsgBox(strText As String, lngFlags As Long, Optional varCaption As Variant) As Long
'   Function to create a message box on screen using API calls and return the handle to the message box
'   Accepts:
'       strText - The text to be displayed in the Message Box
'       lngFlags - the various settings for the message box
'       strCaption - The caption to be displayed in the Message box (if nothing is supplied, uses the current application title if it exists, or else "Microsoft Access")
'   Returns:
'       the return value if successful, otherwise 0.

    On Error GoTo E_Handle
    Dim lngReturn As Long
    If IsMissing(varCaption) Then
        varCaption = CurrentDb.Properties("AppTitle")
    End If
    lngReturn = apiMessageBox(Application.hWndAccessApp, strText, varCaption, lngFlags Or MB_RIGHT)
    fRMsgBox = lngReturn
fExit:
    On Error Resume Next
    Exit Function
E_Handle:
    Select Case Err.Number
        Case 3270 ' Property not found - the application does not have a user defined title
            varCaption = "Microsoft Access"
            Resume Next
        Case Else
            MsgBox Err.Description, vbOKOnly + vbCritical, "Error: " & Err.Number
            fRMsgBox = 0
            Resume fExit
    End Select
End Function

A tip for using this function is to create your code using the standard MsgBox function, and then replace the function name with this one. That way, you can still use intelli-sense to get the constants such as vbYesNo etc.

Top

 


HOME | NEW | TABLES | QUERIES | FORMS | REPORTS | GENERAL | API | DOWNLOADS | TUTORIAL | RESOURCES
E-MAIL
Copyright & Disclaimer

 

Last modified at 06/06/2006 14:53:14