locked
change caption of lable in dialog forms RRS feed

  • Question

  • hello

    i want to use a custom message box. i made it but the message text should be change according to situation from which is called and the rest of my code await for the result of the message box in which the user click on button (Yes, No, ...).

    this is my code:

    Public Function MyMsgBox(MyPrompt As String, Optional MyButton As Button_Style, Optional MyTitle As String, Optional MyImage As Image_Style)
     
        DoCmd.OpenForm "Frm_Message", , , , , acDialog
        
        Form_Frm_Message.Lbl_Prompt.Caption = MyPrompt

        If MyTitle = "" Then
            Form_Frm_Message.Caption = "My Message..."
        Else
            Form_Frm_Message.Caption = MyTitle
        End If

        If MyButton = 0 Then
            Form_Frm_Message.Btn_Okey.Visible = True
            Form_Frm_Message.Btn_Okey.Left = 75.024 '2490
        Else

            If MyButton = 1 Then
                Form_Frm_Message.Btn_Okey.Visible = True
                Form_Frm_Message.Btn_Okey.Left = 75.024 '2490
            End If
            If MyButton = 2 Then
                Form_Frm_Message.Btn_Okey.Visible = True
                Form_Frm_Message.Btn_Cancel.Visible = True
                Form_Frm_Message.Btn_Okey.Left = 75.024 '3690
                Form_Frm_Message.Btn_Cancel.Left = 1605.024 '1290
            End If
            If MyButton = 3 Then
                Form_Frm_Message.Btn_Retry.Visible = True
                Form_Frm_Message.Btn_Cancel.Visible = True
                Form_Frm_Message.Btn_Retry.Left = 75.024 '3690
                Form_Frm_Message.Btn_Cancel.Left = 2279.952 '1290
            End If
            If MyButton = 4 Then
                Form_Frm_Message.Btn_Yes.Visible = True
                Form_Frm_Message.Btn_no.Visible = True
                Form_Frm_Message.Btn_Yes.Left = 75.024 '3690
                Form_Frm_Message.Btn_no.Left = 1410.048 '1290
            End If
            If MyButton = 5 Then
                Form_Frm_Message.Btn_Yes.Visible = True
                Form_Frm_Message.Btn_no.Visible = True
                Form_Frm_Message.Btn_Cancel.Visible = True
                Form_Frm_Message.Btn_Yes.Left = 75.024 '4305
                Form_Frm_Message.Btn_no.Left = 1410.048 '2490
                Form_Frm_Message.Btn_Cancel.Left = 2865.024 '675
            End If
        End If
            
        If MyImage = 0 Then
            Form_Frm_Message.Img_Information.Visible = True
        Else

            If MyImage = 1 Then
                Form_Frm_Message.Img_Danger.Visible = True
            End If
            If MyImage = 2 Then
                Form_Frm_Message.Img_Information.Visible = True
            End If
            If MyImage = 3 Then
                Form_Frm_Message.Img_Query.Visible = True
            End If
            If MyImage = 4 Then
                Form_Frm_Message.Img_Warning.Visible = True
            End If
        End If

    because of this code (DoCmd.OpenForm "Frm_Message", , , , , acDialog) no changes happen on my message box form but when i use this code (DoCmd.OpenForm "Frm_Message") and set the "Modal" and "PopUp" property to "Yes" it works and my code does not wait for the result of message box.

    whats is the problem?

    i my question has ambiguity please tell me.

    thank you in advance.


    • Edited by mmbguide Tuesday, November 17, 2015 9:58 PM
    Tuesday, November 17, 2015 9:48 PM

