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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Filenames populating combobox??? 1

Status
Not open for further replies.

krymat

Technical User
Jul 25, 2000
105
Let's say in directory c:\be I have Several files 01stat03.rep, 01stat04.rep, 02stat01.rep, with different modified dates. How would I get those filenames along with the modified date to populate a combobox.
the info would look like this:
03/26/2001 01stat03.rep
 
Check out the FileSystemObject. It is part of Scrrun.dll i.e. "Scripting" object.
Dim objFS as Scripting.FileSYtemObject
Dim objFiles as Scripting.Files
Dim objFile as Scripting.File
Dim strWPath as string
Dim aryFileName () as filename
Redim aryFileName (0)
strWPath = "C:\xxxxx\yyyy etc"
'*****
'* Process Files in this folder
'*****
Do
If Len(strResponse) > 0 Then Exit Do

On Error Resume Next
strErrSuffix = strErrTitle & "GetFiles=" & strWPath
Set objFiles = objFS.GetFolder(strWPath).Files
If Err.Number <> 0 Then strResponse = Err.Description
On Error GoTo 0
If Len(strResponse) > 0 Then Exit Do
lngFileCount = 0
For Each objfile In objFiles
J = lngFileCount
If (J >= UBound(aryFilename)) Then
ReDim Preserve aryFilename(2 * J + 1) ' Resize the array.
End If
With objfile
lngFileCount = lngFileCount + 1
aryFilename(lngFileCount) = .Name
End With
Next
Exit Do: Loop
ReDim Preserve aryFilename(lngFileCount) ' Resize the array to actual.
 
This seems to be alot of code, but it'll do what you want. It uses the Dir() function along with the Scripting object explained above by John. To Test this, make a form with 2 comboboxes. For the 1st ComboBox set the Row Source Type to FieldListing1 and the Number of columns to 1. For 2nd ComboBox set the Row Source Type to FieldListing2 and the Column Count to 2. Copy and Paste the following code into the code module behind the form. Ensure that you have the Microsoft Scripting Runtime checked in your References. FieldListing1 uses one column, FieldListing2 uses 2 colums, use which one you like the best.

PaulF

Function FileListing1(fld As Control, ID As Variant, row As Variant, col As Variant, Code As Variant) As Variant
On Error GoTo errHandler
Static Entries As Integer
Dim X As Integer
Dim strFile As String
Dim fso As New Scripting.FileSystemObject
Dim ReturnVal As Variant
ReturnVal = Null
Static myfilename(0 To 100) As String
X = 0
Select Case Code
Case acLBInitialize
strFile = Dir(&quot;c:\be\&quot;)
Do While Len(strFile) > 0
If Right(strFile, 3) = &quot;rep&quot; Then
myfilename(X) = fso.GetFile(&quot;c:\be\&quot; & strFile).DateLastModified & &quot; &quot; & strFile
X = X + 1
End If
strFile = Dir
Loop
If myfilename(0) = &quot;&quot; Then
myfilename(0) = &quot;No Files&quot;
End If
Entries = X - 1
ReturnVal = Entries
Case acLBOpen
ReturnVal = Timer ' generate unique ID for control
Case acLBGetRowCount ' get number of rows
ReturnVal = Entries
Case acLBGetColumnCount 'get number of columns
ReturnVal = fld.ColumnCount
Case acLBGetColumnWidth
ReturnVal = -1 ' -1 forces use of default width
Case acLBGetValue
ReturnVal = myfilename(row)
Case acLBEnd
Erase myfilename
End Select
FileListing1 = ReturnVal
Exit Function
errHandler:
MsgBox Err.Number & &quot; : &quot; & Err.Description, vbCritical, &quot;Error&quot;
Exit Function

End Function

Function FileListing2(fld As Control, ID As Variant, row As Variant, col As Variant, Code As Variant) As Variant
On Error GoTo errHandler
Static Entries As Integer
Dim X As Integer
Dim strFile As String
Dim fso As New Scripting.FileSystemObject
Dim ReturnVal As Variant
ReturnVal = Null
Static myfilename(0 To 100, 1) As String
X = 0
Select Case Code
Case acLBInitialize
strFile = Dir(&quot;c:\be\&quot;)
Do While Len(strFile) > 0
If Right(strFile, 3) = &quot;rep&quot; Then
myfilename(X, 0) = fso.GetFile(&quot;c:\be\&quot; & strFile).DateLastModified
myfilename(X, 1) = strFile
X = X + 1
End If
strFile = Dir
Loop
If myfilename(0, 0) = &quot;&quot; Then
myfilename(0, 0) = &quot;No Files&quot;
myfilename(0, 1) = &quot;Of Your Type&quot;
End If
Entries = X - 1
ReturnVal = Entries
Case acLBOpen
ReturnVal = Timer ' generate unique ID for control
Case acLBGetRowCount ' get number of rows
ReturnVal = Entries
Case acLBGetColumnCount 'get number of columns
ReturnVal = fld.ColumnCount
Case acLBGetColumnWidth
ReturnVal = -1 ' -1 forces use of default width
Case acLBGetValue
ReturnVal = myfilename(row, col)
Case acLBEnd
Erase myfilename
End Select
FileListing2 = ReturnVal
Exit Function
errHandler:
MsgBox Err.Number & &quot; : &quot; & Err.Description, vbCritical, &quot;Error&quot;
Exit Function

End Function
 
The Second suggestion worked great.
 
Also how would I sort by Modified Date???
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top