Author Topic: Populating Form Controls at runtime  (Read 5168 times)

0 Members and 1 Guest are viewing this topic.

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Populating Form Controls at runtime
« on: April 26, 2007, 03:03:02 PM »
something that seems so easy is eluding me.  I have a form named frmSetup which has 3 frames, 1 of which is named frmBusCenterLine which has 2 comboboxes, named cboBCLHighBus & cboBCLLowBus.  I am trying to populate the 2 combo boxes on the fly, but to no avail.  Autocad/VBA Help shows this
Quote
Private Sub UserForm_Initialize()
    ScrollBar1.Max = 400
    ScrollBar1.Min = 10
    ScrollBar1.Value = 100
   
    Label1.Caption = "10  -----Percent of " _
        & "Original Size---- 400"
    Label2.Caption = ScrollBar1.Value
   
    Frame1.TextBox1.Text = "Enter your text here."
    Frame1.TextBox1.MultiLine = True
    Frame1.TextBox1.WordWrap = True
   
    Frame1.Zoom = ScrollBar1.Value
End Sub
So, given that example, why cant I get values to go to the cbo boxes?

These are the things I have tried so far
Code: [Select]
      frmSetup.frmBusCenterLine.cboBCLHighBus.AddItem "7'-6"""
      frmSetup.cboBCLHighBus.AddItem "7'-8"""
      frmSetup.frmBusCenterLine.Controls.Item("cboBCLHighBus").AddItem "dsh"
     
      cboBCLLowBus.AddItem "8'-0"""
      cboBCLLowBus.AddItem "8'-6"""
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

Guest

  • Guest
Re: Populating Form Controls at runtime
« Reply #1 on: April 26, 2007, 03:13:31 PM »
Have you clicked on the combo box to check if the info is really getting in there or not?  If you don't add ....ListIndex = 0, the combo box will appear to be empty.  See below... this works for me.

Code: [Select]
Public Sub Main()
    Load frmSetup
    frmSetup.cboBCLHighBus.AddItem "7'-8"""
    frmSetup.cboBCLLowBus.AddItem "8'-0"""
    frmSetup.cboBCLLowBus.AddItem "8'-6"""
   
    frmSetup.cboBCLHighBus.ListIndex = 0
    frmSetup.cboBCLLowBus.ListIndex = 0
   
    frmSetup.Show
End Sub

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Populating Form Controls at runtime
« Reply #2 on: April 26, 2007, 03:27:55 PM »
Matt, are you using frames?  I have never had any problem filling up a cbo box.  I tried adding the listindex part, but its still a no go.

here is the form
« Last Edit: April 26, 2007, 03:30:13 PM by CmdrDuh »
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

Guest

  • Guest
Re: Populating Form Controls at runtime
« Reply #3 on: April 26, 2007, 03:31:36 PM »
Here's the little proggy I threw together.

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Populating Form Controls at runtime
« Reply #4 on: April 26, 2007, 03:35:17 PM »
OK, one thing I see that we are doing differently is your loading from a module, where as I am trying t use the FormInitialize area.  Yours worked by the way.  Let me see if I can adapt mine really quickly.
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Populating Form Controls at runtime
« Reply #5 on: April 26, 2007, 03:40:45 PM »
OK, that worked perfectly.  My question is why....  what makes it different to pass those from a module to the form, than to load them via the form init?
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

Guest

  • Guest
Re: Populating Form Controls at runtime
« Reply #6 on: April 26, 2007, 03:44:19 PM »
Here it is using the Initialize event.  Still works.  Not sure what you're doing differently?  Can you post it or PM me with the code?

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Populating Form Controls at runtime
« Reply #7 on: April 26, 2007, 03:57:16 PM »
No need, just hit me with a baseball bat :pissed: :pissed: :pissed:
I went to copy the code to PM you, and saw that I had selected the wrong event by accident, and this whole time, was populating a different event for the form, one which never runs, so it never filled up the boxes.  DUH!!!
I guess I should have checked there.  Force of habit, just missed with the mouse.
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Populating Form Controls at runtime
« Reply #8 on: April 26, 2007, 03:58:02 PM »
thanks for making me slow down and re-look at the code
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

Guest

  • Guest
Re: Populating Form Controls at runtime
« Reply #9 on: April 26, 2007, 04:02:02 PM »
thanks for making me slow down and re-look at the code
No problem... Been there before myself (many times).   :wink:

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Populating Form Controls at runtime
« Reply #10 on: April 26, 2007, 04:34:52 PM »
Ok, next question.  I am not sure how to word this so bear with me.  Given the form pic above, can I make an enumeration of the controls inside one of the frames, and capture the selected control by number (thinking about an enumeration being zero-based, get the selected control 0-?)
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Populating Form Controls at runtime
« Reply #11 on: April 26, 2007, 04:37:07 PM »
Or maybe an array...
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

Bryco

  • Water Moccasin
  • Posts: 1882
Re: Populating Form Controls at runtime
« Reply #12 on: April 26, 2007, 09:03:12 PM »
This may help, I remember battling through the same thing and forgot the answer.
As far as I remember groupname is better than frames.
Code: [Select]
Option Explicit
Private C As Control
Private F As Frame
Private Sub CommandButton1_Click()
Me.HIDE
Dim i As Integer
For Each C In Controls
    If TypeOf C Is Frame Then
        Set F = C
        For i = 0 To F.Controls.Count - 1
            If F.Controls.Item(i).Name = F.ActiveControl.Name Then
                Debug.Print i, F.ActiveControl.Name
            End If
            If F.Controls.Item(i).Value = True Then
                Debug.Print "Value", F.ActiveControl.Name
            End If
        Next
    End If
Next

End Sub

jjs

  • Guest
Re: Populating Form Controls at runtime
« Reply #13 on: April 27, 2007, 01:25:46 PM »
No need, just hit me with a baseball bat :pissed: :pissed: :pissed:
I went to copy the code to PM you, and saw that I had selected the wrong event by accident, and this whole time, was populating a different event for the form, one which never runs, so it never filled up the boxes.  DUH!!!
I guess I should have checked there.  Force of habit, just missed with the mouse.
Your username says it all
 :ugly:
How is it going cmdr?

jjs

  • Guest
Re: Populating Form Controls at runtime
« Reply #14 on: April 27, 2007, 01:27:50 PM »
what are you wanting to do with the controls? There may be a different solution that is easier/better, if we have an idea of what you need to do.