AllAPI Network - The KPD-Team

 
Allapi Network
 API-Guide
 ApiViewer

 API List

 
API Resources
 Tips & Tricks
 VB Tutorials
 Error Lookup
 
Misc Stuff
 VB examples
 VB Tools
 VB Links
 Top Downloads
 
This Site
 Search Engine
 Contact Form
 

Donate to AllAPI.net

Programming in Visual Basic

More Decisions :

The Select Case Structure

Quite often, as in the last unit, we need to test the same item a number of times for different values. When this is the case, we can replace all of the If ... Then ... Else statements that check that particular item with a Select Case structure. The general format is

Select case ITEMNAME
   Case First Possibility
      Statements
   Case Second Possibility
      Statements
   ...
   Case Final Posibility
      Statements
   Case Else
      Statements
End Select

Each possible value of the item is checked using a Case statement and is followed by one or more Basic statements that will be carried out if there is a match for that value.

The final part of the structure is usually a Case Else statement which will come into operation if none of the other values match. This is useful for informing the user that he/she has made an error.

The Message Box

One Quick and easy way of displaying messages for the user is the message box. This brings up a standard Windows Dialogue Box, with appropriate buttons for the user to choose from. A message box is called from Visual Basic code using the MsgBox function using the following format:

MsgBox (message, type, title)

where message = text to be sdisplayed, type = buttons to be displayed, title = title that appears at the top of the box. The type and title sections are not compulsory - if thay are omitted, you will get a Dialogue Box with just an OK button and the title of the Form as the title of the Dialogue Box.

Task Six :

1. Open the project STOCK.VBP. Double click on the Find Item command button to bring back the previous code produces in Task Five.

2. Delete or edit the existing code to replace the If ... Then ... End If structures with the following Select Case structure:

Select Case txtItem
   Case "Radio"
      txtPrice = 12.54
   Case "Headphones"
      txtPrice = 23.5
   Case "Glasses"
      txtPrice = 13.99
   Case "Teddy Bear"
      txtPrice = 23.49
   Case Else
      Msgbox ("Your item has not been found")
End Select

3. Start the program and try various correct and incorrect values for the item to check that your code is correct.

Exercise Six :

1. Modify the code for the Find Stock Number command button within the Stock project to use a Select Case structure instead of If ... Then ... Else statements.

2. Open the ADDRESS.VBP project and use the Select Case structure for the Find Name command button.

3. Add a new button to the Address project called cmdFindTelephone, with a Caption of Find Telephone Number. Write code code for this command button using the Select Case structure that will check a telephone number that is entered and automatically fill in the name & address details.

Using Else with an If statement

We saw in the previous tasks, the use of Else with the Select Case structure. The Else statement within an If ... Then ... End If structure performs a similar operation. This time, instead of checking a number of possibilities, we only check one condition and the Else statement comes in operation immediately if this condition is not met. The general format is:

If condtion statement(s) Then
   statement(s)
Else
   statement(s)
End If

For example, in the Stock project, we might want to check that a quantity is entered before carrying out calculations:

If Val(txtQuantity) = 0 Then
   MsgBox ("You have not entered a quantity")
Else
   txtSubTotal = Val(txtPrice) * Val(txtQuantity)
   ... other calculations ...
End If

N.B. val(txtQuantity) will also give a value of 0 if text is accidentally entered into the quantity box. The above code will therefore check for a zero quantity or text incorrectly entered.

Exercise Seven:

1. Open the Stock project and amend the code for the Calculate command button to check that a valid quantity has been entered as described above.

2. Amend the code for the Find Stock Number command button to ensure that text is not entered for the Stock Number. Remember that val(StockNumber) will give zero if text is entered. Also, in this case, the Select Case structure should be completely contained within the If ... Else ... End if structure:

If .......... Then
   ........
Else
   Select Case
      ........
      ........
      ........
   End Select
End If

Indentation :

Hopefully, you will have noticed that indentation is widely used in Visual Basic coding. Indentation is used to show the various parts of a structure - the statements that make up the structure are lined up. For example:

If ..... Then
   statement(s)
Else
   statement(s)
End If

Notice how the three components of the structure - If, Else and End If - are lined up and the statements that are linked to each part of the structure are indented. Similary, with the Select Case:

Select Case ...
   Case ...
      statement(s)
   Case ...
      statement(s)
   Case ...
      statement(s)
   Case ...
      statement(s)
End Select

More indentation is needed for structures within structures:

Select Case ...
   Case ...
      If ... Then
         statement(s)
      Else
         statement(s)
      End If
   Case ...
      statement(s)
End Select

The use of indentation makes coding much easier to read and change, particularly when the code becomes more and more complex with the extended use of various types of structure. It is very difficult, without the use of indentation, for instance to match End Ifs, End Selects, etc. to the appropriate opening statements, and this type of mismatch is a very common programming fault.

Comments :

It's easy to remember what a particular piece of simple code does immediately after you have written it. But what happens with very complex code that you wrotesix months ago that you have to change for some reason? Quite often, programmers write code and they can't remember what its purpose was. Similarly, they know why and what they've written, but can't find it!!

The use of comments within programs helps the original programmer and, more importantly, other people who have to read and/or amend the code, understand what particular sections of code do.

Remarks are used troughout the code as explanatory notes for the programmer. Thay do not effect the running of the code in any way. Each line of comments needs to start with Rem statement or a singe apostrophe ( ' ). For example:

Rem The following code checks the item entered into the Stock form
Rem and enters the appropriate price into the form

Select Case txtItem
   Case "Radio"
      txtPrice = 12.54
   Case "Headphones"
      txtPrice = 23.5
   Case "Glasses"
      txtPrice = 13.99
   Case "Teddy Bear"
      txtPrice = 23.49
   'if the item is not found, display appropriate dialogue box
   'with OK button only
   Case Else
      MsgBox ("Your item has not been found")
End Select

Exercise Eight :

Re-visit all the code you have written so far. Check that indentation is used where appropriate and add comments throughout to describe your code.

Page [<< PREV] 1 2 3 4 5 6 [NEXT >>]
Back to Tutorials - Main

 

 


Copyright © 1998-2007, The Mentalis.org Team - Privacy statement
Did you find a bug on this page? Tell us!
This site is located at http://allapi.mentalis.org/