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!

Programmatically create List Box

Status
Not open for further replies.

Tommy408

Programmer
Apr 30, 2006
53
US
Is it possible to programmatically create List Box? Press a button, List Box created? I can do visible true/false, but the list box still take room in the form if it's already created.
 
Hello:

Yes. Utilize the createcontrol method.

Example
The following example first creates a new form based on an Orders table. It then uses the CreateControl method to create a text box control and an attached label control on the form.

Sub NewControls()
Dim frm As Form
Dim ctlLabel As Control, ctlText As Control
Dim intDataX As Integer, intDataY As Integer
Dim intLabelX As Integer, intLabelY As Integer

' Create new form with Orders table as its record source.
Set frm = CreateForm
frm.RecordSource = "Orders"
' Set positioning values for new controls.
intLabelX = 100
intLabelY = 100
intDataX = 1000
intDataY = 100
' Create unbound default-size text box in detail section.
Set ctlText = CreateControl(frm.Name, acTextBox, , "", "", _
intDataX, intDataY)
' Create child label control for text box.
Set ctlLabel = CreateControl(frm.Name, acLabel, , _
ctlText.Name, "NewLabel", intLabelX, intLabelY)
' Restore form.
DoCmd.Restore
End Sub

 
You can only use the CreateControl in design view. So let's say you have a form already created. You can create another form with a command button on it and place the following on the OnClick event:

Private Sub Command0_Click()
Dim ctl As Control
DoCmd.OpenForm "ListBox_Form2", acDesign
Set ctl = CreateControl("ListBox_Form2", acListBox, acDetail, , , 1500, 1500, 1800, 1500)
ctl.Name = "Pick Name"
ctl.Visible = True
ctl.RowSource = "Select distinct [middlename] from [Mailinglist] where [middlename] is not null;"
Set ctl = Nothing
DoCmd.OpenForm "ListBox_Form2"
End Sub

So, I have a form called "Listbox_Form" with a command button. When clicked, it opens another form called "Listbox_Form2" in design view, creates a listbox and populates it with the SQL statement, then opens the form in form view.

In the CreateControl statement, the numbers are the left, right, up, down position of the listbox in twips. There are 1440 twips per inch.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top