【问题标题】:How to insert file names into access table with VBA如何使用 VBA 将文件名插入到访问表中
【发布时间】:2018-04-24 14:44:46
【问题描述】:

我需要一些帮助... 我有一个访问权限的子目录,我在某个文件夹中读取所有扩展名为 .gif 的文件,并且想知道如何继续插入在访问表中读取的所有名称,我在下面编写了代码,但它不是工作……你能帮帮我吗?

Sub realAllFiles ()

Dim varFile As Variant
Dim CustomerFolder As String
Dim fDialog As FileDialog, result As Integer
Set fDialog = Application.FileDialog(msoFileDialogFilePicker)

With fDialog

.AllowMultiSelect = True
.Title = "Upload | Selecione a pasta de imagens capturadas..."
.Filters.Clear
.Filters.Add "All files", "*.gif*"
.InitialFileName = "H:\Gestao de Dados Processamento\FAP\Baixa de Pendencia DUT\Novo Fluxo"

If .Show = True Then
      For Each varFile In .SelectedItems
         CustomerFile = varFile
      Next
      Else: Exit Sub
   End If
End With

DoCmd.TransferText acImportDelim, , "MyTable", CustomerFile, False

End Sub

【问题讨论】:

  • "it is not working" 是什么意思 - 错误消息?意外行为?
  • 你没有定义表格或字段 - 我想这将是一个开始
  • 阅读有关使用 DAO 记录集的 AddNew 命令的信息 - Docmd.Transfertext 不会按您希望的方式工作。

标签: ms-access vba


【解决方案1】:

有很多方法可以做到这一点。

第一种方法是设置一个导入查询,将文件名作为参数传递。

查询的 SQL:

PARAMETERS [prmFileName] Text (255);
INSERT INTO T ( FieldName )  '<- Change to the actual table and field names
SELECT [prmFileName] As _FileName;

然后在循环内调用查询:

Sub realAllFiles()

    Dim varFile As Variant
    Dim CustomerFolder As String
    Dim fDialog As FileDialog, result As Integer
    Set fDialog = Application.FileDialog(msoFileDialogFilePicker)

    With fDialog
        .AllowMultiSelect = True
        .Title = "Upload | Selecione a pasta de imagens capturadas..."
        .Filters.Clear
        .Filters.Add "All files", "*.gif*"
        .InitialFileName = "H:\Gestao de Dados Processamento\FAP\Baixa de Pendencia DUT\Novo Fluxo"
    End With

    If fDialog.Show = True Then
        For Each varFile In fDialog.SelectedItems
            With CurrentDb().QueryDefs("ImportQueryName") '<- change to the above query's name
                .Parameters("[prmFileName]").Value = varFile
                .Execute dbFailOnError
            End With
        Next
    End If
End Sub

第二种方法是通过记录集:

Sub realAllFiles()

    Dim varFile As Variant
    Dim CustomerFolder As String
    Dim fDialog As FileDialog, result As Integer
    Dim rs As DAO.Recordset

    Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
    Set rs = CurrentDb().OpenRecordset("TableName") '<- change to the actual table name

    With fDialog
        .AllowMultiSelect = True
        .Title = "Upload | Selecione a pasta de imagens capturadas..."
        .Filters.Clear
        .Filters.Add "All files", "*.gif*"
        .InitialFileName = "H:\Gestao de Dados Processamento\FAP\Baixa de Pendencia DUT\Novo Fluxo"
    End With

    If fDialog.Show = True Then
       For Each varFile In fDialog.SelectedItems
          rs.AddNew
          rs("Fieldname").Value = varFile '<- change to the actual field name
          rs.Update
       Next
    End If

    If Not rs Is Nothing Then rs.Close
End Sub

【讨论】:

猜你喜欢
  • 2014-09-22
  • 1970-01-01
  • 1970-01-01
  • 2012-06-12
  • 1970-01-01
  • 1970-01-01
  • 2012-07-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多