Can VBA open a txt file?

access83

Registered User.
Local time
Today, 11:09
Joined
Apr 25, 2012
Messages
22
Hi All,

I have a table with the file paths of solutions to problems. What I want to do is select the solution to a problem, click a button 'View' and the solution file will open. The solution files will be txt files and maybe .sql files. Is this possible? I've been googling it but most of what I find is opening text files to read info into Access.

Thanks in advance :)
 
Thanks for that! :)

I had been working on displaying a hyperlink on a form and when the user clicks that the file opens. I have the file path displayed in a text box and changed the 'Is Hyperlink' property to Yes and 'Display as Hyperlink' to Always. I put some code on the onClick property of this text box...

Code:
DoCmd.RunCommand acCmdOpenHyperlink

but when I click on this I get the error..

"Run-time error '2046':
The command or action 'OpenHyperlink' isnt available now"

Do you know why this is or how to fix it? :confused::confused:
 
And the field doesn't need to be defined as a hyperlink data type. You just use the FollowHyperlink method as shown by vbaInet and pass it a valid file name or web address or even email address. As long as the extension is registered, the correct program will open.
 
Hi,

Further to the previous suggestions it is possible to use VBA to read the content of a text file and display it in the database without opening an external file.

This example will read a text-file and display the results in a message box. This requires adding a reference called "Microsoft Scripting Runtime". It would be possible to display the text on a form rather than using the messagebox function.

Code:
Sub DisplayTextFile()
    Dim fso As New FileSystemObject
    Dim t As TextStream
    Dim strFilePath As String
    Dim strBuffer As String
    
    strFilePath = "C:\test.txt"
    
    If fso.FileExists(strFilePath) Then
        Set t = fso.OpenTextFile(strFilePath, ForReading, False)
        strBuffer = t.ReadAll
        MsgBox strBuffer
        t.Close
    End If
    
    Set t = Nothing
End Sub
 
Here's another method
Code:
'---------------------------------------------------------------------------------------
' Procedure : FileToString
' Author    : Jack
' Date      :
' Purpose   : To read a text file and display the last 50 characters (tail)
'---------------------------------------------------------------------------------------
'
Function FileToString()
Dim MyFileNum As Integer
Dim theData As Variant
'--
' FileToString(FILEInput$ as Variant) 'to make this a callable function
Dim FILEInput$ as Variant
'--
   On Error GoTo FileToString_Error

FILEInput$ = "C:\Users\jack\documents\ATT_AlbertM_chat.txt"
MyFileNum = FreeFile
Open FILEInput$ For Input As MyFileNum

theData = Input$(LOF(MyFileNum), MyFileNum)
Close MyFileNum

MsgBox "File Length is " & Len(theData) & vbCrLf & "Last 50 chars (tail)-->" & Right(theData, 50) & "<"
FileToString = theData

   On Error GoTo 0
   Exit Function

FileToString_Error:

    MsgBox "Error " & Err.number & " (" & Err.Description & ") in procedure FileToString of Module jModule"
End Function
 
Last edited:

Users who are viewing this thread

Back
Top Bottom