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.
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.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.