Answers

  • It would be to your benefit to see the Help files on DoCmd.OpenForm.  OpenArgs is a variant string expression.  You can supply any number of data items in the expression.  It's up to you how you delineate your data for extraction once in the form.  You can create a comma separated list of your options and parse them out in the form's OnOpen event.

    It might be useful to know that when you set up your form and procedure to return a value, you'll need to hide the form rather than close it, e.g. Me.Visible=False.  Doing so will allow the code following your DoCmd.OpenForm to run.  At this point you can collect whatever information is necessary from the form such as what button was selected by the user. 

    Once you've gathered your form's information, you'll then want to close the form, e.g. DoCmd.Close acForm,"MyMsgBox."  Following that, your procedure should then be ready to exit and pass the value you've collected from the form on to the procedure's caller.

    Examples:

    Your message box procedure:

    Public Function MyMsgBox(MyPrompt As String, Optional MyButton, Optional MyTitle As String, Optional MyImage) As Integer
      
        On Error GoTo Err_Process
      
        Dim strMsg As String
        Dim strArgs As String
        Dim intReturn As Integer
        Dim strForm As String
        
        strForm = "Frm_Message"
        
        strArgs = MyPrompt
        strArgs = strArgs & "," & MyButton
        strArgs = strArgs & "," & MyTitle
        strArgs = strArgs & "," & MyImage
        
        DoCmd.OpenForm strForm, , , , , acDialog, strArgs
        
        intReturn = Forms(strForm).ReturnValue
        
        DoCmd.Close acForm, strForm
        
    Exit_Process:
        MyMsgBox = intReturn
        Exit Function
    
    Err_Process:
        strMsg = "An unexpected error occurred in procedure: MyMsgBox"
        strMsg = strMsg & vbCrLf & vbCrLf & Err.Number & " " & Err.Description
        MsgBox strMsg, vbExclamation, Error
        Resume Exit_Process
        
    End Function

    Your form procedures:

    Option Compare Database
    Option Explicit
    
    Dim mReturnValue As Integer
    
    Public Property Get ReturnValue() As Integer
    
        ReturnValue = mReturnValue
    
    End Property
    
    Private Property Let ReturnValue(Value As Integer)
        
        mReturnValue = Value
        
    End Property
    
    Private Sub Form_Open(Cancel As Integer)
             
        On Error GoTo Err_Process
        
        Dim strMsg As String
        Dim strArgs As Variant
        Dim intButton As Integer
        Dim intImage As Integer
        
        strArgs = Nz(Me.OpenArgs, "")
        
        If (strArgs = "") Then
            Err.Raise 5
            Cancel = True
        Else
            Me.Lbl_Prompt = Split(strArgs, ",")(0) 'Prompt
            intButton = Split(strArgs, ",")(1) 'Button
            Me.Caption = Split(strArgs, ",")(2)  'Title
            intImage = Split(strArgs, ",")(3) 'Image
             
            If (Me.Caption = "") Then
                Me.Caption = "My Message..."
            End If
            
            Select Case intButton
            Case 2
                Me.Btn_Okey.Visible = True
                Me.Btn_Cancel.Visible = True
                Me.Btn_Okey.Left = 75.024 '3690
                Me.Btn_Cancel.Left = 1605.024 '1290
            Case 3
                Me.Btn_Retry.Visible = True
                Me.Btn_Cancel.Visible = True
                Me.Btn_Retry.Left = 75.024 '3690
                Me.Btn_Cancel.Left = 2279.952 '1290
            Case 4
                Me.Btn_Yes.Visible = True
                Me.Btn_no.Visible = True
                Me.Btn_Yes.Left = 75.024 '3690
                Me.Btn_no.Left = 1410.048 '1290
            Case 5
                Me.Btn_Yes.Visible = True
                Me.Btn_no.Visible = True
                Me.Btn_Cancel.Visible = True
                Me.Btn_Yes.Left = 75.024 '4305
                Me.Btn_no.Left = 1410.048 '2490
                Me.Btn_Cancel.Left = 2865.024 '675
            Case Else
                Me.Btn_Okey.Visible = True
                Me.Btn_Okey.Left = 75.024 '2490
            End Select
            
            Select Case intImage
            Case Else
                Me.Img_Information.Visible = True
            Case 1
                Me.Img_Danger.Visible = True
            Case 2
                Me.Img_Information.Visible = True
            Case 3
                Me.Img_Query.Visible = True
            Case 4
                Me.Img_Warning.Visible = True
            End Select
        End If
    
    Exit_Process:
        Exit Sub
    
    Err_Process:
        strMsg = "An unexpected error occurred in procedure: Form_Open"
        strMsg = strMsg & vbCrLf & vbCrLf & Err.Number & " " & Err.Description
        MsgBox strMsg, vbExclamation, Error
        Resume Exit_Process
    
    End Sub
    
    Private Sub Btn_Okey_Click()
    
        Me.ReturnValue = 1
        Me.Visible = False
        
    End Sub
    
    Private Sub Btn_Cancel_Click()
        
        Me.ReturnValue = 2
        Me.Visible = False
        
    End Sub

    Note that your measures need to be in Twips.  Twips are defined as a unit of measurement that is equal to 1/20 of a point, or 1/1440 of an inch.  There are 567 Twips in a centimeter.







    • Edited by RunningManHD Wednesday, November 18, 2015 3:54 PM
    • Marked as answer by mmbguide Wednesday, November 18, 2015 8:35 PM
    Wednesday, November 18, 2015 11:22 AM

