Link to home
Start Free TrialLog in
Avatar of Declan Basile
Declan BasileFlag for United States of America

asked on

Populating a combobox from a text file

I have an Access 2003 .mdb with a listbox that displays a list of folders (i.e. directories) on a computer.  When the user selects an item in the listbox, I want a combobox to be filled with a list of files in that folder.  I discovered that I can use the command shell with the command "dir /B > x.txt" to create a text file containing a list of files in that folder, but how can I take that text file and populate a combobox with it?  Do I need to open the file in code?  If so, how?  Or is there an easier way?
ASKER CERTIFIED SOLUTION
Avatar of PatHartman
PatHartman
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Declan Basile

ASKER

Yes.  I don't believe that native Access comboboxes support the AddItem method.  

TransferText worked great and was very simple, however I had to name the field of the local Access table "F1" for it to work.  How would I specify the fieldname of the Access table to append the data to if the name wasn't "F1"?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This code will do it (no text file or table needed):

Private Sub Form_Open(Cancel As Integer)
'Created by Helen Feddema 5-Feb-2010
'Last modified by Helen Feddema 10-Feb-2010

On Error GoTo ErrorHandler

   Dim fso As New Scripting.FileSystemObject
   Dim fld As Scripting.Folder
   Dim fil As Scripting.File
   Dim strDocsPath As String
   Dim lst As Access.ListBox
   
   Set lst = Me![lstFiles]
   strDocsPath = "G:\Documents\"
   
   Set fld = fso.GetFolder(strDocsPath)
   
   For Each fil In fld.Files
      lst.AddItem fil.Name
   Next fil
   
ErrorHandlerExit:
   Exit Sub

ErrorHandler:
   MsgBox "Error No: " & Err.Number _
      & " in Form_Open procedure; " _
      & "Description: " & Err.Description
   Resume ErrorHandlerExit

End Sub

Open in new window


The listbox needs to have its Row Source Type set to Value List.