locked
How do I catch the Label content - the label caption? RRS feed

  • Question

  • Hi

    I want to catch the content - label caption on a Form and the use it as Subject in an email.

    I know the email part, but cant catch the content/caption.

    The name of the Label is "Title"

    Tried Me.[Title] and Me![Title]


    Cheers // Peter Forss Stockholm

    Monday, September 10, 2018 3:29 PM

Answers

  • This is my code, it is activated by a push button on the Form.

    Hi Peter,

    Personally I never use Screen.ActiveForm, because this can be influenced by many other things.

    Instead, I would use this kind of construction.

    The calling code is:

    Private Sub Kommandoknapp40_Click()
        POEmailOrder Me
    End Sub 

    and the sub POEmailOrder would then look like:

    Function POEmailOrder(cur_form As Form)
    
    On Error GoTo POEmailOrder_Err
    
    Dim receiver As String
    Dim Subject As String
    Dim What As String
    
    ' Line below doesnt work
    What = cur_form!Title.Caption
    
    receiver = cur_form.[epost].Column(1)
    receiver = Replace(Split(receiver, "#")(1), "mailto:", "")
    
    Subject = What & " - Purchase Order No: " & cur_form.[BeställningsID]
    
    DoCmd.SendObject acReport, "Beställning av Beverages från formulär", "", receiver, "", "", Subject, "", True, ""
    
    
    POEmailOrder_Exit:
        Exit Function
    
    POEmailOrder_Err:
        MsgBox Error
        Resume POEmailOrder_Exit
    
    End Function

    Also, an additional advantage is that in this way you can write routines that are independant of which particular form is used.

    Imb.



    • Edited by Imb-hb Tuesday, September 11, 2018 7:39 AM type
    • Marked as answer by ForssPeterNova Tuesday, September 11, 2018 12:16 PM
    Tuesday, September 11, 2018 7:37 AM

