Aiysha
asked on
read and write a string into a txt file.
I want a vb command that saves values from text box or a string into a txt file and also reads that line back into vb code.
Thank you.
Thank you.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Instead using a text file for storing some data you can
use the registry very easily to store short informations :
This sample creates the following key in the registry :
HKCU\Software\VB and VBA Program Settings\Project1\Settings
Under "HKCU\...\Project\" it creates the additional key "Settings" with the value
"Hey,I love Microsoft"
Private Sub Form_Load()
zzz = "Hey,I love Microsoft"
'S A V I N G :
'= = = = = = = =
'Set some data to registry
SaveSetting App.Title, "Settings", "firmName", zzz ' "App.Title" contains the variable "Project1"
'R E A D I N G :
'= = = = = = = =
'read some data from registry
qqq = GetSetting(App.Title, "Settings", "firmName", "sorry, but the registry entry 'firmName' doesn't exist")
MsgBox qqq
' RESULT : a message box pops up with the text - - -> Hey,I love Microsoft
End Sub
'Annotation:
'The last parameter of the GetSetting() function allows to supply a DEFAULT value
'if the requested registry entry is not found...
'Counter = GetSetting(App.Title, "Settings", "Counter", "-1")
'If Counter = "-1" Then ' there was no value found in registry
use the registry very easily to store short informations :
This sample creates the following key in the registry :
HKCU\Software\VB and VBA Program Settings\Project1\Settings
Under "HKCU\...\Project\" it creates the additional key "Settings" with the value
"Hey,I love Microsoft"
Private Sub Form_Load()
zzz = "Hey,I love Microsoft"
'S A V I N G :
'= = = = = = = =
'Set some data to registry
SaveSetting App.Title, "Settings", "firmName", zzz ' "App.Title" contains the variable "Project1"
'R E A D I N G :
'= = = = = = = =
'read some data from registry
qqq = GetSetting(App.Title, "Settings", "firmName", "sorry, but the registry entry 'firmName' doesn't exist")
MsgBox qqq
' RESULT : a message box pops up with the text - - -> Hey,I love Microsoft
End Sub
'Annotation:
'The last parameter of the GetSetting() function allows to supply a DEFAULT value
'if the requested registry entry is not found...
'Counter = GetSetting(App.Title, "Settings", "Counter", "-1")
'If Counter = "-1" Then ' there was no value found in registry
This sample stores the textbox content
(Text1.text) in the registry.
-Everytimes when you restart the vb program, you will see
the text1.text from your 'last session' .
Needs:
======
1 Textbox
1 Form
Private Sub Form_Load()
Text1.Width = 3000
If zz1 = "-1" Then
Text1 = "Please enter some text"
Else
Text1 = zz1
End If
End Sub
Private Sub Text1_Change()
zz9 'store text1 content
End Sub
Function zz1() As String ' read
zz1 = GetSetting(App.Title, "Settings", "textbox1", "-1")
End Function
Sub zz9() ' save
SaveSetting App.Title, "Settings", "textbox1", Text1.Text
End Sub
(Text1.text) in the registry.
-Everytimes when you restart the vb program, you will see
the text1.text from your 'last session' .
Needs:
======
1 Textbox
1 Form
Private Sub Form_Load()
Text1.Width = 3000
If zz1 = "-1" Then
Text1 = "Please enter some text"
Else
Text1 = zz1
End If
End Sub
Private Sub Text1_Change()
zz9 'store text1 content
End Sub
Function zz1() As String ' read
zz1 = GetSetting(App.Title, "Settings", "textbox1", "-1")
End Function
Sub zz9() ' save
SaveSetting App.Title, "Settings", "textbox1", Text1.Text
End Sub
Sub GetSettings()
txtMyTextField.Text = GetSetting(App.EXEName, "Settings", "MyTextField", "")
End Sub
Sub SaveSettings()
Call SaveSetting(App.EXEName, "Settings", "MyTextField", txtMyTextField.Text)
End Sub
and then call GetSettings from your form load or wherever, and SaveSettings from the change event or the form close event or wherever.
If you want to specify the file name, you could use the windows API calls GetPrivateProfileString and WritePrivateProfileString,