There are many different ways to handle data (records) stored within files. The code on these pages gives you the opportunity to save, search, display and update data from textfiles (set up in Notepad).
I need to save a new username and password to a text file i.e. to create a new account. You need a text file saved into your DEBUG FOLDER which is in your BIN folder
The user will enter their details into the text boxes, click ‘Save’ and their new user and password details will be saved to your textfile.
Note: Whenever you want to read and/or write to textfiles, you need to put the following code above the Public Class statement:
Imports System.IO
Public Class Form1
Declare an Array = "MyFile" (This can be called anything you want)
StreamWriter = You are going to WRITE (save) data to your textfile - you need to tell the program where it is located (Dir$). The TRUE on the end tells the program to add your data onto the end of the textfile.
3. You are putting what is in the text boxes into the text file.
4. MyFile is your array (you may have called it something different)
5. LSet - takes the characters in the text box and pads them out to 10. (You decide how many spaces you need i.e. if it is an email address it would need to be longer)
6. The '&' character is used to string together contents of multiple textboxes (e.g. username & password), so they appear in one line in your textfile.
Check your text file in your bin/debug folder, to make sure your details have been successfully saved with the correct number of spaces in between each field.
Enter the pupil's username in the username textbox and press 'SEARCH'. The pupil's details which match your username will be retrieved from the textfile and displayed in the labels underneath.
Remember to put this line of code above Public Class Form1:
Imports System.IO
Declare an Array = "MyPupilFile" (This can be called anything you want)
File.ReadAllLines will read all the records in your "pupildetails.txt" textfile and store them in your array "MyPupilFile"
NOTE: arrays start as position 0, so your first record will be stored in array position 0, the second record in position1, etc.
3. Now we need to loop through all of the records in the array and find the pupil with the same username as the one entered into the textbox.
Note: UBound means the end of the array (i.e. the end of the textfile)
4. Inside our FOR loop, we need to check IF the first 10 characters of the record (the username) matches with the username input into the textbox
Let's look at this code in a bit more detail, to read/retrieve the username:
Trim(Mid(MyPupilFile(RecordNumber), 1, 10))
The Mid function allows you to choose specific characters in the string (record). In this case, our username is stored in the first 10 characters of our file.
The Mid function needs two numbers:
The first number (value 1 in this code) tells the computer to start at position 1 in the record (1st character of username)
The second number is how many characters you want to read/retrieve i.e. 10 characters in the code above
Here is a second example, where we want to retrieve the password:
Trim(Mid(MyPupilFile(RecordNumber), 11, 10))
The password is the next 10 characters in the record, so starting at position 11, read/retrieve 10 characters
NOTE: The Trim function then gets rids of any spaces before trying to match with the input username.
5. The lines of code inside the IF... End If pull out the other fields such as Password, FirstName, Surname and NumberOfSubjects and put them into the labels on the form.
Example textfile pupildetails.txt:
File Structure =
Username 10 chars
Password 10 chars
FirstName 10 chars
Surname 15 chars
NoOfSubjects 2 chars
This time, we would need to find a match in the file on username and password.
We can use an IF statement with multiple conditions (check username matches what is entered in the username textbox, AND check password matches what is entered in the password textbox). If they do match. let the user log on, if they do not match, then login is rejected.
In the code above, you can see we use a different text file called Login.txt, and this time, we just pull out the first 10 chars for the username:
Trim(Mid(Users(RecordNumber), 1, 10)) (Trim gets rid of any spaces)
and the next 10 chars for the password:
Trim(Mid(Users(RecordNumber), 11, 10))
Then we match each one on the characters entered in the text boxes:
If Trim(Mid(Users(RecordNumber), 1, 10)) = TxtUsername.Text And
Trim(Mid(Users(RecordNumber), 11, 10)) = TxtPassword.Text
Using an array, find the record position that you want to update (LineTo Update)
Update that record in the array.
Write out the whole array (WriteAllLines) to the textfile - this will overwrite the old records in that textfile.
Let's look at this code in a bit more detail:
Finds out how may records are in the array called Lines
Stores the number of records in the variable NumberOfLines, which we can then use to loop through the records in the array in the For/Next statement.
(It's just an alternative to using UBound(Lines), but sometimes we need to find out how many records are in the file for other reasons).
This time we are using File.WriteAllLines which will save ALL the records in the array to the names.txt textfile, overwriting whatever was there before.
Datagrids are a really neat way of displaying multiple records at the same time on a form. They look a bit like a table, with column headings (fields or data items) and rows (records from a textfile).
These videos show you all you need to know about creating a datagrid, displaying, adding and updating data from textfiles using a datagrid.
Creating a DataGrid
Alternatively, you could add a datagrid to your form and then use this code to create the columns in your datagrid:
Adding & Updating Data
Displaying / Retrieving Data
Checking the update works
Clearing a datagrid
Choosing a value in the datagrid, then displaying associated data in textboxes
There are two ways to create a datagrid, so there are also two ways to add columns to the grid:
As described earlier, the datagrid can be created in the code. To add an extra column, add 1 to the ColumnCount (change it to 6 in this example), then add a new line of code:
DataGridView1.Columns(5).Name = "Totals"
You can also create a datagrid by adding one to your form from the toolbox, then adding the columns, one at a time.
Name = what VB will refer to it as e.g. Totals
Header Text = what you want to see as your column heading e.g. Total (£)
When you want to create additional columns in your datagrid, just add your new column in the same way as you set up your datagrid. Click on the little triangle in the top right of the datagrid and choose Add Column..
Datagrid with additional field/column--->
Set up a variable (called TotalsTemp in this example). This will be used to accumulate the costs of the other items.
Here is the whole procedure, which will add up all your costs and output the total in the Totals column.
Note:
Note:
Note - this line of code will allow display of the datagrid only i.e. the data cannot be clicked on, or altered by the user.
In summary, these powerpoints give you a range of ways to process textfiles in Visual Basic. They may help you to understand it further. You could do the exercises in the powerpoints as practise for file handling: