Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding ListBox Control at Runtime

Status
Not open for further replies.

JBBennett

Technical User
Jul 31, 2012
6
I need to query a database and build a multi-page form based on the data I find. I am working on the last page which is a list of names and what was done with them. To pass the data to the form, I am using a property and doing all the add control work in the property (since I could not figure out a way to get the data out of the property and into the form):

Public Property Let Names(ByVal strNames As String)
' Declare variables.
Dim lblName() As Label
Dim lstChoice() As ListBox

For intCounter = 1 To intNameCount
ReDim lblName(1 To intCounter)
ReDim lstChoice(1 To intCounter)
Dim strCurrentName As String

Set lblName(intCounter) = Me.MultiPage1("pg1").Controls.Add("Forms.Label.1")
Set lstChoice(intCounter) = Me.MultiPage1("pg1").Controls.Add("Forms.ListBox.1")

' Add labels with the names.
With lblName(intCounter)
.Caption = strCurrentName
.Top = intLabelTop
.Left = 6
.Height = 23
.Width = 80
.BorderStyle = fmBorderStyleSingle
.Name = "lblName" & intCounter​
End With

' Add list boxes with the choices.
With lstChoice(intCounter)
.Top = intLabelTop
.Left = 90
.Height = 23
.Width = 150
.AddItem ("Choice used.")
.AddItem ("Choice *NOT* used.")
.Name = "lstChoice" & intCounter​
End With
intLabelTop = intLabelTop + 28​
Next

End Property

This builds the form quite well. But when I get back into my main program and try to utilize the choices from the list boxes, they do not work. I stepped through the code and used message boxes and determined the value of the list boxes are all blank.

For Each objNames In frmQuestions.MultiPage1("pg1").Controls
Select Case Left(objNames.Name, 7)
Case "lblName"
strChoice = Replace(objNames.Name, "lblName", "")
If frmQuestions.Controls("lstChoice" & strChoice).Value = "Choice used." Then
strNames = strNames & objNames.Caption​
Else
strNonNames = strNonNames & objNameds​
End If​
End Select​
Next

I tried switching to CheckBoxes but had no success at all building the form (basic parameters like Top and Caption could not be set). I am at a loss as to how to get the values in the list boxes to be passed out of form after OK is clicked.

Please help.

Jason Bennett
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top