i want a text box to hold the information entered, so if i close the program or if i shutdown the pc and run the exe the number that i typed will still be there.
is this possible? if so how? thanks
Printable View
i want a text box to hold the information entered, so if i close the program or if i shutdown the pc and run the exe the number that i typed will still be there.
is this possible? if so how? thanks
When the program closes, save the value somewhere.. perhaps to a text file or the registry.
When the program starts, load the value you saved.
If you want to use a text file, see the "Files" section of our Classic VB FAQs (in the FAQ forum, which is shown near the top of our home page)
i am fairly new to programming but can some one provide an example? because i find it a bit difficult and i am trying to learn programming at the same time.
We'll assume your text box is named "textbox1"
In the form unload event enter the following
open app.path & "\mytext.txt" for output as #1
print #1, textbox1.text
close #1
That'll write the contents of your text box into a file
In the form load event enter the following
dim lsText as string
on error resume next 'in case the file does not exist
open app.path & "\mytext.txt" for input as #1
input #1, lsText
close #1
textbox1.text=lsText
This will read the file and write it into your text box as the form opens
Hope this helps
Dave
thanks dave that is what i wanted and works. ok now how would i add another text box to save under the first data which is stored in the text file?
i have tried to change the coding but does not work.
Not sure what it is you want. Can you explain a little clearer please.
If you just want to include data from a second text box, just do this:Quote:
Originally Posted by adam786
open app.path & "\mytext.txt" for output as #1
print #1, textbox1.text
print #1, textbox2.text
close #1
In the form load event enter the following
dim lsText as string
on error resume next 'in case the file does not exist
open app.path & "\mytext.txt" for input as #1
input #1, lsText
Textbox1.text=IsText
input #1, IsText
Textbox2.Text= IsText
close #1
Note, if you are using Vista, you cannot have your app.path be in the Program Files directory. Vista will not allow you to write your text file into the Program Files directory.
Actually, even if you are using XP, you still should not put your text file in the Program Files directory.
i tried that and it only saved text box 2 and not 1. what might the problem be?
I made a typo mistake in the code I gave you.
The line that says:" input #1, lsText" should have read "input #1, IsText"
If you just copied the code you copied that error. change it so it is an "I" (letter I) and not an "l" (letter L) and it'll work.
How aboutCode:Private Sub Form_Load()
If Dir$(App.Path & "\mytext.txt") <> vbNullString Then 'it exists - do it
Open App.Path & "\mytext.txt" For Output as #1
Print #1,Text1.Text & vbCrLf
Print #1, Text2.Text
Close #1
Else
Msgbox App.Path & "\mytext.txt does not exist." 'tell them it does not exist
End If
End Sub
I think you meant to say that if the string does exist, then you want to open it as #1, and then read the values and transfer them to the textboxes. You wouldn't be printing them from the textboxes in the Form_Load event.Quote:
Originally Posted by Hack
If it equalled vbNullString, then the file would not exist.
Although, I noticed I neglected to include the Open statement in my original post. I corrected that.
But my point is that in the Form_Load event, the person needs to open the file for INPUT, not for OUTPUT, which is what you have.Quote:
Originally Posted by Hack
If you're going to open a text file for OUTPUT, you don't need to check if it already exists or not. VB will either create a new file, or it will overwrite the existing file.
Your code should look more like this:
Code:Private Sub Form_Load()
Dim A$
If Dir$(App.Path & "\mytext.txt") <> vbNullString Then 'it exists - do it
Open App.Path & "\mytext.txt" For Input as #1
Input #1, A$
Text1.Text=A$
Input#1, A$
Text2.Text=A$
Close #1
Else
Msgbox App.Path & "\mytext.txt does not exist." 'tell them it does not exist
End If
End Sub
ok thanks for the reply's. the last thing i need is to insert heading in the txt file. so if the heading of the text boxes will be listed and the values underneath the heading i.e.
below is the txt file:
-----------------
textbox1
1111
textbox2
1dddffe
------------------
so on the form which i entered data in the text boxes the data entered in the text box is displayed under the title of the txt box. is there any way to do this? and can any one help?
Sure, when you print to the text file, just print anything you want. You can even print the blank line in between. So it would look something like this.Quote:
Originally Posted by adam786
Remember when you read it back during your Form_Load event, you write the corresponding line.Code:Open MyText.txt for output as #1
Print "Textbox1"
Print Text1.Text
Print ""
Print "Textbox2"
Print Text2.Text
Close #1
Code:Dim A$
Open MyText.txt for Input as #1
Input #1,A$ 'This will be the word Textbox1 so just skip to the next line
Input #1,A$ 'This will be the text that was originally in Textbox1
Text1.Text=A$ 'send this text to Textbox1
Input #1,A$ 'This is a blank line so just skip
Input #1,A$ 'This is the word Textbox2 so do nothing with it and skip.
Input #1,A$ 'This will be the text that was originally in Textbox2
Text2.Text=A$ 'send this text to Textbox2
Close #1
can you please tell me what the a$ means in the coding below because i can confused on what to enter
Code:Dim A$
Open MyText.txt for Input as #1
Input #1,A$ 'This will be the word Textbox1 so just skip to the next line
Input #1,A$ 'This will be the text that was originally in Textbox1
Text1.Text=A$ 'send this text to Textbox1
Input #1,A$ 'This is a blank line so just skip
Input #1,A$ 'This is the word Textbox2 so do nothing with it and skip.
Input #1,A$ 'This will be the text that was originally in Textbox2
Text2.Text=A$ 'send this text to Textbox2
Close #1