Solved Adding Items to Combobox, but They aren't showing up

AngelSpeaks

Active member
Local time
Today, 10:34
Joined
Oct 21, 2021
Messages
653
I have a combobox, cboDatabases. I am trying to load the names of Access Databases in the project's path into a combo box . When I step thru the code, I am seeing the database name, but it's not loading into the combo box. I did try to hardcode specific items into the combo box, but no success. The form is frmSelectCompany, row source type is "Value List", the width of the column is 4 inches.

Thanks

Here is the code:

Code:
Private Sub Form_Load()
    Dim FSO As Scripting.FileSystemObject
    Dim SourceFolder As Scripting.Folder
    Dim FileItem As Scripting.File
    Dim strPath As String
    strPath = CurrentProject.path
 
    Set FSO = New Scripting.FileSystemObject
    Set SourceFolder = FSO.GetFolder(strPath)
 
    For Each FileItem In SourceFolder.files
        If Right(FileItem.Name, 6) = ".accdb" Then
            Me.cboDataBases.AddItem (FileItem.Name)
        End If
    Next FileItem
   
 End Sub
 
I've tested your code and it works fine. The database names are added to the combobox and displayed
 
Form_Load() is early in the lifecycle of a form. It's possible you have some downstream process that erases the contents of the combo. To test for this case, you can add this line of code after your For loop...
Code:
Debug.Print Me.cboDataBases.RowSource
...which should show a semi-colon delimited list of the filenames you added.

Other things to check are the ColumnCount and ColumnWidths properties of the combo. I don't know if you can set the ColumnCount to zero, but if the first item in ColumnWidths is zero, this will very effectively hide the filenames, even if they were correctly added to the RowSource.
 
Form_Load() is early in the lifecycle of a form. It's possible you have some downstream process that erases the contents of the combo. To test for this case, you can add this line of code after your For loop...
Code:
Debug.Print Me.cboDataBases.RowSource
...which should show a semi-colon delimited list of the filenames you added.

Other things to check are the ColumnCount and ColumnWidths properties of the combo. I don't know if you can set the ColumnCount to zero, but if the first item in ColumnWidths is zero, this will very effectively hide the filenames, even if they were correctly added to the RowSource.
Column count is 1, width is 4inches, right now the only code is what I posted. I will try your code. Thanks
 
maybe the code you posted in not from the same form?
 
And ColumnWidths? If you want, run this code, and post what gets printed to the Immediate pane.
Code:
Private Sub Form_Load()
    Dim fso As New Scripting.FileSystemObject
    Dim fd As Scripting.File
 
    For Each fd In fso.GetFolder(CurrentProject.Path).Files
        If Right(fd.Name, 6) = ".accdb" Then Me.cboDatabases.AddItem fd.Name
    Next
    
    With Me.cboDatabases
        Debug.Print "RowSourceType", .RowSourceType
        Debug.Print "RowSource", .RowSource
        Debug.Print "ColumnCount", .ColumnCount
        Debug.Print "ColumnWidths", .ColumnWidths
        Debug.Print "ListWidth", .ListWidth
    End With
End Sub
Then we can see everything that matters to the visibility of columns in your Combo.
 
For comparison, here is my output for the above code for your code that works for me.
Code:
RowSourceType Value List
RowSource     
Classes.accdb;Commissions.accdb;Commissions_Backup.accdb;Commissions_be.accdb;Contacts.accdb;Database1.accdb;Database2.accdb;Database3.accdb;DB Format Upgrader1.accdb;Diabetes - Copy.accdb;Diabetes -080324).accdb;Diabetes.accdb;Diabetes_2024-01-18.accdb;Diabetes_Backup.accdb;Diabetes_Backup1.accdb;Diabetes_Backup230924.accdb;Diabetes_be.accdb;DVLA.accdb;Employee.accdb;Employee_Backup.accdb;Employee_be.accdb;Food.accdb;GCCS.accdb;Holiday Planner V2_be.accdb;JAG Xero.accdb;JAGTest.accdb;Music.accdb;MusicDir.accdb;MusicDir_Backup.accdb;New Test.accdb;New Test_Backup.accdb;New Test_Backup1.accdb;New Test_Backup2.accdb;NoRef.accdb;Northwind 2007.accdb;Northwind2.accdb;Old Diabetes.accdb;Old Test Backup.accdb;Old Test.accdb;Old Test_Backup.accdb;Old Test_Backup1.accdb;our_budget 2007.accdb;PaulTest.accdb;PaulTest_Backup.accdb;PA_Allocation.accdb;Personal Account Ledger.accdb;Test Call Reporting_be.accdb;Test.accdb;Test2.accdb;Test_Backu20..accdb;Test_Backup.accdb;Test_Backup1.accdb;Test_Backup10.accdb;Test_Backup2
.accdb;Test_Backup3.accdb;Test_be.accdb;Translate.accdb;ua upload.accdb;UA.accdb;Utils.accdb;Utils_Backup.accdb
ColumnCount    1 
ColumnWidths  3402
ListWidth     0
 