All replies

  • What about
    Me.Caption


    Daniel Pineault, 2010-2017 Microsoft MVP
    Professional Support: http://www.cardaconsultants.com
    MS Access Tips and Code Samples: http://www.devhut.net

    Monday, September 10, 2018 3:32 PM
  • The name of the Label is "Title"

    Hi Peter,

    Or the caption of the label:    Me![Title].Caption   or  [Title],Caption

    Imb.

    Monday, September 10, 2018 5:33 PM
  • Hi Daniel

    Sorry but it didn't work

    What = Me.Caption Gives Compile error - Invalid use of Me keyword.



    Cheers // Peter Forss Stockholm

    Monday, September 10, 2018 6:04 PM
  • Hi Imb

    Sorry but Me![title].Caption gives Compile error - Invalid use of Me keyword


    Cheers // Peter Forss Stockholm

    Monday, September 10, 2018 6:07 PM
  • Hi

    This solved it:

    Dim frmCurrentForm As Form
    Set frmCurrentForm = Screen.ActiveForm

    What = frmCurrentForm.Caption


    Cheers // Peter Forss Stockholm

    Monday, September 10, 2018 6:08 PM
  • Me only works within an object (form, report).  If you aren't running this within an object then you can't use Me and need to set an object and then use it to get the caption.

    Daniel Pineault, 2010-2017 Microsoft MVP
    Professional Support: http://www.cardaconsultants.com
    MS Access Tips and Code Samples: http://www.devhut.net

    Monday, September 10, 2018 10:34 PM
  • Hi Daniel

    This is my code, it is activated by a push button on the Form.

    Private Sub Kommandoknapp40_Click()
    Call POEmailOrder
    End Sub

    Function POEmailOrder()
    On Error GoTo POEmailOrder_Err
    
    Dim receiver As String
    Dim Subject As String
    Dim frmCurrentForm As Form
    Dim What As String
    
    Set frmCurrentForm = Screen.ActiveForm
    
    ' Line below doesnt work
    What = Me.Title.Caption
    
    ' But this line does:
    'What = frmCurrentForm.Title.Caption
    receiver = frmCurrentForm.[epost].Column(1)
    receiver = Replace(Split(receiver, "#")(1), "mailto:", "")
    
    Subject = What & " - Purchase Order No: " & frmCurrentForm.[BeställningsID]
    
    DoCmd.SendObject acReport, "Beställning av Beverages från formulär", "", receiver, "", "", Subject, "", True, ""
    
    
    POEmailOrder_Exit:
        Exit Function
    
    POEmailOrder_Err:
        MsgBox Error
        Resume POEmailOrder_Exit
    
    End Function
    


    Cheers // Peter Forss Stockholm

    Tuesday, September 11, 2018 4:35 AM
  • This is my code, it is activated by a push button on the Form.

    Hi Peter,

    Personally I never use Screen.ActiveForm, because this can be influenced by many other things.

    Instead, I would use this kind of construction.

    The calling code is:

    Private Sub Kommandoknapp40_Click()
        POEmailOrder Me
    End Sub 

    and the sub POEmailOrder would then look like:

    Function POEmailOrder(cur_form As Form)
    
    On Error GoTo POEmailOrder_Err
    
    Dim receiver As String
    Dim Subject As String
    Dim What As String
    
    ' Line below doesnt work
    What = cur_form!Title.Caption
    
    receiver = cur_form.[epost].Column(1)
    receiver = Replace(Split(receiver, "#")(1), "mailto:", "")
    
    Subject = What & " - Purchase Order No: " & cur_form.[BeställningsID]
    
    DoCmd.SendObject acReport, "Beställning av Beverages från formulär", "", receiver, "", "", Subject, "", True, ""
    
    
    POEmailOrder_Exit:
        Exit Function
    
    POEmailOrder_Err:
        MsgBox Error
        Resume POEmailOrder_Exit
    
    End Function

    Also, an additional advantage is that in this way you can write routines that are independant of which particular form is used.

    Imb.



    • Edited by Imb-hb Tuesday, September 11, 2018 7:39 AM type
    • Marked as answer by ForssPeterNova Tuesday, September 11, 2018 12:16 PM
    Tuesday, September 11, 2018 7:37 AM
  • +1 to Imb's approach.

    Daniel Pineault, 2010-2017 Microsoft MVP
    Professional Support: http://www.cardaconsultants.com
    MS Access Tips and Code Samples: http://www.devhut.net

    Tuesday, September 11, 2018 10:29 AM
  • Hi Imb and Daniel

    I took Imbs code. Compile error -Variable not defined



    Cheers // Peter Forss Stockholm


    Tuesday, September 11, 2018 11:24 AM
  • You forgot to include the input variable

        Function POEmailOrder(cur_form As Form)

    So you'd need to change your code slightly to include the cur_Form input variable cur_Form As Form.

    Also, if you're not returning a value, you could switch the Function to a Sub.


    Daniel Pineault, 2010-2017 Microsoft MVP
    Professional Support: http://www.cardaconsultants.com
    MS Access Tips and Code Samples: http://www.devhut.net



    Tuesday, September 11, 2018 11:56 AM


  • Hi Peter,

    You forgot the argument of the function:

       Function POEmailOrder(cur_form As Form)

    But why a function, if you don't use the function-result?

    Imb.

    Tuesday, September 11, 2018 11:58 AM
  • Hi Imb

    Now it run smooth. Using your code. Thank you very much.

    Function or Sub ... 

    Sometimes I start something by creating av Macro. Then I convert the Macro to VBA code. Its the built in converter picking "Function" instead of "Sub". Sometimes I change that manually, but often I forget.


    Cheers // Peter Forss Stockholm

    Tuesday, September 11, 2018 12:15 PM
  • Now it run smooth. Using your code. Thank you very much.

    Hi Peter,

    Good to hear you succeeded.

    I like this kind of programming. It is the way towards re-usable code, what makes (or at least can make) your programming easier and more robust. All my applications (100+) are build in this way. The nice thing about it is that all knowledge gathered in the years is immediately available in the applications, and all future applications to come.

    Imb.

    Tuesday, September 11, 2018 12:40 PM
  • Hi Imb

    Please teach me.

    What is the differens between:

    POEmailOrder Me

     and

    Call POEmailOrder


    Cheers // Peter Forss Stockholm

    Wednesday, September 12, 2018 4:47 AM
  • What is the difference

    Hi Peter,

    "Call" is the instruction that the program execution will be transfered to the Sub. It is an optional instruction, so you can use it or not. If you use it, then the arguments of the Sub, if there are, must be enclosed in round brackets. It is a matter of (house)style if you want to use it.

    So,    POEmailOrder    is equivalent to    Call POEmailOrder,
    and    POEmailOrder Me    is equivalent to    Call POEmailOrder(Me)

    The difference between    POEmailOrder    and    POEmailOrder Me:

    In the latter case you pass the Me object to the Sub. The Sub then exactly knows which object to use, including all its methods and properties. In the first case you still have to define internally which object to use, e.g. with Screen.ActiveForm.

    You can read more about all this matter in the Help of Access.

    Imb.


    • Edited by Imb-hb Wednesday, September 12, 2018 7:32 AM
    Wednesday, September 12, 2018 7:31 AM
  • Imb

    Thank you very much. I didn't know. 


    Cheers // Peter Forss Stockholm

    Wednesday, September 12, 2018 7:39 AM