MS-ACCESS 通过带有文件对话框的 vba 复制文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20508520/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 00:54:02  来源:igfitidea点击:

MS-ACCESS Copying file via vba with file dialog

vbams-accessfiledialog

提问by user2744572

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.

我正在尝试创建一个打开文件对话框的按钮,然后让您选择要复制到数据库文件夹中的图像。我一直在使用这段代码,但我被困在 filecopy 命令上,我似乎无法正确格式化它。我使用数据库的路径加上几个文件夹,最后是一个组合框来选择特定的文件夹来创建路径(这样在移动数据库时它不会中断,并且组合框根据类别对图像进行排序) . 这是我一直在使用的代码。谢谢你们。

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

回答by engineersmnky

I have answered this question herebut 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.

您可能需要进行更改,并且必须要求 Microsoft Office 12.0 Object Library for FileDialog 才能工作。大部分 FileDialog 代码取自Microsoft.

回答by user2744572

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

使用 Siddharth routs 建议,我删除了额外的括号并进行了一些调整,瞧!代码有效。我尝试了engineeringmnky方法,但路径没有正确生成。为了修复代码本身,唯一真正的错误是在文件副本的目标部分,没有文件名,所以我使用

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