Visual Basic Programming

Page 16

On this page, you will learn about a different way to input data and store it, along with the list box object. You will learn this using sequential access files. A sequential access file is a data file (.dat) that stores records which are entered by the user. For instance, a user could enter a name into a program, and the program may take that name and write it to a data file. The user may also retrieve certain part of the file as well. Sequential access files are named as so because each record is written to the data file one after another. Now we can begin.

You will first learn about the list box object.

I know that we had previously done the same task with code, but I just wanted to make you aware that you may also do this without coding.

The top object is a text box as I am sure you guessed, and the two objects beneath it are list boxes.

Before we continue, we need to understand some of the methods and properties for list boxes. The ones we will use are listed below.

Keyword

Type Purpose Syntax
AddItem Method Adds string (text) to list lstName.AddItem String
**
A string is just text**
RemoveItem Method Removes string from list lstName.RemoveItem String
List Property Used as index; stores entire list lstName.List(Value of items' index(es))
ListIndex Property Used to look at a particular list entry lstName.ListIndex = Value
Value
is a numeric value. Starts at 0. Setting ListIndex to 0 selects the first list entry, 1 the second etc...
Text Property Used to take the text from the selected entry frmName.Caption = lstName.Text
Clear Method Clears the list box lstName.Clear

The line you entered uses the method AddItem to put the text from txtAdd into the list box lstStuff. It simply adds the item that you designate (in this case the text in txtAdd), just as the code reads.

Now let's code the cmdRemove command button.

The line you entered uses the RemoveItem method to delete the selected item (lstNew.ListIndex) from the list box lstNew.

Line 1: This is a new function. This tests to see if the length (Len) of the string (String: The text in something) txtAdd.Text is greater than 0. One character is a length of 1.
Line 2: If the length is greater than zero, it will enable the cmdAdd command button.
Line 3: Otherwise (the length of txtAdd.Text is zero), set the Enabled property of cmdAdd to False (Disable it).
Line 4: End the If statement.

If you run the application, you can add the text from the text box into the first list box. You may also remove items from the list box below by pressing the Remove button. You may have noticed that you get an error if you try to remove an item from the list box without an item selected. We will fix this problem shortly.

Line 1: Sets the focus to the txtAdd object; makes txtAdd the active object.
Line 2: Tests the length of the item selected in the list box lstNew. When (If) the length of the text is greater than 0, the command button cmdRemove is enabled. Otherwise (Else), cmdRemove is disabled.

If you try to remove text in a list box when nothing is selected, Visual Basic creates an error.

Line 1: Sets the forms caption to the text of the selected item in the lstNew list box
Line 2: Tests to see if the length (Len) of the string (or text) in the lstNew list box is not zero (greater than zero). If the length is greater, cmdRemove is enabled. Otherwise, cmdRemove is disabled. A length of 1 is one character, 2 is two characters, and so forth

If we do not test the length of the text in the list box, the code makes the program add nothing, which generates an error.

Personally, I think we have a very nice program using list boxes; but how about coding some more stuff to test the list in each list box. We could display a message if the user tries to enter text into the list box that already exists. We do this by using the For Next loop. Let's start the process.

To understand the line above, see below:

Line 1: Begins For Next loop, starting intCheck at zero and stops at the value of the amount of entries (list count) in the list box lstStuff.
Line 2: Tests to see if somewhere in the list (the List property includes the entire list) of lstStuff the same text as the text in the text box txtAdd exists. The variable intCheck in the parenthesis designates the entry of the list, which changes as the loop loops, or increments.
Line 3: If somewhere in the list the text is the same as the text in txtAdd, display a message box (MsgBox) reading Item already exists.
Line 4: Exits the sub-procedure. The code will otherwise add the text in txtAdd regardless because of the lstStuff.AddItem txtAdd.Text line beneath the loop.
Line 5 ends the If statement, and Line 6 increments the For Next loop; goes to the next value of intCheck.

Good for you if you did it without looking! We are done with list boxes. Now that we have learned about the list box, we may continue, and learn about Sequential Access Files. Save the form and project only if YOU want to. Let us continue.

Congratulations if you survived the tedious process setting up the interface. Now we will begin the coding!

This line simply selects the first item in the list box lstFruits.

We are going to use seven variables for this application, so let's declare them now.

You may wonder what the parenthesis are next to the variable strEntries are. This sets up dimensions for the variable, or different parts to store data in. This is called an array. Arrays are usually used to store a large amount of related data. Because we are going to store a numerous amount of related text in a variable, it is better sometimes to just use one variable with more than one dimension.

The line above stores the value of the text entered into the input box. The LCase used is a function used to change the text of the selected item in the list box lstFruits to lower case letters. It looks much better in the input box this way.

This line of code tests to see if there is anything in the variable lngEntries. The Str prefix in front of the variable treats it as a string. The Long data type stores numeric values and not text. The variable is however accepting text, so it behave like a string. If it is not treated as a string, we cannot test to see if the text in the InputBox is blank ("") because the input box accepts strings. If the user enters nothing, the variable lngEntries stores 0.

The line above opens the file fruits.dat in order to append (add to) to the file as file #1

Line 1: Begin For Next loop; starts lngWrite at 1 and goes up to whatever the user enters into the InputBox, which was stored in lngEntries.
Line 2: Writes the text of the list box lstFruits (the selected item in the list box) then the index of the list item in lstFruits to file number 1, which is what the file was opened as above earlier
Line 3
: Goes to the next value of lngWrite. Remember that lngWrite only goes up to the value of lngEntries, which is what YOU enter.

Line 1: Sets the variable bytFruit to the selected item in the list box lstFruits.
Line 2: Sets the caption of the label with the index corresponding with the selected list item to lngEntries (what the user enters in the Input Box), then adds the current value of the label to the variable's (lngEntries) value.
Line 3: Closes the file. If you want to open the same file for a different access method, such as Output, it must be closed first.

Now let's get this thing to add up the quantities.

Line 1: Begins For Next loop; sets the range of values of intFruits of 0 to 11, the index range of the label lblFruit.
Line 2: Takes the value of the caption in the label lblFruit with index of wherever the loop is (0 through 11) and adds the value to lngTotal, then sets the total value equal to itself (lngTotal).
Line 3: Increments the loop; goes to the next value of intFruits.
Line 4: Sets the caption of lblTotal to Total fruits: then lngTotal.

If you run the application, you may notice that when you try to add fruits, the first entry works fine, but then it keeps incrementing by the value of the label corresponding with the selected item in the list box. This is because the number in the label's caption is set to add to lngTotal. To solve this, all we need to do is reset lngTotal.

Now we will code the cmdExit button, but a little differently.

          bytResponse = MsgBox("Are you sure you want to exit?", vbYesNo + vbQuestion, "")
          If bytResponse = vbYes Then End

Line 1: Displays a message box including a yes and no button(vbYesNo), and displays it as a question (vbQuestion)
Line 2: Tests bytResponse; if the user clicks Yes, end the application.

Now for the clear button.

This does the same as above, but with a different message

Line 1: Tests to see whether the user pressed Yes
Line 2: When the users presses Yes, open the file C:\Windows\Desktop\fruits.dat to write data to it (Output), which writes over existing data.
Line 3: Write nothing to file number 1, thus clearing it.

Now that we have cleared the records in the file, let's clear the form.

Line 1: Looks at bytFruit 0 to 11
Line 2
: Clears the caption of lblFruit with index of 0 to 11 (all of them).
Line 3: Increments loop; goes to next value of bytFruit.
Line 4: Sets the focus to the command button cmdAdd (makes it active).
Line 5: Ends the If statement.
Line 6: Closes the file.
Line 7: Sets the caption of the label lblTotal to Total fruits.

I hope that you enjoyed this long tutorial. Now you may move on to Page 17.

Top of Page

Page 1 | Page 2 | Page 3 | Page 4 | Page 5 | Page 6 | Page 7 | Page 8 | Page 9 | Page 10 | Page 11

Page 12 | Page 13 | Page 14 | Page 15 | Page 16 | Page 17 | Page 18 | Page 19 | Page 20

Page 21 | Page 22 | Page 23 | Page 24 | Page 25 | Page 26 | Page 27 | Page 28 | Page 29 | Page 30