All replies

  • hi mmbguide,

    You can make a reference to the Frm_message by using:

            Set msg_form = Forms("Frm_Message")

    From then on use the msg_form object:

    Public Function MyMsgBox(MyPrompt As String, Optional MyButton As Button_Style, Optional MyTitle As String, Optional MyImage As Image_Style)
    
         Dim msg_form as Form
    
      
         DoCmd.OpenForm "Frm_Message", , , , , acDialog
         Set msg_form = Forms("Frm_message")
    
         msg_form.Lbl_Prompt.Caption = MyPrompt
    
         If MyTitle = "" Then
            msg_form.Caption = "My Message..."
         Else
             msg_form.Caption = MyTitle
         End If
    
    
         Select Case MyButton
         Case 0:
             msg_form.Btn_Okey.Visible = True
             msg_form.Btn_Okey.Left = 75.024 '2490
         Case 1:
             msg_form.Btn_Okey.Visible = True
             msg_form.Btn_Okey.Left = 75.024 '2490
         Case 2:
             msg_form.Btn_Okey.Visible = True
             msg_form.Btn_Cancel.Visible = True
             msg_form.Btn_Okey.Left = 75.024 '3690
             msg_form.Btn_Cancel.Left = 1605.024 '1290
         Case 3:
             'etc
         End Select

    With respect to the Left value of the control, I "think" in centimeters, so I made a function In_twips, taht converts cm to twips:

    Function In_twips(cur_cm As Variant) As Integer
      If (glo_width = 0) Then
        In_twips = Int(567 * cur_cm)   '1440 in inch 'absoluut
      Else
        In_twips = Int(cur_cm * 5.67 * glo_width) 'percentueel
      End If
    End Function

    Imb.

    Edit: when I see this result, you have to take into account the case that cur_cm has a Null value. But in my practice cur_cm has never been Null.

    Imb.

    • Edited by Imb-hb Tuesday, November 17, 2015 10:24 PM Edit added
    Tuesday, November 17, 2015 10:20 PM
  • Your problem is that you are attempting to open a form as acDialog.  That's fine, but all the code after that isn't run until after your form is closed.  The way to manage your input options is to pass them into your form through the OpenArgs argument and then process them in the form's OnOpen or OnLoad event. 

    One other thing you might make note of is that the proper way to refer to an open form is by using the Forms collection, e.g. Forms("YourFormName").  However, when processing your options from within the called form, you'll use the qualifier Me, e.g. Me.ControlName.

    • Edited by RunningManHD Tuesday, November 17, 2015 10:34 PM
    Tuesday, November 17, 2015 10:26 PM
  • Dim msg_form as Form
    DoCmd.OpenForm "Frm_Message", , , , , acDialog

    -------------

    but this code run the form as dialog and the rest of code does not run...

    if the reference form is not run the access show a message box for : can't find the reference....

    what should i do?

    Wednesday, November 18, 2015 9:49 AM
  • Your problem is that you are attempting to open a form as acDialog.  That's fine, but all the code after that isn't run until after your form is closed.  The way to manage your input options is to pass them into your form through the OpenArgs argument and then process them in the form's OnOpen or OnLoad event. 

    One other thing you might make note of is that the proper way to refer to an open form is by using the Forms collection, e.g. Forms("YourFormName").  However, when processing your options from within the called form, you'll use the qualifier Me, e.g. Me.ControlName.

    how many variable can i put in OpenArgs?
    Wednesday, November 18, 2015 9:49 AM
  • It would be to your benefit to see the Help files on DoCmd.OpenForm.  OpenArgs is a variant string expression.  You can supply any number of data items in the expression.  It's up to you how you delineate your data for extraction once in the form.  You can create a comma separated list of your options and parse them out in the form's OnOpen event.

    It might be useful to know that when you set up your form and procedure to return a value, you'll need to hide the form rather than close it, e.g. Me.Visible=False.  Doing so will allow the code following your DoCmd.OpenForm to run.  At this point you can collect whatever information is necessary from the form such as what button was selected by the user. 

    Once you've gathered your form's information, you'll then want to close the form, e.g. DoCmd.Close acForm,"MyMsgBox."  Following that, your procedure should then be ready to exit and pass the value you've collected from the form on to the procedure's caller.

    Examples:

    Your message box procedure:

    Public Function MyMsgBox(MyPrompt As String, Optional MyButton, Optional MyTitle As String, Optional MyImage) As Integer
      
        On Error GoTo Err_Process
      
        Dim strMsg As String
        Dim strArgs As String
        Dim intReturn As Integer
        Dim strForm As String
        
        strForm = "Frm_Message"
        
        strArgs = MyPrompt
        strArgs = strArgs & "," & MyButton
        strArgs = strArgs & "," & MyTitle
        strArgs = strArgs & "," & MyImage
        
        DoCmd.OpenForm strForm, , , , , acDialog, strArgs
        
        intReturn = Forms(strForm).ReturnValue
        
        DoCmd.Close acForm, strForm
        
    Exit_Process:
        MyMsgBox = intReturn
        Exit Function
    
    Err_Process:
        strMsg = "An unexpected error occurred in procedure: MyMsgBox"
        strMsg = strMsg & vbCrLf & vbCrLf & Err.Number & " " & Err.Description
        MsgBox strMsg, vbExclamation, Error
        Resume Exit_Process
        
    End Function

    Your form procedures:

    Option Compare Database
    Option Explicit
    
    Dim mReturnValue As Integer
    
    Public Property Get ReturnValue() As Integer
    
        ReturnValue = mReturnValue
    
    End Property
    
    Private Property Let ReturnValue(Value As Integer)
        
        mReturnValue = Value
        
    End Property
    
    Private Sub Form_Open(Cancel As Integer)
             
        On Error GoTo Err_Process
        
        Dim strMsg As String
        Dim strArgs As Variant
        Dim intButton As Integer
        Dim intImage As Integer
        
        strArgs = Nz(Me.OpenArgs, "")
        
        If (strArgs = "") Then
            Err.Raise 5
            Cancel = True
        Else
            Me.Lbl_Prompt = Split(strArgs, ",")(0) 'Prompt
            intButton = Split(strArgs, ",")(1) 'Button
            Me.Caption = Split(strArgs, ",")(2)  'Title
            intImage = Split(strArgs, ",")(3) 'Image
             
            If (Me.Caption = "") Then
                Me.Caption = "My Message..."
            End If
            
            Select Case intButton
            Case 2
                Me.Btn_Okey.Visible = True
                Me.Btn_Cancel.Visible = True
                Me.Btn_Okey.Left = 75.024 '3690
                Me.Btn_Cancel.Left = 1605.024 '1290
            Case 3
                Me.Btn_Retry.Visible = True
                Me.Btn_Cancel.Visible = True
                Me.Btn_Retry.Left = 75.024 '3690
                Me.Btn_Cancel.Left = 2279.952 '1290
            Case 4
                Me.Btn_Yes.Visible = True
                Me.Btn_no.Visible = True
                Me.Btn_Yes.Left = 75.024 '3690
                Me.Btn_no.Left = 1410.048 '1290
            Case 5
                Me.Btn_Yes.Visible = True
                Me.Btn_no.Visible = True
                Me.Btn_Cancel.Visible = True
                Me.Btn_Yes.Left = 75.024 '4305
                Me.Btn_no.Left = 1410.048 '2490
                Me.Btn_Cancel.Left = 2865.024 '675
            Case Else
                Me.Btn_Okey.Visible = True
                Me.Btn_Okey.Left = 75.024 '2490
            End Select
            
            Select Case intImage
            Case Else
                Me.Img_Information.Visible = True
            Case 1
                Me.Img_Danger.Visible = True
            Case 2
                Me.Img_Information.Visible = True
            Case 3
                Me.Img_Query.Visible = True
            Case 4
                Me.Img_Warning.Visible = True
            End Select
        End If
    
    Exit_Process:
        Exit Sub
    
    Err_Process:
        strMsg = "An unexpected error occurred in procedure: Form_Open"
        strMsg = strMsg & vbCrLf & vbCrLf & Err.Number & " " & Err.Description
        MsgBox strMsg, vbExclamation, Error
        Resume Exit_Process
    
    End Sub
    
    Private Sub Btn_Okey_Click()
    
        Me.ReturnValue = 1
        Me.Visible = False
        
    End Sub
    
    Private Sub Btn_Cancel_Click()
        
        Me.ReturnValue = 2
        Me.Visible = False
        
    End Sub

    Note that your measures need to be in Twips.  Twips are defined as a unit of measurement that is equal to 1/20 of a point, or 1/1440 of an inch.  There are 567 Twips in a centimeter.







    • Edited by RunningManHD Wednesday, November 18, 2015 3:54 PM
    • Marked as answer by mmbguide Wednesday, November 18, 2015 8:35 PM
    Wednesday, November 18, 2015 11:22 AM