Author Topic: Populating Form Controls at runtime  (Read 5210 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: 1883
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.

Guest

  • Guest
Re: Populating Form Controls at runtime
« Reply #15 on: April 27, 2007, 01:34:10 PM »
An array of controls in VBA is not possible like it is in straight VB.  You can kinda "fudge" it, but I've always found that it's easier to work with controls that have unique names and not CombBox(0), ComboBox(1), etc...  Call it "personal preference", but I think it's easier that way.

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Populating Form Controls at runtime
« Reply #16 on: April 27, 2007, 03:55:36 PM »
What I was trying to do was determine which option was selected.  I was thinking of the array (I learned to do that in C#) where I could cycle through the array and find the selected control.  I ended up using select case to parse the controls.  Is there a better way?  Bryco, I saw your post, but have been in meetings all day, so I haven't had a chance to look into that yet.
Code: [Select]
      Dim booTrue As Boolean
      booTrue = True
      Select Case booTrue
            Case frmSetup.optBS3.Value
                  ThisDrawing.SetVariable "USERI4", 3
            Case frmSetup.optBS4.Value
                  ThisDrawing.SetVariable "USERI4", 4
            Case frmSetup.optBS5.Value
                  ThisDrawing.SetVariable "USERI4", 5
            Case frmSetup.optBS6.Value
                  ThisDrawing.SetVariable "USERI4", 6
      End Select
      Select Case booTrue
            Case frmSetup.optVC138kv.Value
                  ThisDrawing.SetVariable "USERS2", "138"
            Case frmSetup.optVC69kv.Value
                  ThisDrawing.SetVariable "USERS2", "69"
            Case frmSetup.optVC46kv.Value
                  ThisDrawing.SetVariable "USERS2", "46"
            Case frmSetup.optVC230kv.Value
                  ThisDrawing.SetVariable "USERS2", "230"
            Case frmSetup.optVC345kv.Value
                  ThisDrawing.SetVariable "USERS2", "345"
      End Select
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)

jjs

  • Guest
Re: Populating Form Controls at runtime
« Reply #17 on: April 27, 2007, 04:46:46 PM »
Randall or the llama had some code to fool vba into thinking there were control arrays. I tried it and did not like it so much. With the few number of controls you have, you are better off doing it how you currently are.

Are you calling those user variables from a lisp routine or vba routine?

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Populating Form Controls at runtime
« Reply #18 on: April 27, 2007, 06:04:51 PM »
VBA
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)

jjs

  • Guest
Re: Populating Form Controls at runtime
« Reply #19 on: April 29, 2007, 03:45:35 PM »
is the vba routine inside this project, or a different one?

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Populating Form Controls at runtime
« Reply #20 on: April 30, 2007, 10:03:09 AM »
this one
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)