2015-11-10 32 views
0

我需要从DB Access 2007导出我的表以使用VBScript与;分隔txt文件。使用VBScript导出访问表以分隔txt文件

我下面的代码:

Set accDB = CreateObject("Access.Application")  
     accDB.visible = true 
     accDB.automationsecurity = 1 
     accDB.OpenCurrentDatabase("D:\Users\db2015.mdb")   
     accDB.DoCmd.TransferText acExportDelim, ";", "tb2015", "D:\Users\tb2015.txt", False 
     accDB.CloseCurrentDatabase 
     accDB.Quit   
    Set accDB = Nothing 

但我有错误:tb2015.txt找不到

我试图加在我的代码如下:

accDB.DoCmd.OpenQuery "SelectQuery", acNormal, acEdit 
    accDB.DoCmd.OutputTo acOutputTable, "tb2015", "txt", "D:\Users\tb2015.txt" 

在这种情况下,我不没有错误,但tb2015.txt与|而不是;

请帮帮我。

预先感谢您。

编辑#1

这是新的代码,但我有错误:

Set accDB = CreateObject("Access.Application")  
     accDB.visible = true 
     accDB.automationsecurity = 1 
     accDB.OpenCurrentDatabase("D:\Users\db2015.mdb") 
     Const acExportDelim = 2  
     accDB.DoCmd.TransferText acExportDelim, , "tb2015", "D:\Users\tb2015.txt", False 
     accDB.CloseCurrentDatabase 
     accDB.Quit   
    Set accDB = Nothing 

编辑#2

解决的办法是这样的代码:

Const acExportDelim = 2 
    Set accDB = CreateObject("Access.Application") 
     accDB.visible = true 
     accDB.automationsecurity = 1 
     accDB.OpenCurrentDatabase("D:\Users\db2015.mdb")   
     accDB.DoCmd.OpenQuery "myView", acNormal, acEdit 
     accDB.DoCmd.TransferText acExportDelim, "tb2015", "tb2015", "D:\Users\tb2015.txt" 
     accDB.CloseCurrentDatabase 
     accDB.Quit 
    Set accDB = Nothing 
+0

你可以尝试http://stackoverflow.com/questions/13482242/create-comma-separated-file-csv-from-access-scheduled-daily-from-windows/13483597#13483597我非常怀疑,“; “是您的规范的名称,请参阅https://msdn.microsoft.com/en-us/library/bb214141(v=office.12).aspx – Fionnuala

+0

*“但我有错误”* - 什么错误?在哪一行? – Andre

+0

对不起,我有第11行错误'accDB.DoCmd.TransferText acExportDelim,,“tb2015”,“D:\ Users \ tb2015.txt”,False错误是**为行动或方法需要主题** –

回答

0

这里发生的是:

VBScript不知道acExportDelim,所以它被解释为未知的变数,有值为0

DoCmd.TransferText,0 = acImportDelim上下文。

因此,它试图导入tb2015.txt,并得到“文件未找到”的错误。

复制Const acExportDelim = 2从访问对象目录,并将其添加到您的脚本。并删除“;” SpecificationName参数。

注意:acOutputTable = 0,这就是为什么您的DoCmd.OutputTo运行(意外)。

为避免出现此类错误,请将Option Explicit添加到脚本的顶部,它强制执行变量声明。

+0

感谢您的帮助,请参阅我的编辑#1的第一个问题,建议不起作用。 –

相关问题