Avatar of Murray Brown
Murray Brown
Flag for United Kingdom of Great Britain and Northern Ireland asked on

Access Load form name and capion to two separate ComboBoxes

Hi

I am using the following Access VBA code to load a list of forms to a Combobox
How do I load the form Captionto an additional ComboBox?


Sub oLoadFormsToCombo()

On Error GoTo EH

    Dim frm As Object
    Dim I As Integer
    
    For I = cmbFormsAndReports.ListCount - 1 To 0 Step -1
        Me.cmbFormsAndReports.RemoveItem (I)
    Next I
    
    Me.cmbFormsAndReports.SetFocus
    Me.cmbFormsAndReports.Text = ""
    Me.cmbFormsAndReports.AddItem ("")
    For Each frm In CurrentProject.AllForms
        Me.cmbFormsAndReports.AddItem (frm.Name)
    Next
    
Exit Sub
EH:
MsgBox "Error loading forms: " & Err.Description
    
End Sub

Open in new window

Microsoft AccessVBA

Avatar of undefined
Last Comment
Dale Fye

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Anders Ebro (Microsoft MVP)

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Murray Brown

ASKER
thanks very much
Anders Ebro (Microsoft MVP)

You might need to exclude the active form, otherwise Access will try to switch that into design view. It could be something like this:
 if me.name<>frm.name then
       docmd.OpenForm frm.name,acDesign,,,,acHidden
       me.cmbFormsCaption.AddItem (forms(frm.name).Caption)
       docmd.close acForm,frm.name
 End If

Open in new window

Dale Fye

 But why would you want these in two separate combo boxes, instead of displaying both the formname and caption in the same combo with appropriate column widths so that both can be seen when dropped down, which is what I would do.  Then, if you need to actually see both after a form is selected from the combo, use the AfterUpdate event to populate a textbox to display the caption of the selected form.

Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy