Results 1 to 16 of 16

Thread: text box help

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2008
    Posts
    182

    text box help

    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

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: text box help

    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)

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    May 2008
    Posts
    182

    Re: text box help

    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.

  4. #4
    Junior Member
    Join Date
    Jun 2008
    Posts
    17

    Re: text box help

    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
    Last edited by cmdnc0; Jun 1st, 2008 at 02:08 PM.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    May 2008
    Posts
    182

    Re: text box help

    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.

  6. #6
    Junior Member
    Join Date
    Jun 2008
    Posts
    17

    Re: text box help

    Not sure what it is you want. Can you explain a little clearer please.

  7. #7
    Hyperactive Member
    Join Date
    Oct 2007
    Location
    Indiana
    Posts
    295

    Re: text box help

    Quote Originally Posted by adam786
    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.
    If you just want to include data from a second text box, just do this:

    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.

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    May 2008
    Posts
    182

    Re: text box help

    i tried that and it only saved text box 2 and not 1. what might the problem be?

  9. #9
    Hyperactive Member
    Join Date
    Oct 2007
    Location
    Indiana
    Posts
    295

    Re: text box help

    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.

  10. #10
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: text box help

    How about
    Code:
    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
    Last edited by Hack; Jun 2nd, 2008 at 11:43 AM.

  11. #11
    Hyperactive Member
    Join Date
    Oct 2007
    Location
    Indiana
    Posts
    295

    Re: text box help

    Quote Originally Posted by Hack
    How about
    Code:
    Private Sub Form_Load()
    If Dir$(App.Path & "\mytext.txt") <> vbNullString Then 'it exists - do it
        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.
    Last edited by Hack; Jun 2nd, 2008 at 11:42 AM.

  12. #12
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: text box help

    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.

  13. #13
    Hyperactive Member
    Join Date
    Oct 2007
    Location
    Indiana
    Posts
    295

    Re: text box help

    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.

    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
    Last edited by Caskbill; Jun 2nd, 2008 at 12:12 PM.

  14. #14

    Thread Starter
    Addicted Member
    Join Date
    May 2008
    Posts
    182

    Re: text box help

    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?

  15. #15
    Hyperactive Member
    Join Date
    Oct 2007
    Location
    Indiana
    Posts
    295

    Re: text box help

    Quote Originally Posted by adam786
    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.

    Code:
    Open MyText.txt for output as #1
    Print "Textbox1"
    Print Text1.Text
    Print ""
    Print "Textbox2"
    Print Text2.Text
    Close #1
    Remember when you read it back during your Form_Load event, you write the corresponding line.

    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

  16. #16

    Thread Starter
    Addicted Member
    Join Date
    May 2008
    Posts
    182

    Re: text box help

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width