msaccess - 将sql查询存储在外部文件中

时间:2015-08-11 19:38:15

标签: sql ms-access external

我不喜欢MsAccess的内置文本编辑器,并且想使用外部文本编辑器。

扩展上一个问题:msaccess - sql view - autocomplete / intellisense or alternate way to write queries?

有没有办法将sql查询存储在外部文件中并让MsAccess引用它?

1 个答案:

答案 0 :(得分:1)

如果您可以通过VBA设置记录源,那么您可以使用:

Public Function ReadTxt(filePath As String) As String

    Dim oFSO As FileSystemObject
    Set oFSO = New FileSystemObject

    Dim oFS As TextStream

    If oFSO.FileExists(filePath) Then

        On Error GoTo Err

        Set oFS = oFSO.OpenTextFile(filePath)
        ' read file
        ReadTxt = oFS.ReadAll
        'Debug.Print IIf(oFS Is Nothing, "file is closed", "file opened")
        oFS.Close
    Else
        MsgBox "The file path is invalid.", vbCritical, vbNullString
        Exit Function
    End If

    Exit Function

Err:
    MsgBox "Error while reading the file.", vbCritical, vbNullString
    oFS.Close
    Exit Function

End Function

用法:ReadTxt("C:\TempFolder\YourQuery.txt")

但是,它有很多摆弄,为什么不将它(SQL)剪切并粘贴到Access中?