Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ms access 在文件夹中的所有Access文件中运行Access VBA_Ms Access_Vba - Fatal编程技术网

Ms access 在文件夹中的所有Access文件中运行Access VBA

Ms access 在文件夹中的所有Access文件中运行Access VBA,ms-access,vba,Ms Access,Vba,我一直在试图找到一种方法,在文件夹中的所有文件上运行以下VBA代码,而不必手动打开每个文件 这是我现在拥有的代码(将所需的表导出为带分隔符的txt文件,包括列名): 在研究这个问题时,我发现它在excel中工作,但我不确定如何在access中更改变量,尤其是工作簿引用 谢谢大家! 编辑--有效的代码: Dim FS As FileSystemObject Set FS = New FileSystemObject Dim MyFolder As Folder Set MyFolder = FS.

我一直在试图找到一种方法,在文件夹中的所有文件上运行以下VBA代码,而不必手动打开每个文件

这是我现在拥有的代码(将所需的表导出为带分隔符的txt文件,包括列名):

在研究这个问题时,我发现它在excel中工作,但我不确定如何在access中更改变量,尤其是工作簿引用

谢谢大家!

编辑--有效的代码:

Dim FS As FileSystemObject
Set FS = New FileSystemObject
Dim MyFolder As Folder
Set MyFolder = FS.GetFolder("C:\Users\name\Downloads\cnt\Folder")
Dim MyFile As File
Set appAccess = CreateObject("Access.Application")

For Each MyFile In MyFolder.Files
    appAccess.OpenCurrentDatabase (MyFile.Path)
    appAccess.Visible = True
    NewFileName = MyFile.Path & ".txt"
    appAccess.DoCmd.TransferText acExportDelim, "", "tblScTurCount", NewFileName, True
    appAccess.CloseCurrentDatabase
Next

考虑使用
文件系统对象

为此,您必须在
Microsoft脚本运行时
库中添加
参考
。(转到VBA编辑器中的工具>参考…)


考虑到您正在Access中使用VBA,请使用或方法在Access中打开文件

考虑使用
文件系统对象

为此,您必须在
Microsoft脚本运行时
库中添加
参考
。(转到VBA编辑器中的工具>参考…)


考虑到您正在Access中使用VBA,请使用或方法在Access中打开文件

但我并不完全理解。您使用的是Access还是Excel?但我不完全理解。您使用的是Access还是Excel?谢谢您的建议。不幸的是,在
Set MyFolder=FS.GetFolder(“C:\path\of\the\folder”)
上,我得到了“运行时错误'91':对象变量或未设置块变量”作为参考,我添加了Microsoft脚本运行时库,并将“C:\path\of\the\folder”更改为我文件夹的实际路径。再次感谢你!哦,很抱歉,我会更新它,忘了创建FS对象。我想它差不多就在那里了,我只是不确定如何在op中的
DoCmd.TransferText
语句中使用变量
MyFile
。具体来说,我如何在MyFile中的表上运行该命令(这些表都被命名为“tblSCTurCount”)。我还编辑了原始帖子,以显示我当前的代码是什么样子的。您是否使用Access或Excel?我不明白你想做什么。什么是
DoCmd
MyFile
是目录中的一个文件。它有许多属性,您可以使用MyFile查看。(在MyFile之后键入句点)。它有
名称
路径
以及您可以获得的其他属性。谢谢您的建议。不幸的是,在
Set MyFolder=FS.GetFolder(“C:\path\of\the\folder”)
上,我得到了“运行时错误'91':对象变量或未设置块变量”作为参考,我添加了Microsoft脚本运行时库,并将“C:\path\of\the\folder”更改为我文件夹的实际路径。再次感谢你!哦,很抱歉,我会更新它,忘了创建FS对象。我想它差不多就在那里了,我只是不确定如何在op中的
DoCmd.TransferText
语句中使用变量
MyFile
。具体来说,我如何在MyFile中的表上运行该命令(这些表都被命名为“tblSCTurCount”)。我还编辑了原始帖子,以显示我当前的代码是什么样子的。您是否使用Access或Excel?我不明白你想做什么。什么是
DoCmd
MyFile
是目录中的一个文件。它有许多属性,您可以使用MyFile查看。(在MyFile之后键入句点)。它具有
名称
路径
以及您可以获得的其他属性。
Dim FS As FileSystemObject
Set FS = New FileSystemObject
Dim MyFolder As Folder
Set MyFolder = FS.GetFolder("C:\Users\name\Downloads\cnt\Folder")
Dim MyFile As File
Set appAccess = CreateObject("Access.Application")

For Each MyFile In MyFolder.Files
    appAccess.OpenCurrentDatabase (MyFile.Path)
    appAccess.Visible = True
    NewFileName = MyFile.Path & ".txt"
    appAccess.DoCmd.TransferText acExportDelim, "", "tblScTurCount", NewFileName, True
    appAccess.CloseCurrentDatabase
Next
Sub test()
    Dim FS As FileSystemObject
    Set FS = New FileSystemObject

    Dim MyFolder As Folder
    Set MyFolder = FS.GetFolder("C:\path\of\the\folder")

    Dim MyFile As File
    For Each MyFile In MyFolder.Files
        'do what you want to do with each file

        'to use the file name:
        MyFile.Name

        'I suppose you have to:
        Application.OpenCurrentDatabase MyFile.Path
        '(please verify if this path contains the filename and extension too). 

         'But create a different filename for each txt:
         NewFileName = MyFile.Path + ".txt"

        'Then you do:
        DoCmd.TransferText acExportDelim, "", "tblScTurCount", NewFileName, True

    Next
End Sub