<%@ Language=VBScript %> VB63

VISUAL BASIC 6

When setting Properties from a Procedure in a Standard Module prefix each Object Name with the Form Name and a period to let VB know which form is referenced.  Call Sub Procedure by using a literal string (Call by Value) -  Example: AddListBox "Kimberly" or by using a Variable (Call by Reference) - Example: AddNameToListBox = NewName$.

 Example: AddName Procedure to use in 2 Text Boxes

    Sub

           AddName (Team$, ReturnString$)

           Prompt$ = "Enter a" &Team$& "Employee"

           NM$ = InputBox(Prompt$, "Input Box") WrapCharacter$ = Chr(13) + Chr(10)

           ReturnString$ = NM$ &WrapCharacter$

    End Sub

 cmdSales_Click (button) AddName "Sales", SalesPosition$ txtSales.Text=txtSales.Text &SalesPosition&.

 cmdMkt_Click (button) AddName "Marketing", MktPosition$ txtMkt.Text=txtMkt.Text &MktPosition$.

Note: Char$13 = carriage return; Char$10=linefeed

 

ByVal Keyword is used in an argument list when declaring a procedure and instructs VB to keep a copy of original argument  and to return it unchanged  when the Procedure ends even if the variable was modified in the procedure.

Example: Sub  CostPlusInterest (ByVal Cost, Total)

 

Passing a Variable by Value:  This is an alternative to ByVal Keyword to prevent a passed Variable from being modified ; convert it to a literal value by enclosing it in ().

Example: CostPlusInterest (Price), Total.

 

Objects: Objects on a Form are stored in the same file. The entire set of Objects on a Form is called the Controls Collection, which is created automatically when a new Form is opened and expands when Objects are added to the Form.  Each collection in a program has its own name that can be referenced in the program code.  Include Form Name when using Controls Collection.

Example:  Form1.Controls.

 

Referencing Objects in a Collection by using Object names in the assignment statement.

Example: Form1.Controls! Label1.Caption="Employees"  '!' links Label1.Object to Controls Collection  (OR)

Address an Object in a Collection by specifying index position of the Object in the group by using For Each... Next loop.

Example: For Each Control in Form1.Controls   Control.Left=Control.Left+200  Next Control.

 

Forms Collection:  Collection of all loaded Forms in a program by a For Each ... Next loop; can set the characteristics of one or more Forms.

 

Printers Collection:  Collection of all available printers. Display printer names and let user choose the printer to use for output.

 

Database Collection:  Collection related to data access and dbms.

 

VB for Application Collections:  VB macros for MS Office application like MSWord, Excel etc.  MSWord all Open documents are stored in document collection, each paragraph in a currect document is stored in a paragraph collection.

Example: Seach for a doc in document collection and if found makes it the active doc else load the document

Dim aDoc, docFound, docLocation

docLocation="C:\sample\myletter.doc" For each aDoc in Documents if InStr(1, aDoc.Name, "myletter.doc", 1) then aDoc.Activate Exit For Else docFound=False EndIf Next aDoc. If docFound=False Then Documents.Open Filename=docLocation.

 

Array:  Arrays can be Created or declared in a program code like variables. If declared locally it can be used only in the Procedure in which it is declared.  Arrays that contain a set number of elements are called Fixed Size Arrays. 

Example: Public ArrayName (Dim 1 Elements,  Dim 2 Elements, ...) as DataType.

Arrays containing variable number of elements are Dynamic Arrays.

Example: Public Temperature() as Variant.

 

To declare Arrays locally in an Event Procedure replace the public keyword with the Static keyword and place declaration inside Event Procedure.

 

To refer to an element of an Array you use the Array Name and an Array Index enclosed in (). Index must be integer value.

 

Dynamic Arrays are dimensioned at runtime when user speficies the size of array or logically add statement in program to determine an array size based on specific conditions.

Example: Specify name of array Public Temperature() as Variant. Get input from user

Days=InputBox("How many days", "Create Array"); use Redim statement to set the dim for array. Redim Temperature(Days); use number as upperbound in For Next loop to process array element. For i%=1 to Days Temperature(i%)=InputBox(Prompt$, Title$) Next i%.

 

Multidimensional arrays can handle one or more tables of information.

 

Option Base Statement: If the index of first element in each array is to be 1 instead of 0 use the Option Base 1 statement in a standard module.

 

Always set a Form's AutoDraw Property to be True when using Print Method to display information on the Form. VB redraws screen if Form gets covered by another Window.

 

Text files opened using Open for input or output - Sequential files or Dynamic files. Line input reads a line of input from text file, EOF(), Close().

Example: Open.CommonDialog1.Filename for input as (#1 is an integer refers to no. of files opened) Private sub mnuItemOpen_Click()  Wrap$=Char$(13)+Char$(10)  CommonDialog1.Filter= "Textfiles(*.txt) *.txt";  CommonDialog1.ShowOpen if CommonDialog1.Filename<>" " Then Form1. mnuPointer=11 Open.CommonDialog1.Filename for Input #1 on Error Goto TooBig Do Until EOF(1) Line Input #1, LineofText$ AllText$=AllText$ &LineofText$ & Wrap$ Loop lblFile.Caption=CommonDialog1.Filename txtFile.Text=AllText$ txtFile.Enabled=True mnuItemClose.Enabled=True, mnuItemOpen.Enabled=False  TooBig: if ErrNo=71 lblFile.Caption = "File too large to be opened" End.

 

Home |  VB6-1 |  VB6-2 |  VB6-3 |  VB6-4 |  VB6-5 |  HTML-1 |  HTML-2 |

This site was last updated 06/21/06