MS-ACCESS Copying file via vba with file dialog

9.6k views Asked by At

I'm trying to create a button that opens up a file dialog, then lets you select a image to copy into a folder with the database. I've been working with this code but I'm stuck at the filecopy command, I can't seem to format it correctly. I use the pathway of the database plus a few folders then finally a combo box to select the specific folder to create the pathway (so that it doesn't break if the database is moved, and the combo box sorts the images based on category). Here's the code I've been using. Thanks guys.

Private Sub Command156_Click()

   Dim fDialog As Office.FileDialog
   Set fd = Application.FileDialog(msoFileDialogFilePicker)
   Dim varFile As Variant



   ' Set up the File Dialog. '
   Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
    fd.InitialFileName = [Application].[CurrentProject].[Path]
   With fDialog

      ' Allow user to make multiple selections in dialog box '
      .AllowMultiSelect = False

      ' Set the title of the dialog box. '
      .Title = "Please select a Image"

      ' Clear out the current filters, and add our own.'
      .Filters.Clear
      .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

     filecopy([.SelectedItems],[GetDBPath] & "\Images\Equipment\" & Combo153)

      Else

      End If
   End With
End Sub
2

There are 2 answers

0
engineersmnky On

I have answered this question here but I'll repost for you

Here is a concept

Sub Locate_File()
   Dim fDialog As Office.FileDialog
   Dim file_path As String
   Set fDialog = Application.FileDialog(msoFileDialogFilePicker)

   With fDialog  
    'Set the title of the dialog box.
    .Title = "Please select one or more files"

    'Clear out the current filters, and add our own.
    .Filters.Clear
    .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
       file_path = .SelectedItems(1)
       Copy_file file_path,Combo153
    Else
       MsgBox "You clicked Cancel in the file dialog box."
    End If
  End With
End

Sub Copy_file(old_path As String, file_name As String)
  Dim fs As Object
  Dim images_path As String
  images_path = CurrentProject.Path & "\Images\Equipment\"
  Set fs = CreateObject("Scripting.FileSystemObject")
  fs.CopyFile old_path, images_path  & file_name
  Set fs = Nothing
End

You may need to make changes and you must require the Microsoft Office 12.0 Object Library for FileDialog to work. Much of the FileDialog code was taken from Microsoft.

0
user2744572 On

Using Siddharth routs suggestion, I removed the extra brackets and made a few tweaks and voila! the code worked. I tried engineersmnky method but the pathway wasn't generating correctly. To fix the code itself, the only real error was that on the destination part of the file copy, there was no file name, So I used

Dir(Trim(.SelectedItems.Item(1)

To get the file name and tacked it on the end. Heres the rest of the code for anyone else who wants it.

Private Sub Command156_Click()

   Dim fDialog As Office.FileDialog
   Set fd = Application.FileDialog(msoFileDialogFilePicker)
   Dim varFile As Variant



   ' Set up the File Dialog. '
   Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
    fd.InitialFileName = Application.CurrentProject.Path
   With fDialog

      ' Allow user to make multiple selections in dialog box '
      .AllowMultiSelect = False

      ' Set the title of the dialog box. '
      .Title = "Please select a Image"

      ' Clear out the current filters, and add our own.'
      .Filters.Clear
      .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
      ' This section takes the selected image and copy's it to the generated path'
      ' the string takes the file location, navigates to the image folder, uses the combo box selection to decide the file category, then uses the name from the filedialog to finish the path'
     FileCopy .SelectedItems(1), Application.CurrentProject.Path & "\Images\Equipment\" & Combo153 & "\" & Dir(Trim(.SelectedItems.Item(1)))


      Else

      End If
   End With
End Sub