code for keeping entered data constant

viv345's Avatar, Join Date: Feb 2010
Contributor
How can i keep my data constant after quitting from program. For example:-
If i have entered Potato in any text box. when quit and reopen the program the word potato remains their.
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Save them to a file and reload the content from file when you run next time.
viv345 like this
0
virxen's Avatar, Join Date: Nov 2009
Pro contributor
or you can use the registry to accomplish that.
viv345 like this
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Quote:
Originally Posted by virxen View Post
or you can use the registry to accomplish that.
I don't think that is a good idea and any application specific data should be stored in registry and not user specific data of what he enters. Just semantics.
viv345 like this
0
viv345's Avatar, Join Date: Feb 2010
Contributor
How? Any example. Please
Thanks
0
virxen's Avatar, Join Date: Nov 2009
Pro contributor
Quote:
Originally Posted by viv345 View Post
How? Any example. Please
Thanks
first of all name your vb version
for example vb6 , vb2008
viv345 like this
0
viv345's Avatar, Join Date: Feb 2010
Contributor
In VB6
0
virxen's Avatar, Join Date: Nov 2009
Pro contributor
with the file method
==================
you need a form with a textbox named text1

Code:
Option Explicit
Dim filename As String
Dim filepath As String
Dim text As String

Private Sub Form_Load()'when you load form ,checks if the file exists and reads data and stores to text var. and textbox1
filename = "datastore.txt"
filepath = App.Path + "\" + filename
Dim f As Integer
f = FreeFile
If Dir(filepath) = filename Then
Open filepath For Input As #f
Line Input #f, text
Text1.text = text
Close #f
End If
End Sub

Private Sub Form_Terminate()'when the form terminates,exits-->save text to file
Dim f As Integer
f = FreeFile
Open filepath For Output As #f
Print #f, text
Close #f
End Sub

Private Sub Text1_Change()'when you alter text save it to text variable
text = Text1.text
End Sub
viv345 like this
0
viv345's Avatar, Join Date: Feb 2010
Contributor
Tried:
Given : filepath = App.Path + "\" + filename as C:\Datastore.text
and form 1 with text1.text
BUT IT not worked
Project attached

Attached Files
File Type: zip Project1.zip (1.4 KB, 4 views)
0
virxen's Avatar, Join Date: Nov 2009
Pro contributor
the program works great when you press X to close your form.

I never told you anything about a command button.
If you want to end your program with a command button then before "end"
copy paste the form_terminate() code

like the code below

Code:
Private Sub Command1_Click()
Dim f As Integer
f = FreeFile
Open filepath For Output As #f
Print #f, text
Close #f
End
End Sub