用于MS Access查询的Application.LoadFromText的替代方法

时间:2009-05-13 18:18:15

标签: ms-access vba vbscript

我正在尝试使用VBScript从文本文件,查询加载到MS Access查询集合。我使用的是这样的东西:  该代码源自Here

 for each myFile in folder.Files
    objecttype = fso.GetExtensionName(myFile.Name)
    objectname = fso.GetBaseName(myFile.Name)
    WScript.Echo "  " & objectname & " (" & objecttype & ")"

    if (objecttype = "form") then
        oApplication.LoadFromText acForm, objectname, myFile.Path
    elseif (objecttype = "bas") then
        oApplication.LoadFromText acModule, objectname, myFile.Path
    elseif (objecttype = "mac") then
        oApplication.LoadFromText acMacro, objectname, myFile.Path
    elseif (objecttype = "report") then
        oApplication.LoadFromText acReport, objectname, myFile.Path
    elseif (objecttype = "sql") then
        'oApplication.LoadFromText acQuery, objectname, myFile.Path
        ' Add create querydef code here
    end if

 next

但我不确定如何使用VBScript创建查询定义。

有什么想法吗?

注意: 我最初使用以下内容导出到文件:

For Each myObj In oApplication.CurrentDb.QueryDefs 
   Set f = fso.CreateTextFile(sExportpath & "\" & myObj.Name & ".sql", True) 
   f.WriteLine(myObj.SQL) 
   f.Close 
Next

2 个答案:

答案 0 :(得分:4)

这会保存查询defs

For i = 0 To db.QueryDefs.Count - 1
    Application.SaveAsText acQuery, db.QueryDefs(i).Name, sExportpath & "\" & db.QueryDefs(i).Name & ".sql"
Next i

然后LoadFromText应该工作

答案 1 :(得分:3)

为了完整起见,我想添加我的解决方案。

if (objecttype = "form") then
    oApplication.LoadFromText acForm, objectname, myFile.Path
elseif (objecttype = "bas") then
    oApplication.LoadFromText acModule, objectname, myFile.Path
elseif (objecttype = "mac") then
    oApplication.LoadFromText acMacro, objectname, myFile.Path
elseif (objecttype = "report") then
    oApplication.LoadFromText acReport, objectname, myFile.Path
elseif (objecttype = "sql") then
    oApplication.LoadFromText acQuery, objectname, myFile.Path
end if

显然,所有需要添加的内容都是:

  Const acQuery = 1

感谢DJ