Consulting

Results 1 to 5 of 5

Thread: Code not showing form

  1. #1
    VBAX Expert
    Joined
    Jan 2005
    Posts
    574
    Location

    Code not showing form

    Below is some amazing code by Johnske

    Sub MakeUserForm()
     Dim MyUserForm As VBComponent
     Dim NewOptionButton As Msforms.OptionButton
     Dim NewCommandButton1 As Msforms.CommandButton
     Dim NewCommandButton2 As Msforms.CommandButton
     Dim MyComboBox As Msforms.ComboBox
     Dim N As Integer, MaxWidth As Long
     '<< FIRST CHECK THAT THIS USERFORM DOESN'T ALREADY EXIST >>
     For N = 1 To ActiveWorkbook.VBProject.VBComponents.Count
     If ActiveWorkbook.VBProject.VBComponents(N). _
     Name = "NewForm" Then
     '//(If it exists, show the existing form)
     'NewForm.Show
     ShowForm
     Exit Sub
     End If
     Next N
     '<< THERE IS NO EXISTING FORM, SO MAKE ONE >>
     Set MyUserForm = ActiveWorkbook.VBProject _
     .VBComponents.Add(vbext_ct_MSForm)
     '//set the form properties
     With MyUserForm
     .Properties("Height") = 100
     .Properties("Width") = 200
     On Error Resume Next
     .Name = "NewForm"
     .Properties("Caption") = "Here is your user form"
     End With
     '<< ADD A BUTTON TO THE FORM >>
     Set NewCommandButton1 = MyUserForm.Designer. _
     Controls.Add("forms.CommandButton.1")
     '//set the button properties
     With NewCommandButton1
     .Caption = "Cancel"
     .Height = 18
     .Width = 44
     .Left = MaxWidth + 147
     .Top = 6
     End With
     '<< ADD AN OK BUTTON TO THE FORM >>
     Set NewCommandButton2 = MyUserForm.Designer. _
     Controls.Add("forms.CommandButton.1")
     '//set the ok button properties
     With NewCommandButton2
     .Caption = "OK"
     .Height = 18
     .Width = 44
     .Left = MaxWidth + 147
     .Top = 28
     End With
     '<< ADD CODE IN THE USERFORM MODULE >>
     With MyUserForm.CodeModule
     N = .CountOfLines
     .InsertLines N + 1, "Sub CommandButton1_Click()"
     .InsertLines N + 2, " Unload Me"
     .InsertLines N + 3, "End Sub"
     .InsertLines N + 4, ""
     .InsertLines N + 5, "Sub CommandButton2_Click()"
     .InsertLines N + 6, " Unload Me"
     .InsertLines N + 7, "End Sub"
     End With
     '<< ADD A COMBO BOX TO THE FORM >>
     Set MyComboBox = MyUserForm.Designer. _
     Controls.Add("Forms.ComboBox.1")
     '//set the ComboBox properties
     With MyComboBox
     .Name = "Combo1"
     .Left = 10
     .Top = 10
     .Height = 16
     .Width = 100
     End With
     ShowForm
     End Sub
    It works great except i cant get it to show the form once it has created it, any Ideas?

  2. #2
    Knowledge Base Approver
    The King of Overkill!
    VBAX Master
    Joined
    Jul 2004
    Location
    Rochester, NY
    Posts
    1,727
    Location
    Hi gibbo,
    What does your "ShowForm" code look like?
    Matt

  3. #3
    Administrator
    Chat VP VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    Quote Originally Posted by mvidas
    Hi gibbo,
    What does your "ShowForm" code look like?
    Matt
    My ... Sorry, gibbo, it was late at night when I did that up and I omitted it, try this:[vba]Option Explicit

    Sub MakeUserForm()
    Dim MyUserForm As VBComponent
          Dim NewOptionButton As Msforms.OptionButton
          Dim NewCommandButton1 As Msforms.CommandButton
          Dim NewCommandButton2 As Msforms.CommandButton
          Dim MyComboBox As Msforms.ComboBox
          Dim N As Integer, MaxWidth As Long
    '<< FIRST CHECK THAT THIS USERFORM DOESN'T ALREADY EXIST >>
          For N = 1 To ActiveWorkbook.VBProject.VBComponents.Count
                If ActiveWorkbook.VBProject.VBComponents(N). _
                   Name = "NewForm" Then
                      '//(If it exists, show the existing form)
                      Run ("ShowForm")
                      Exit Sub
                End If
          Next N
    '<< THERE IS NO EXISTING FORM, SO MAKE ONE >>
          Set MyUserForm = ActiveWorkbook.VBProject _
                           .VBComponents.Add(vbext_ct_MSForm)
          '//set the form properties
          With MyUserForm
                .Properties("Height") = 100
                .Properties("Width") = 200
                On Error Resume Next
                .Name = "NewForm"
                .Properties("Caption") = "Here is your user form"
          End With
    '<< ADD A BUTTON TO THE FORM >>
          Set NewCommandButton1 = MyUserForm.Designer. _
                                  Controls.Add("forms.CommandButton.1")
          '//set the button properties
          With NewCommandButton1
                .Caption = "Cancel"
                .Height = 18
                .Width = 44
                .Left = MaxWidth + 147
                .Top = 6
          End With
    '<< ADD AN OK BUTTON TO THE FORM >>
          Set NewCommandButton2 = MyUserForm.Designer. _
                                  Controls.Add("forms.CommandButton.1")
          '//set the ok button properties
          With NewCommandButton2
                .Caption = "OK"
                .Height = 18
                .Width = 44
                .Left = MaxWidth + 147
                .Top = 28
          End With
    '<< ADD CODE IN THE USERFORM MODULE >>
          With MyUserForm.CodeModule
                N = .CountOfLines
                .InsertLines N + 1, "Sub CommandButton1_Click()"
                .InsertLines N + 2, "    Unload Me"
                .InsertLines N + 3, "End Sub"
                .InsertLines N + 4, ""
                .InsertLines N + 5, "Sub CommandButton2_Click()"
                .InsertLines N + 6, "    Unload Me"
                .InsertLines N + 7, "End Sub"
          End With
    '<< ADD A COMBO BOX TO THE FORM >>
          Set MyComboBox = MyUserForm.Designer. _
                           Controls.Add("Forms.ComboBox.1")
          '//set the ComboBox properties
          With MyComboBox
                .Name = "Combo1"
                .Left = 10
                .Top = 10
                .Height = 16
                .Width = 100
          End With
                Run ("ShowForm")
    End Sub
    
    Sub ShowForm()
    NewForm.Show
    End Sub
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

  4. #4
    VBAX Expert
    Joined
    Jan 2005
    Posts
    574
    Location
    Thanks JOHNSKE


    You are to be congratulated on a great piece of code

  5. #5
    Administrator
    Chat VP VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    Quote Originally Posted by gibbo1715
    Thanks JOHNSKE


    You are to be congratulated on a great piece of code
    Thanx very much for that gibbo, have you checked out the VBAX Knowledge base? There's literally HEAPS of fantastic stuff by some great coders in there... Click here
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •