Is there a componet that will allow me to browse a drive for files? If not is there a way to create one? I am trying to import a file, but I want to user to be able to search their computer for it. Please help.
The Browse drive for files will not work for me. I was going to use the Microsoft Common Dialog box, but then discovered that Browsing for a file will not work.
What I want to do is allow a user to select the file they want to import. I want to be able to grab file names in a directory and display them in a listbox. Then I want to place a selected filename in a varible that will be used in DoCmd.TransferText acImportDelim "filename.txt".
Hope this makes sense. If not, I will try to explain better.
What I want to do is allow a user to select the file they want to import. I want to be able to grab file names in a directory and display them in a listbox.
This code will allow you to fill a combo box with the files found in the C:\Temp\ directory.
My example is using a combo box named cbSelectFile.
Code:
[COLOR=Green]'Combo box named [cbSelectFile][/COLOR][COLOR=Green]'Row Source Type set to [Value List][/COLOR]
[COLOR=RoyalBlue]Const cPath = "C:\Temp\"[/COLOR]
Private Sub Form_Open(Cancel As Integer)
Call Update_cbSelectFile
End Sub
Public Function Update_cbSelectFile()
Dim sPath As String
Dim sFileList As String
Dim sFilename As String
sFileList = ""
sFilename = Dir(cPath)
Do While sFilename <> ""
sFileList = sFileList & sFilename & ";"
sFilename = Dir
Loop
Me.cbSelectFile.RowSource = sFileList
Me.cbSelectFile.Requery
End Function
Thanks alot for that ghudson, it is an excellent piece of code but i have been unable to modify to work in my environment. I do not have an excellent knowledge of vb as well which doesn't help matters.
Basically when i browse for a file, the file name i choose populates the box but when i press import it tries to import win.ini as a test. i have tried to change it but i just cant quite understand what i need to change to get it to function properly.
You just need to remove the Win.ini stuff [that was only for testing] and replace it with the value of the text box [tbFile] that is storing the file path\name info. Like this...
Code:
Private Sub bImport_Click()
On Error GoTo Err_bImport_Click
Me.tbHidden.SetFocus
If IsNull(tbFile) Or tbFile = "" Then
MsgBox "Please browse and select a valid file to import.", vbCritical, "Invalid File"
Else
DoCmd.TransferText acImportDelim, , "tImport", tbFile
End If
Exit_bImport_Click:
Exit Sub
Err_bImport_Click:
MsgBox Err.Number & " - " & Err.Description
Resume Exit_bImport_Click
End Sub
The transfer method depends on the type of file you are trying to import. My example is for a text file. You would use the TransferSpreadsheet method for an Excel file.