Form_Load() is early in the lifecycle of a form. It's possible you have some downstream process that erases the contents of the combo. To test for this case, you can add this line of code after your For loop...
Code:
Debug.Print Me.cboDataBases.RowSource
...which should show a semi-colon delimited list of the filenames you added.

Other things to check are the ColumnCount and ColumnWidths properties of the combo. I don't know if you can set the ColumnCount to zero, but if the first item in ColumnWidths is zero, this will very effectively hide the filenames, even if they were correctly added to the RowSource.
Column count is 1, width is 4inches, right now the only code is what I posted. I will try your code. Th
And ColumnWidths? If you want, run this code, and post what gets printed to the Immediate pane.
Code:
Private Sub Form_Load()
    Dim fso As New Scripting.FileSystemObject
    Dim fd As Scripting.File
 
    For Each fd In fso.GetFolder(CurrentProject.Path).Files
        If Right(fd.Name, 6) = ".accdb" Then Me.cboDatabases.AddItem fd.Name
    Next
   
    With Me.cboDatabases
        Debug.Print "RowSourceType", .RowSourceType
        Debug.Print "RowSource", .RowSource
        Debug.Print "ColumnCount", .ColumnCount
        Debug.Print "ColumnWidths", .ColumnWidths
        Debug.Print "ListWidth", .ListWidth
    End With
End Sub
Then we can see everything that matters to the visibility of columns in your Combo.
Results with your code:

Code:
RowSourceType Value List
RowSource     AngelsChecking.accdb;MyLedger Conversion.accdb;MyLedger FE.accdb;MyLedger_be - BASE.accdb;MyLedger_be.accdb;TEST FE.accdb
ColumnCount    1
ColumnWidths  5760
ListWidth     0
RowSourceType Value List
RowSource     AngelsChecking.accdb;MyLedger Conversion.accdb;MyLedger FE.accdb;MyLedger_be - BASE.accdb;MyLedger_be.accdb;TEST FE.accdb
ColumnCount    1
ColumnWidths  5760
ListWidth     0
RowSourceType Value List
RowSource     AngelsChecking.accdb;MyLedger Conversion.accdb;MyLedger FE.accdb;MyLedger_be - BASE.accdb;MyLedger_be.accdb;TEST FE.accdb
ColumnCount    1
ColumnWidths  5760
ListWidth     0
 
Just change the combo's "BackColor" property to something other than "No Color". All the data is there, its just the BackColor is the same as the ForeColor.
 
I'm afraid I don't understand your response.
Well it would not be the first time, that someone posts code that is not actually what they are using? :(
So always worth double checking?, especially since the code you posted works for everyone but you?

I just deleted that and created another
1739567571176.png

However I am thinking as the list was black, you have black on black? :-(

If you must have black, change the font colour.
1739567701622.png
 
This is weird. Updated database attached Please look at the two images. The items in the combo box get displayed when I click the down arrow.
cbo1.png
cbo2.png
 

Attachments

Well they will. :(
It is up to you if you want to set a particular entry on load.
 
Well they will. :(
It is up to you if you want to set a particular entry on load.
I've never seen this happen. First time I loaded a combo box with code. Every other time they were bound to a table or a query or I typed in maybe five items.
 
A combo from a table/query is going to do the same thing until you use the dropdown or start typing?
 
Ok this is what I did. When I created the control, a large box was created. When I changed the combobox to a list box or reduced the size of the box I got what I wanted. See attached. Thanks
 

Attachments

  • cbo3.png
    cbo3.png
    40.9 KB · Views: 17
Here is my Daily Entry form in my Diabetes table.
You will see the combo is empty until I do what I said is needed to do in my previous post.

A listbox behaves completely differently. :(

1739571541586.png
 
Here is my Daily Entry form in my Diabetes table.
You will see the combo is empty until I do what I said is needed to do in my previous post.

A listbox behaves completely differently. :(

View attachment 118535
I generally do not use list box but in this situation I wanted it. I must be having a senior moment.
 

Users who are viewing this thread

Back
Top Bottom