Welcome to the visual basic index! The Visual Basic Index. Main Page ~ Sign Guestbook ~ View Guestbook

Source code

Summery of tutorials
Introduction to VB
Lesson one
Lesson Quiz
Lesson two
Lesson three
Lesson four

Exam Project 3
Exam Project 4

VB Books + Selected
VB mailing list
Add to bookmarks


Cheap UK books
Fix word documents Domestic energy assessor
Visual Basic Tutorial (Lesson 3) Visual Basic Tutorial (Lesson 3)
Click On one  
Lesson 3
Hopefully you will learn this during lesson 3. :

  • Displaying Message Boxes
  • Opening Files
  • Retreving Information from files
  • Saving Information to files
  • Printing text to the printer
  • Control Arrays


    Msgboxes

    Message boxes are used when you want to ask the user a question or display an error message(s) and advise the user. There are six types of message boxes here are their functions and what they do. Here is the listing of all the possible msgbox events

    The Buttons displayed in a message here
    Button LayoutValueShort Description
    vbOKonly0Displays the OK button.
    vbOKCancel1Displays the ok and cancel button.
    vbAbortRetryIgnore2Displays the Abort , Retry , Ignore
    vbYesNoCancel3Displays Yes , No and Cancel button
    vbYesNo4Displays the Yes / No button
    vbRetryCancel5Displays the retry and Cancel buttons.

    The Icons dispayed in the message box are here
    Icon on messageValueShort Description
    vbCritical16Displays critical message icon
    vbQuestion32Displays question icon
    vbExclamation48Displays exclamation icon
    vbInformation64Displays information icon

    The Default button displayed in a message form
    Default ButtonValueShort Description
    vbDefaultButton10Button 1 is default
    vbDefaultButton2256Button 2 is default
    vbDefaultButton3512Button 3 is default

    Msgbox Return Value
    Return ValueValueShort Description
    vbOk1The User Clicked OK
    vbCancel2The User Clicked Cancel
    vbAbort3The User Clicked Abort
    vbRetry4The User Clicked Retry
    vbIgnore5The User Clicked Ignore
    VbYes6The User Clicked Yes
    VbNo7The User Clicked No

    The syntax for use of the message box in Mgsgbox "TEXT", VALUE, "TITLE"
    If you want to use two or more tables just add the values together. Therefore to print a message box to say "The Device was not Found!" OK & Explanation :

    Source code 1

    Private Sub Form_Load()

    MsgBox "The Device was not Found!", 48, "Header"

    End Sub

    Source code 2

    Private Sub Form_Load()

    MsgBox "The Device was not found!", vbExclamation, "Header"

    End Sub

    You should get the picture shown below whatever source code you used.

    This is a basic msgbox which in this case has not been processed in any way. The following Source code displays a msgbox that ask you for specific text. For example lets make a password program out of this message box.

    Private Sub Form_Load()

    lngBefore = Timer
    Do
    strAns = InputBox("What is the password Password is Example", "Password Required")
    Loop Until Val(strAns) = Example
    lngAfter = Timer
    msgbox "Correct Password", vbInformation

    End Sub

    Once you copy and paste the source code you should get prompted for a password in a different type of msgbox as it includes text. From looking at this example you should be able to see how the loop function works to generate and then check the value. All of the Return Values work in the same way to return an object input.

    Opening & Retriving information from files

    When applications are loaded they normal get some setting out of the registry or file this section will show you how to retrieve 1 string out of a file.

    Private Sub Form_Load()

    Dim F As Integer, password As String
    F = FreeFile
    Open App.Path & "\password.txt" For Input As F
    Input #F, password
    Close #F

    End Sub

    As you can see from this source code the password is previously declared as a string. After this is done be sure to close the file otherwise next time you want to store or read the file the computer will think it is being used by another application and windows will not let you do anything with it. So as you can see it is Very Important to close the file

    Storing Information to a file

    FTP programs often store information to a file such as a username and password or host information in the same way. This following example will put some information into the file.

    Private Sub Form_Load()

    Dim F As Integer, pass As String
    F = FreeFile
    save = txtNew
    Open App.Path & "\password.txt" For Output As F
    Write #F, Text1.text
    Close #F

    End Sub

    Although this is a bit obvious I think I should include it just incase I think differently to other people.

    Printing text to the printer.

    This is rather easy to do and it gets used in notepad etc...

    Private Sub Form_Load()

    Printer.Print " The printer will print this text "
    Printer.Print ""
    Printer.Print " It will leave a line here"
    Printer.Print " It should add the contence of text1.text here : " & Text1.Text & " As you can see it works"
    Printer.Print ""
    Printer.EndDoc 'This will tell the printer it has finished.

    End Sub

    Everything that apears in position (A) will get printed by the default printer printer.print " A ". The printer enddoc is used to tell the printer the job is finished if this is not added the printer can not estimate how near it will be until it has finish and when it has finished it will think it has'nt so be sure to include this to prevent confusion.

    Control Arrays

    A control array is a list of controls with the same name. Therefore, instead of using five command buttons with separate five names, you can place a command button control array on the form, and that control array holds five command buttons. The control array can have a single name, and you will distinguish the control from each other with a subscript. One of the best reasons to use control array from that first control, all the elements in the control array take on the same property values, You then can change those properties that need to be changed without having to set every property for each control individually. Control arrays have a lot in common with data arrays. A control array has one array, and you distinguish all the array's controls from each other with the zero-based subscript. ( The index property holds the controls subscript number ). All of the control elements must be the same data type. As soon as you place a control on a form that has the same name as an existing control, Visual Basic makes sure you that you want to begin a control array by issuing the warning message to show that the control is already in use. This is used as a built in safety so that you do not over right an existing control by putting it some where else on the same form. If you answer the warning box with a no button, Visual Basic uses a default control name for the placed control.

    Picture Not available at the moment!

    All event procedures that use control from a control array require a special argument value passed to them that the determines which control is being worked on. For example if your application contains a single control command button named cmdtotal the click () event begins and ends as follows

    Private sub cmdtotal_click()

    End Sub

    If however you create a control array named the same name as before ( cmdtotal ) it will end up like this

    Private sub cmdtotal_click (index as integer)

    End sub

    The procedure uses the index argument as the control index number ( the subscript ) that the user clicked, Therefore if you want to change the clicked command buttons caption property inside the cmdtotal_click () the procedures you would need are as follows Cmdtoal(index).caption = "A caption name" The index value holds the command button's index the user click to generate the event procedures so you will always respond to the proper control clicked if you use Index after the control array name.


    Join the mailing list to recive updates and source code Click Here