Try to get file from Window Explorer

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Is it possible to select a certain file to copy into a certain folder using
the file control box. Those you always see when you open a file in
applications. if I am not wrong, Visual Basic can create forms to display
files that are in the harddisk but I seem to forget. Sighx.

What I am trying to do is to copy 2 files from a certain folder to my folder
so macro can be carried out after the file is in the folder I want. At the
end of the Import, the files in the import folder will be deleted so as to
allow the second date to be import to the timecard.
 
Yes it is ez... there are several options depending what version of Access
you are using. Which version of Access are you using? 97, 2000, 2002???
 
if you already know the file path and destination path you can use syntax like
Dim SourceFile, DestinationFile
SourceFile = "SRCFILE" ' Define source file name.
DestinationFile = "DESTFILE" ' Define target file name.
FileCopy SourceFile, DestinationFile ' Copy source to target.
 
If you are using 2002 you can use code like this to get values into a list
box or whatever... I like list boxes so I use them extensively. You can
select as many files as you want, store them in your listbox and then copy
them based on... wherever you want to put them. I have a form that has the
listbox on it. I select a contractID from a combo which populates a lable (I
used to use a txt box but my data kept disapearing. so I use the path to copy
into an XML parser that gets the xml out of SQL server where it is stored and
parses it to a text file. when I click the button to pull up the parser
(using the shell command) the data stored in my lable storage place is passed
to a listbox. then when I am ready to import my text files to tables in
access, I select the files in the list box and click the button.

this is based on the code at MSDN
Private Sub cmdGetFileNames_Click()
On Error GoTo Err_cmdGetFileNames_Click

Dim fDialog As Office.FileDialog
Dim varFile As Variant

'Clear listbox contents.
Me.lstFileList.RowSource = ""

'Set up the File Dialog.
Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
With fDialog
.AllowMultiSelect = True
.Title = "Please select one or more files"
.Filters.Clear
.Filters.Add "Access Databases", "*.MDB"
.Filters.Add "Excel Workbooks", "*.XLS"
.Filters.Add "Text Files", "*.TXT"
.Filters.Add "All Files", "*.*"

'Show the dialog box. If the .Show method returns True, the
'user picked at least one file. If the .Show method returns
'False, the user clicked Cancel.
If .Show = True Then
'Loop through each file selected and add it to our list box.
'I've added to a list box... You could put the code i provided
previously with
'a slight hack and copy the files you need using filecopy.
For Each varFile In .SelectedItems
Me.lstFileList.AddItem varFile

Next
Else
MsgBox "You clicked Cancel in the file dialog box."
End If
End With

Exit_cmdGetFileNames_Click:
Exit Sub

Enjoy
rev
 
I will go try it. What If I want to do it in way like when you open a file.
It will pop up an open file forms where user can select the file he or she
want? (Something similar to the Windows Open File Function? Is this possible?
 
I have modified your source code and just can't find a way to do the copy
file function. I need to copy 2 file of different name to a different
destination. How do I go about it?

Private Sub UpdateFiles_Click()
On Error GoTo Err_UpdateFiles_Click

Dim fDialog As Office.FileDialog
Dim varFile As Variant
Dim Sourcefile, DestinationFile

'Clear Listbox Contents.
Me.lstFilelist.RowSource = ""

'Set up the File Dialog.
Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
With fDialog
.AllowMultiSelect = True
.Title = "Please Select The Location Of Tim1 And Tim2 You Want To
Import"
.Filters.Clear
.Filters.Add "DBase Files", "*.dbf"
'Show the dialog box. If the .Show method returns True, the
'User Picked At Least One File. If The .Show Method Returns
'False, The User Clicked Cancel.
If .Show = True Then
' Loop through each file selected and add it to our list box.
' I've added to a list box...You Could Put the code I Provided
previously with
' A slight hack and copy the files you need using filecopy.
For Each varFile In .SelectedItems
Me.lstFilelist.AddItem varFile
Sourcefile = "SRCFile" ' Define Source File Name
DestinationFile = "DESTFile" ' Define Target File Name
FileCopy Sourcefile, DestinationFile ' Copy Source To Target

Next
Else
MsgBox "The Action Is Cancelled."
End If
End With
DoCmd.RunMacro "Update TimeCard"
MsgBox "Update Completed", vbOKCancel, "Message Box"

ExitErr_UpdateFiles_Click:
DoCmd.SetWarnings True
DoCmd.Hourglass False
Exit Sub

Err_UpdateFiles_Click:
MsgBox Err.Description
Resume ExitErr_UpdateFiles_Click

End Sub
 
Back
Top