问题
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