Results 1 to 12 of 12

Thread: I really need Help, to Access data from text file

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2001
    Posts
    9

    Question

    I am New to VB... I am writing a reletivly simple program It uses a random number gen to give me a numer between 1 and 100.. I then want that number to go out to an External File and pull the appriate line that goes with it.. Each number will have 3 details... and ITEM name.. its Cost and a Description. If anyone can help me out here I would be greatly apriciative.... I know this is proably not that hard but I only started this morning.. and I have just about everything in it working but this... so PLEASE>>>>>

    TIA
    Rogan

  2. #2
    Hyperactive Member badgers's Avatar
    Join Date
    Sep 1999
    Location
    Madison, WI USA
    Posts
    444
    how is the text file formatted?
    if number 50 is generated do you want line #50?
    what delimiters are used in the text file to tell between the fields in the record?
    I am so skeptical, I can hardly believe it!
    PS I am not a 'hyperactive member' I am a cool, calm, and collected member

  3. #3

    Thread Starter
    New Member
    Join Date
    Jan 2001
    Posts
    9

    Well I am not to sure... but see if this helps

    Some times the 1 - 10 could give you one item lets just say
    Toothpaste Then 11-15 would give you toothbrush and so on..

    But then In the File I would have somthing like

    Item: Cost: Description:
    Tothpaste 2.00 Used to help clean teeth
    toothbrush 2.00 Used to Clean Teeth

    Now in the Program.. I made it contert the any numer 1-10 to just one with an if then statment.. and then 11-15 = 2 and so on.. if that makes sense.. My problem is I can write
    out the Data in Either word or Execel?? i thin right? xcel would be easier but I am not sure how to import it into the program. That is Where I really get lost...
    TIA
    Rogan

  4. #4
    Hyperactive Member badgers's Avatar
    Join Date
    Sep 1999
    Location
    Madison, WI USA
    Posts
    444
    if your text file looks like this
    Item: Cost: Description:
    Tothpaste 2.00 Used to help clean teeth
    toothbrush 2.00 Used to Clean Teeth
    you may be in trouble. It may be better to use a delimiter that is unique to help figure out the fields.
    If you "assume" that the item will "always" be one word you may be able to make this work.
    textbox text1 contains the sample text file. I use it because it was fast for me. you will use code to read you file in place of what I have.
    Code:
    Private Sub Form_Load()
    Dim Stemp As String, Sitem As String, Scost As String, Sdescription As String, Srecord As String
    Dim AllRecords As Variant, i As Long, j As Long
    Stemp = Text1.Text 'assign your text file to the string Stemp.
    i = 2 'figure out which number you want and assign it to variable i
          'the array will be offset by one, so the first record will be number 2.
    AllRecords = Split(Stemp, vbCrLf)
    Srecord = AllRecords(i) ' i is the record number you want 1,2 ect..
    i = InStr(Srecord, " ") ' looking for the first space
    Sitem = Left$(Srecord, i)
    j = InStr(i + 1, Srecord, " ")
    Scost = Mid(Srecord, i, j - i)
    Sdescription = Right(Srecord, Len(Srecord) - j)
    End Sub
    i used text1.text as a holder for your example text file. you will do this differently as well as assign a value for the variable i. I would strongly suggest using a delimiter like a ";:;" or something unique. this will give you more flexability in what you can have in a field.
    To work this to its end, using a database would be the most future proof. My understanding that this is just for your education so have fun with the text files.

    I am so skeptical, I can hardly believe it!
    PS I am not a 'hyperactive member' I am a cool, calm, and collected member

  5. #5

    Thread Starter
    New Member
    Join Date
    Jan 2001
    Posts
    9

    So if I wer to???

    Item: Cost: Description:
    Toothpaste *=* 2.00 *=* Used to help clean teeth
    Toothbrush *=* 2.00 *=* Used to Clean Teeth

    If i were to do somthing like that it would work better?
    I know that useing like an acces file would be easier.. But I want the User of the program to be able to change the items in the Generator because this is to be used in a gaming envirment and the GM may want to change items to ones he likes and so on... But the code you gave me looks like it will help me a great deal... Do I have to figure out how do use the diliniator things.. LOL... Like I said I am 2 days into VB... And I mainly used to program in C++ But never got into using DATA files!
    TIA
    Rogan

  6. #6
    Hyperactive Member badgers's Avatar
    Join Date
    Sep 1999
    Location
    Madison, WI USA
    Posts
    444
    that will work
    Code:
    Private Sub Form_Load()
    Dim Stemp As String, Sitem As String, Scost As String, Sdescription As String, Srecord As String
    Dim AllRecords As Variant, OneRecord As Variant
    Stemp = Text1.Text 'assign your text file to the string Stemp.
    i = 2 'figure out which number you want and assign it to variable i
          'the array will be offset by one, so the first record will be number 2.
    AllRecords = Split(Stemp, vbCrLf)
    Srecord = AllRecords(i) ' i is the record number you want 1,2 ect..
    OneRecord = Split(Srecord, "*=*")
    Sitem = OneRecord(0)
    Scost = OneRecord(1)
    Sdescription = OneRecord(2)
    MsgBox "item                    cost          description" & vbCrLf & Sitem & "    " & Scost & "    " & Sdescription
    End Sub
    I am so skeptical, I can hardly believe it!
    PS I am not a 'hyperactive member' I am a cool, calm, and collected member

  7. #7

    Thread Starter
    New Member
    Join Date
    Jan 2001
    Posts
    9

    Now.. THANK YOU

    One last question.... Can I then Input each of those into a text box? So that way.. in the funcion I have 3 text box's become visible and then one would be the Item Description and the Cost? and have it post the Text in those boxes?
    I know you can,, I just have to learn... Because I want to send the CONTEXT in the Variable to the caption, not the variable.. Cause If i remmber if you put somthin like

    text1.caption = "The contents of variable *item*"


    Would somthing like that work...
    tia
    rogan?

  8. #8
    Hyperactive Member badgers's Avatar
    Join Date
    Sep 1999
    Location
    Madison, WI USA
    Posts
    444
    if you have three text boxes text1, text2, text3.
    text1.text=sitem
    text2.text=scost
    text3.text=sdescription
    will fill the text boxes
    you could eliminate the string variables sitem, scost, and sdescription and use the text boxes to hold the data.
    text boxes do not have a caption property.
    you will need to add lables to the form to clue the user into what each text box represents.
    While you are at it I would change the names of the text boxes from the default text1, text2, text3 to txtItem, txtCost, txtDescription.
    Code:
    Private Sub Form_Load()
    Dim Stemp As String, Srecord As String
    Dim AllRecords As Variant, OneRecord As Variant
    Stemp = Text1.Text 'assign your text file to the string Stemp.
    i = 2 'figure out which number you want and assign it to variable i
          'the array will be offset by one, so the first record will be number 2.
    AllRecords = Split(Stemp, vbCrLf)
    Srecord = AllRecords(i) ' i is the record number you want 1,2 ect..
    OneRecord = Split(Srecord, "*=*")
    txtItem.Text = OneRecord(0)
    txtCost.Text = OneRecord(1)
    txtDescription.Text = OneRecord(2)
    'use labels above the text boxes to tell the user what each text box is
    End Sub
    I am so skeptical, I can hardly believe it!
    PS I am not a 'hyperactive member' I am a cool, calm, and collected member

  9. #9
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238
    i knew ADO can open a flat database (Text file) but I still looking for the solution. Perhaps someone can help? With the ADO, you can add, edit, delete any line at anytime you wish.


  10. #10

    Thread Starter
    New Member
    Join Date
    Jan 2001
    Posts
    9

    Thank you

    Thank you Badger for all your help!!



    Chris ADO? I do not know what this is.. Feel free to email me if you come up with any ideas

    [email protected]

    Thanks
    Rogan.....

  11. #11

    Thread Starter
    New Member
    Join Date
    Jan 2001
    Posts
    9

    One last thing...

    I have it in and when I try to run it I get this error


    It highlights the name of the text file...
    the line stemp = minpot.txt and says Method or data member not found... Now I am guessing it is just not finding the file? I am probably missing somthing Simple!
    HEHEHE.....
    Rogan

  12. #12
    Hyperactive Member badgers's Avatar
    Join Date
    Sep 1999
    Location
    Madison, WI USA
    Posts
    444
    Code:
    Private Sub Form_Load()
    Dim Stemp As String, Srecord As String, iFile As Integer
    Dim AllRecords As Variant, OneRecord As Variant
    iFile = FreeFile
    Open "c:\temp.txt" For Input As #iFile 'assign your text file to the string Stemp.
    Stemp = Input(LOF(iFile), #iFile)
    Close #iFile
    i = 2 'figure out which number you want and assign it to variable i
          'the array will be offset by one, so the first record will be number 2.
    AllRecords = Split(Stemp, vbCrLf)
    Srecord = AllRecords(i) ' i is the record number you want 1,2 ect..
    OneRecord = Split(Srecord, "*=*")
    txtItem.Text = OneRecord(0)
    txtCost.Text = OneRecord(1)
    txtDescription.Text = OneRecord(2)
    'use labels above the text boxes to tell the user what each text box is
    End Sub
    I am so skeptical, I can hardly believe it!
    PS I am not a 'hyperactive member' I am a cool, calm, and collected member

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