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

Combo box lists file names in specific directory 2

Status
Not open for further replies.

mulligh

Technical User
May 8, 2001
97
0
0
US
I want to have a combo box that lists the filenames that are in a specific directory. Only files that have a "txt" or "doc" extension will show up in the list. Users will be able to select one of these files from the list and it will be stored in a table field so that the data string can be used to open or edit that file.

I've done a search and found some examples, but nothing along these lines.

I'm using Access 2002.

Thanks for any help.
 
use the "DIR" command

This code works I just made it
make a new button on your form and paste this VBA coe in the Event Procedure.
----------------------------------
Dim Rs1 As ADODB.Recordset
Dim SQLCode As String
Set Rs1 = New ADODB.Recordset
SQLCode = "Delete from [txt-doc Files];"
Rs1.Open SQLCode, CurrentProject.Connection

MyPath = "c:\" ' Set the path.
MyName = Dir(MyPath, vbDirectory) ' Retrieve the first entry.
Do While MyName <> &quot;&quot; ' Start the loop.
' Ignore the current directory and the encompassing directory.
If MyName <> &quot;.&quot; And MyName <> &quot;..&quot; Then
' Use bitwise comparison to make sure MyName is a directory.
If (GetAttr(MyPath & MyName) And vbDirectory) = vbDirectory Then
'Debug.Print MyName ' this is a folder if you want it
ElseIf Right(MyName, 4) = &quot;.txt&quot; Or Right(MyName, 4) = &quot;.doc&quot; Then
Debug.Print MyName ' these are the files

SQLCode = &quot;INSERT INTO [txt-doc Files] ( Filenames ) SELECT '&quot; & MyName & &quot;' AS Expr1;&quot;
Rs1.Open SQLCode, CurrentProject.Connection

End If ' it represents a directory.
End If
MyName = Dir ' Get next entry.
Loop

Set Rs1 = Nothing
Set Conn2 = Nothing

'refresh combo box
Me!Combo1.Requery
------------------------------

a few things to note:
you need to create a table called &quot;txt-doc Files&quot; spelled exactly that way.
you need to make one field in the table called &quot;Filenames&quot; with a text type and 255 field size, to allow for very long file names.

The combo boxes &quot;Row source&quot; property must be this
SELECT [txt-doc Files].Filenames FROM [txt-doc Files];

Like I said it works


DougP, MCP
 
Doug, thanks so much. This is exactly what I needed. And thanks for taking the time to write the comments throughout the code. Appreciate you taking your time to help me.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top