Search code examples
vbams-forms

How do I get different fonts in dynamically-generated text boxes?


When generating my form, I have code which looks something like this:

Set obj = Me.DataSetAuthor.Controls.Add("Forms.Label.1", fld)
With obj
    .top = top
    .Left = 5000
    .Width = 20
    .height = 18
    .ControlTipText = "search " & heading
    .caption = "U"
    .FontName = "Wingdings 2"
    .Font.name = "Wingdings 2"
    .Font.Size = 16
End With

In my static editor version, caption "U" with FontName "Wingdings 2" gives me the little circle with the "X" inside. But not here, I get the "U" in font size 16. What do I need to do to get the Wingdings 2 font?


Solution

  • You should firstly set the font name and only then the caption:

    Private Sub UserForm_Initialize()
      Dim obj As MSForms.Label, top As Double, Heading As String
      top = Me.top: Heading = "My header"
       Set obj = Me.Controls.Add("Forms.Label.1", "myLab1")
       With obj
           .top = top
            .Left = 50 'for 5000 it may go out from the form...
            .Width = 20
            .Height = 18
            .ControlTipText = "search " & Heading
            .Font.Name = "Wingdings 2"
            .Caption = "U"
            .Font.Size = 16
       End With
    End Sub