Creating a Management Solution in Visual Basic #1 Design and Writing To Files

Submitted by: 
Language: 
Visitors have accessed this post 169 times.

Introduction:
This tutorial is going to be on how to create a record management solution in Visual Basic.

This Tutorial:
Since this is a multi-part tutorial series, this tutorial is going to be on setting up the form and creating the much needed writeToFile function.

The Design:
For your reference, I am using Visal Studio Ultimate 2013, the programming should be the same but the design may differ from previous or future versions of the studio program.

We are going to try and create the whole design in this first tutorial but we may have to add more later on for added features, or possibly even remove some to alter the way the entire solution works.

So first let add a textbox, call it addRecordBox and place it at the top of the form (top left is where I have placed mine), then create a button next to it, call it addRecordButton and set the text to 'Create Record...'. These two controls will allow us to create a new record with the entered text within the textbox. The button will open a new form where the user is able to add, modify and remove as many values as they like for the record to be created.

Next we are going to create a remove record option, we need the same controls as above. Create a textbox, name it removeRecordText. Create a button, place it next to the removeRecordText textbox and call it removeRecordButton.

We are also going to create an advanced option where the user is able to filter through the results contained within the database and can modify, add new records, or remove them straight from the filtering GUI.

So for the filtering option, we are just going to add a button to the main form named 'filterRecordsButton', place it at the bottom middle of the form and set the text on it to 'Filter Records...'.

Making The WriteToFile Function:
Now we are ready for the code for the main writeToFile function. This function will be the one which will open the file stream, write the new data to it and close the stream. We always ensure that the stream is closed to stop any read/write errors to/from our own program and other programs later on.

So first we create the main function with the name as 'writeToFile' and two parameters; the file name to open the stream to, and the text to write.

  1. Private Sub writeToFile(ByVal filePath As String, ByVal data As String)
  2. End Sub

Then we want to create an instance of a StreamWriter as this is what we will be using to wrie to the file. We named it 'file', appropriately...

  1. Private Sub writeToFile(ByVal filePath As String, ByVal data As String)
  2. Dim file As StreamWriter
  3. file = My.Computer.FileSystem.OpenTextFileWriter(filePath, True)
  4. file.WriteLine(data)
  5. file.Close()
  6. End Sub

We also set the file object to a new streamwriting object 'OpenTextFileWriter' which takes two parametrs; the path to the file to write to, and whether it should append to the file or not. We parse the filePath as the path to the file (since it is used in the call to the function to write to the file) and we set the appending boolean to true otherwise if we set it to false, it would overwrite the pre-existing data/content within the file and replace it with the new data which we write next.

Once the file is open as the 'file' object, we use the 'write' function to write the parsed data in the call to the function which is named 'data'. The data variable is already a string and is able to written to the file without any intervention.

Finally we close the file streamwriter to avoid errors later on within ours and other programs - this function also syncs any changes made to the file to stop corruption and loss of data.


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.