storing information to a txt database.


(If you cant view the image its @ " http://members.optusnet.com.au/~sescothern/inputbox2.jpg ")


How do i make it so that, once the information has been input and approved by the user on the left hand side, it is stored in a txt file of some sort and then JUST the company name is listed in the listbox on the right.

Then on the user double clicking a company on the top right listbox, the companies info is displayed in the listbox below.

The user can add as many companies as he/she wants. and view them accordingly.

If you want me to post what code i have so far, i can.



Answer this question

storing information to a txt database.

  • jackycn

    You could also use the My.Computer.Filesystem.WriteAllText method with the append option set to true top apppend contents to a file.

    However for what you are trying to achieve I would say a plain text file does not necessarily sound like the best approach - you could use an XML file which although a little bit more complicated to develop the code - will provide a better capability for searching and retrieving the data items, or even a simple database using Access or SQL Express.

    As scott said clearing items can be done using the clear and add methods on the listbox.items collection.

    A useful ebook to look at is

    http://msdn.microsoft.com/vbasic/learning/introtovb2005/

     This provides a nice walkthough on building a data centric application using a simple database. 


  • comspy

    As Spotty mentioned, this may be a better application for a database - but if you really need (or want) to use text files, then here is some psuedo code to get you started:

    'Code to Save new Company
    '------------------------
    'Use a StringBuilder to build the text to save
    Dim sb As New System.Text.StringBuilder
    'Use String.Format to include headers for each line
    sb.AppendLine(String.Format("Company: {0}", CompanyTextBox.Text))
    sb.AppendLine(
    String.Format("Address: {0}", AddressTextBox.Text))
    sb.AppendLine(
    String.Format("Phone No.: {0}", PhoneNumberTextBox.Text))
    sb.AppendLine(
    String.Format("ABN: {0}", ABNTextBox.Text))

    'Create a StreamWriter to save the text
    'to disk
    Dim writer As System.IO.StreamWriter
    'Here we create the actual writer; several things
    'happen here... "path" should be a variable containing
    'the location to save the file e.g. c:\myfiles
    'We then use String.Format to create a filename
    'using CompanyName.txt - so each company will have its
    'own text file in the folder that "path" points to
    writer = System.IO.File.CreateText( _
    System.IO.Path.Combine( _
    path,
    String.Format("{0}.txt", CompanyTextBox.Text)))

    'now write the actual data
    writer.Write(sb.ToString)
    'close the file
    writer.Close()

    'Code to List all companies
    '--------------------------
    'Clear the current list
    ClientBox.Items.Clear()

    'Get the list of company text files stored in
    'the "path" location
    Dim files() As String
    files = System.IO.Directory.GetFiles(path, "*.txt")

    '(Note that you could make up your own extension
    'when the file is created; you don't have to use txt)
    'Add each file to the list

    For Each file As String In files

    ClientBox.Items.Add(System.IO.Path.GetFileName(file))

    Next

    'Code to display a single company - this
    'would go in the ClientList.DoubleClick event
    'handler in your case
    '--------------------------------------------
    'Make sure a file is selected

    If ClientBox.SelectedIndex > -1 Then

    'Get a StreamReader to read the text file
    Dim reader As System.IO.StreamReader

    reader = System.IO.File.OpenText(System.IO.Path.Combine(path, String.Format("{0}.txt", ClientBox.SelectedItem)))

    'Read the text file into the other box
    ClientInfoBox.Text = reader.ReadToEnd

    'Close the stream
    reader.Close()

    End If

    This code makes some assumptions about the objects (ClientBox is a ListBox, ClientInfoBox is a TextBox, ignored SendBox as I wasn't sure what it was for). Although this code is generic, it should give you some good starting points.

    HTH, GL!



  • Hadrienlc

    You can use the StreamWriter class to output data to a text file. Something like this should work:

    Sub AppendDataToTextFile(ByVal data As String, ByVal path As String)
    Using writer As New StreamWriter(path, True)
    writer.WriteLine(data)
    End Using
    End Sub


    You can clear a list box by doing lb.Items.Clear (where lb is the name of the list box), and then insert items into it by using lb.Items.Add.

    -Scott Wisniewski


  • storing information to a txt database.