Results 1 to 9 of 9

Thread: text file question

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2005
    Posts
    1,364

    text file question

    Im using something like this to write to the text file:

    VB Code:
    1. Open "C:\test.txt" For Append As #1
    2.         Print #1, Text1.Text & "=" & Text2.Text
    3.         Close #1

    which writes into the text file like this:

    John=3565 (where John is the text1 name and 3565 is the text2 ID)

    But now, if I make 2 more text boxes..

    Text3.Text and Text4.Text, and i enter John in Text3.Text and 3565 in Text4.Text, and then i clikc Command1 button, how can i make it msgbox that the username=id is a correct combination?
    otherwise i need it to msgbox that its a wrong username/ID etc..

    hope you guys know what i mean, thanks in advance!!

  2. #2
    Fanatic Member doofusboy's Avatar
    Join Date
    Apr 2003
    Posts
    526

    Re: text file question

    You would need to open your C:\test.txt file for Output, read in a line with something like this:

    Dim read_data As String
    Line Input#1, read_data

    Then, split what's read on the '=' to get what's on both sides of the equal sign with:

    Dim a As String
    Dim b As String
    a = Split(read_data, "=") (0)
    b = Split(read_data, "=") (1)

    Then compare these values to what is in Text3 and Text4:

    If Text3.Text = a And Text4.Text = b Then
    ' enter code here for what to do if they match
    Else
    ' enter code here for what to do if they don't match
    End If
    Do canibals not eat clowns because they taste funny?

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2005
    Posts
    1,364

    Re: text file question

    ok thanks i got this:

    VB Code:
    1. Private Sub Command8_Click()
    2.     Dim read_data As String
    3.     Open "C:\test.txt" For Append As #1
    4.     Line Input #1, read_data
    5.     Dim a As String, b As String
    6.     a = Split(read_data, "=")(0)
    7.     b = Split(read_data, "=")(1)
    8.     Text3.Text = a
    9.     Text4.Text = b
    10.     Close #1
    11. End Sub

    but i get bad file code on this line:

    Line Input #1, read_data

    (at any time the text file will only have 1 line, so i just want it to read the first line in the file)

    test.txt contains this:

    John=3575

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

    Re: text file question

    Quote Originally Posted by doofusboy
    You would need to open your C:\test.txt file for Output, read in a line with something like this:

    Dim read_data As String
    Line Input#1, read_data

    Then, split what's read on the '=' to get what's on both sides of the equal sign with:

    Dim a As String
    Dim b As String
    a = Split(read_data, "=") (0)
    b = Split(read_data, "=") (1)

    Then compare these values to what is in Text3 and Text4:

    If Text3.Text = a And Text4.Text = b Then
    ' enter code here for what to do if they match
    Else
    ' enter code here for what to do if they don't match
    End If
    One minor change I would make
    VB Code:
    1. Dim a As String
    2. a = Split(read_data, "=")
    3.  
    4. If Text3.Text = a(0) And Text4.Text = a(1) Then
    5.      ' enter code here for what to do if they match
    6. Else
    7.      ' enter code here for what to do if they don't match
    8. End If
    You really don't need the extra variable.

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2005
    Posts
    1,364

    Re: text file question

    well i get compile erorr, expected array..

    VB Code:
    1. Private Sub Command8_Click()
    2.     Dim read_data As String
    3.     Open "C:\test.txt" For Append As #1
    4.     Line Input #1, read_data
    5.     Dim a As String
    6.     a = Split(read_data, "=")
    7.     Text3.Text = a(0)
    8.     Text4.Text = a(1)
    9.     Close #1
    10. End Sub

    im just trying to get 'John' into text3.text and the ID into text4.text

    anyone see where im going wrong

  6. #6
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: text file question

    Dim a as string

    should be

    Dim a() as string
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2005
    Posts
    1,364

    Re: text file question

    ok thanks i changed it but now i get: bad file mode on this line;

    Line Input #1, read_data

  8. #8
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: text file question

    change Append to Input

    Open "C:\test.txt" For Append As #1

    is for writing to the file not reading from

    Open "C:\test.txt" For Input As #1
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  9. #9

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2005
    Posts
    1,364

    Re: text file question

    ooooops forgot about that, yeah works thanks alot guys!

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