msaccess - storing sql query in external file

纵饮孤独 提交于 2019-12-12 03:37:28

问题


I dislike the built in text editor for MsAccess and would like to use an external text editor.

Expanding on a previous question: msaccess - sql view - autocomplete / intellisense or alternate way to write queries?

Is there a way I can store the sql query in an external file and have MsAccess reference it?


回答1:


If you are OK with setting the recordsource via VBA, then you can use this:

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

Usage: ReadTxt("C:\TempFolder\YourQuery.txt")

However, it's a lot of fiddling around, why not just cut and paste it (the SQL) into Access?



来源:https://stackoverflow.com/questions/31950497/msaccess-storing-sql-query-in-external-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!