how would i open .txt file as open readonly so user can't change data.
Printable View
how would i open .txt file as open readonly so user can't change data.
you can't control that, yur best bet would be to make a text box in your program and lock it so no one can edit it
just make a new form with it in it or somehthing
Check the "Open" command and just open it for "read". Then don't allow it to be saved. You tell the user as he opens the file that he will not be able to save changes.
Code:'if you want the file to be read only you can set
'the attributes and it would go like this.
Option Explicit
Public myFile As String
Private Sub Command1_Click()
myFile = "C:\my documents\myfile.txt"
SetAttr myFile, vbReadOnly
Shell "C:\WINDOWS\NOTEPAD.EXE " & myFile, 1
'now you can make changes and try to save the file
'and you won't be able to.
'you must reset the attributes when you close your app
'or the file will remain read only.
End Sub
'OR
'
'if you wish to use the textbox it goes like this
'
Private Sub Form_Load()
'add a textbox, set it to multiline and scrollbars
'Open a file in binary mode and read into a textbox or string
'this opens your file and dumps it into a textbox
'the user can do what they want but since it is only a textbox
'none of the changes will reflect inside the file
Dim sHolder As String
Dim intNum As Integer
Dim sFileName As String
'open for binary and read
sFileName = "C:\My Documents\MyFile.txt"
intNum = FreeFile
Open sFileName For Binary As intNum
sHolder = Space(LOF(1))
Get #1, , sHolder
Close intNum
'assign the text to a textbox
Text1 = sHolder ' for a string (strString = sholder)
End Sub
'this sets the file back to normal
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
SetAttr myFile, vbNormal
End Sub
Private Sub Form_Terminate()
SetAttr myFile, vbNormal
End Sub
Private Sub Form_Unload(Cancel As Integer)
SetAttr myFile, vbNormal
End Sub