I.T. Discussion Community!
-Collapse +Expand
VB Classic
Search VB Classic Group:

Advanced
-Collapse +Expand VB Classic Store

Prestwood eMagazine

June Edition
Subscribe now! It's Free!
Enter your email:

   ► KBVB Classic Knowledge Base  Print This    Code Snippet DB All Groups  

VB Classic Code Snippets Page

These Code Snippets are contributed by you (our online community members). They are organized by our knowledge base topics. Specifically, by the VB Classic sub-topics.

Contribute a Code Snippet
Expand All

34 Visual Basic Classic Code Snippets

Group: Visual Basic Classic


Topic: VB Classic

-Collapse +Expand 1. VB Classic Array (x = Array())
 

Arrays in VB Classic use a 0-based indice. UBound returns -1 if the array has no elements, 0 if it has 1, 1 if it has 2, etc.

Dim MyArray As Variant
Dim i As Integer
 
MyArray = Array("Mike", "Lisa", "Felicia", "Nathan")
 
If UBound(MyArray) > -1 Then
  For i = 0 To UBound(MyArray)
    MsgBox (MyArray(i))
  Next
End If
Posted By Mike Prestwood, Post #102134, KB Topic: VB Classic
-Collapse +Expand 2. VB Classic Empty String Check (Len(s&vbNullString))
 

In VB Classic, you have to add an empty string to the value being compared in order to get consistent results. For example, add &"" to your string varilable or it's code equivalent &vbNullString. Then compare to an empty string or verify it's length to 0 with Len.

All these will work for variables unassigned, set to "", or set to Null:

If s&"" = "" Then
MsgBox ("Quotes with &'' say null is empty")
End If
 
If Len(s&"") = 0 Then
MsgBox ("Len with &'' says null is empty")
End If
 
If Len(s&vbNullString) = 0 Then
MsgBox ("Using vbNullString also works!")
End If
Posted By Mike Prestwood, Post #102040, KB Topic: VB Classic
-Collapse +Expand 3. VB Classic File Extensions
 
  • .BAS = VB source code file.
  • .CLS = VB class file (one class per file).
Posted By Mike Prestwood, Post #101485, KB Topic: VB Classic
-Collapse +Expand 4. VB Classic Parameters (ByRef, ByVal)
 

By Reference or Value
For parameters, you can optionally specify ByVal or ByRef. ByRef is the default if you don't specify.

Function SomeRoutine(ByRef pPerson, ByVal pName, Age)
Posted By Mike Prestwood, Post #101628, KB Topic: VB Classic



Topic: Tool Basics

-Collapse +Expand 5. VB Classic Assignment (=)
 

VB Classic uses = for it's assignment operator.

Dim Age As Integer
Dim FullName As String
   
FullName = "Randy Spitz"
Age = 38
Posted By Mike Prestwood, Post #101583, KB Topic: Tool Basics
-Collapse +Expand 6. VB Classic Case Sensitivity (No)
 

VB Classic is not case sensitive. If you type any other case for commands or variables, VB Classicwill change it to the "accepted" or "defined" case. For example, if you type msgbox it is converted to MsgBox.

The following code works:

MsgBox ("hello")
Posted By Mike Prestwood, Post #101342, KB Topic: Tool Basics
-Collapse +Expand 7. VB Classic Code Blocks (End Xxx)
 

VB Classiccode blocks are surrounded by statement ending keywords that all use End such as End Sub, End If, and WEnd.

Sub x
End Sub
 
If x Then
End If
  
While x
WEnd
Posted By Mike Prestwood, Post #101496, KB Topic: Tool Basics
-Collapse +Expand 8. VB Classic Comments (' or REM)
 

Commenting Code
VB Classic, like all the VB-based languages, uses a single quote (') or the original class-style basic "REM" (most developers just use a quote). VB Classic does NOT have a multiple line comment.

Directives - #

Directives are sometimes called compiler or preprocessor directives. A # is used for directives within VB Classic code. VB Classic offers only an #If..then/#ElseIf/#Else directive.

'Single line comment.

REM Old school single line comment.

#If MyDirective Then
'...some code.
#End If

 

Posted By Mike Prestwood, Post #101507, KB Topic: Tool Basics
-Collapse +Expand 9. VB Classic Comparison Operators (=, <>)
 

Save as VB Classic. Common comparison operators:

= equal
<> not equal
< less than
> greater than
<= less than or equal
>= greater than or equal
//Does VB evaluate the math correctly? No!
If 0.1 + 0.1 + 0.1 = 0.3 Then
MsgBox "correct"
Else
MsgBox "not correct"
End If
Posted By Mike Prestwood, Post #101587, KB Topic: Tool Basics
-Collapse +Expand 10. VB Classic Constants (Const kPI = 3.1459)
 

Scope can be Public, Global, or Private. The use of the newer Public keyword is preferred to the older Global. Private Const is the same as just specifying Const.

Const kPI = 3.1459
Const kName = "Mike"
 
//Public variable:
Public Const kFeetToMeter=3.28, kMeterToFeet=.3
Posted By Mike Prestwood, Post #101708, KB Topic: Tool Basics
-Collapse +Expand 11. VB Classic Deployment Overview
 

VB applications require the VB runtime DLL (for version 6, it's VB600.DLL) plus any additional dependencies you've added such as Crystal Reports, ActiveX controls, and DLLs.

You can use any of the many free and commercially available installation packages.

Posted By Mike Prestwood, Post #101909, KB Topic: Tool Basics
-Collapse +Expand 12. VB Classic Development Tools
 

Languages Focus: Development Tools

Primary development tool(s) used to develop and debug code.

VB Classic Development Tools

Microsoft Visual Basic 1...6. VB Classic is not compatible with VB.Net.

Posted By Mike Prestwood, Post #101551, KB Topic: Tool Basics
-Collapse +Expand 13. VB Classic End of Statement (Return)
 

Languages Focus: End of Statement

In coding languages, common End of statement specifiers include a semicolon and return (others exist too). Also of concern when studying a language is can you put two statements on a single code line and can you break a single statement into two or more code lines.

VB Classic End of Statement

A return marks the end of a statement and you cannot combine statements on a single line of code. You can break a single statement into two or more code lines by using a space and underscore " _".

MsgBox "Hello1"
MsgBox "Hello2"
MsgBox "Hello3"

'The following commented code
'on a single line does not work...
'MsgBox "Hello4" MsgBox "Hello5"

'Two or more lines works too with a space+underscore:
MsgBox _
"Hello6";
Posted By Mike Prestwood, Post #101691, KB Topic: Tool Basics
-Collapse +Expand 14. VB Classic If Statement (If..ElseIf..Else..End If)
 

The End If is optional if you put your code on a single line.

//Single line example.
If X = True Then MsgBox "hello" 
 
//Complete example.
If X = True Then
MsgBox "hello"
ElseIf Y = "ABC" Then
MsgBox "goodbye"
Else
MsgBox "what?"
End If
Posted By Mike Prestwood, Post #101387, KB Topic: Tool Basics
-Collapse +Expand 15. VB Classic Literals (quote)
 

Literals are quoted as in "Prestwood". If you need to embed a quote use two quotes in a row.

MsgBox ("Hello")
MsgBox ("Hello ""Mike"".")
  
'Does VB evaluate this simple
'floating point math correctly? No! 
If (.1 + .1 + .1) = 0.3 Then
MsgBox "Correct"
Else
MsgBox "Not correct"
End If
Posted By Mike Prestwood, Post #101530, KB Topic: Tool Basics
-Collapse +Expand 16. VB Classic Overview and History
 

Language Overview: Class based language. Although you can create classes, VB Classic is not fully OOP. It is a traditional language with a few OOP extensions. You code in a traditional approach using functions, procedures, and global data, and you can make use of simple classes to help organize your reusable code. It also supports one-level abstract class to implemented class using the Implements keyword.

Target Platforms: Microsoft Visual Basic 6 is most suitable for creating Windows desktop applications that use the VB600.DLL runtime DLL within Microsoft Windows.

Posted By Mike Prestwood, Post #101725, KB Topic: Tool Basics
-Collapse +Expand 17. VB Classic Report Tools Overview
 

Crystal Reports was very popular with VB Classic developers and came bundled with Visual Basic 3 through 6. VB6 offers both Crystal Reports and the new Microsoft Data Report Designer.

Posted By Mike Prestwood, Post #101650, KB Topic: Tool Basics
-Collapse +Expand 18. VB Classic String Concatenation (& or +)
 

Although you can use either a & or a + to concatenate values, my preference is to use a + because more languages use it. However, if you use & then some type conversions are done for you. If you use + you will sometimes have to cast a value to concatenate it. For example, you will have to use CStr to cast a number to a string if you use the + operator as a concatenation operator.

Dim FirstName As String
Dim LastName As String
 
FirstName = "Mike"
LastName = "Prestwood"
 
MsgBox "Full name: " & FirstName & " " + LastName
 
MsgBox "2+2=" + CStr(2+2)
Posted By Mike Prestwood, Post #101594, KB Topic: Tool Basics
-Collapse +Expand 19. VB Classic Unary Operators
 

An operation with only one operand (a single input) such as + and -.

Posted By Mike Prestwood, Post #101562, KB Topic: Tool Basics
-Collapse +Expand 20. VB Classic Variables (Dim x As Integer)
 

VB Classic is a loosely typed language. Declaring variables is optional unless you use the Option Explicit statement to force explicit declaration of all variables with Dim, Private, Public, or ReDim. Using Option Explicit is strongly recommended to avoid incorrectly typing an existing variable and to avoid any confusion about variable scope.

Undeclared variables are variants. To specifically declare a variant, use:

Dim x As Variant
Dim x 

Common data types include Byte (0..255), Boolean, Integer (2-byte integers), Long (4-byte integers), Currency, Single (32-bit number), Double (64-bit number), Date, String, and variant.

Variables declared with Dim at the module level are available to all procedures within the module. At the procedure level, variables are available only within the procedure.

Dim FullName As String
Dim Age As Integer
Dim Weight As Double
  
FullName = "Mike Prestwood"
Age = 32
Weight = 154.4
 
'Declaritive assignment not supported:
''Dim Married As String = "Y"   '>>>Not supported.
Posted By Mike Prestwood, Post #101569, KB Topic: Tool Basics



Topic: Language Basics

-Collapse +Expand 21. VB Classic Logical Operators (and, or, not)
 

VB Classic logical operators:

and and, as in this and that
or or, as in this or that
Not Not, as in Not This

'Given expressions a, b, c, and d:
If Not (a and b) and (c or d) Then
  'Do something.
End If
Posted By Mike Prestwood, Post #101892, KB Topic: Language Basics



Topic: Language Details

-Collapse +Expand 22. VB Classic Associative Array (Collection)
 

In addition to Add and Item, collections also offer Count and Remove. Notice that Add uses the format of Value, Key (which is backwards from many other languages).

Dim States As New Collection
   
States.Add "California", "CA"
States.Add "Nevada", "NV"
    
MsgBox (States.Item("CA"))
Posted By Mike Prestwood, Post #101580, KB Topic: Language Details
-Collapse +Expand 23. VB Classic Class..Object (Yes)
 

One class per file. File must use a .cls extension (instead of .bas). You can include your classes in your project (a traditional OOP approach) or compile them to a DLL or ActiveX EXE, register them, and use them that way (a uniquely VB approach).

Posted By Mike Prestwood, Post #101405, KB Topic: Language Details
-Collapse +Expand 24. VB Classic Custom Routines (Sub, Function)
 

VB Classic is a non-OOP language with some OOP features. It offers both Subs and Functions. A Sub does not return a value while a Function does. When Subs and Functions are used in a class module, they become the methods of the class.

Sub DoSomething(ByVal pMessage As String)
  MsgBox (pMessage)
End Sub
 
Function GetAge(ByRef pFullname As String) As Integer
  GetAge = 7
End Function
Posted By Mike Prestwood, Post #101601, KB Topic: Language Details
-Collapse +Expand 25. VB Classic Overloading (Not Supported)
 

Developer defined overloading in VB Classic:

  • Operator - No
  • Method - No
Posted By Mike Prestwood, Post #101467, KB Topic: Language Details
-Collapse +Expand 26. VB Classic Self Keyword (Me)
 

The Me keyword is a built-in variable that refers to the class where the code is executing. For example, you can pass Me from one module to another.

Private Sub Command4_Click()
  MsgBox Me.Name 'Displays name of form (Form1 in this case).
End Sub
Posted By Mike Prestwood, Post #101954, KB Topic: Language Details



Topic: Commands

-Collapse +Expand 27. VB Classic Left of String
 

VB Classic Left of String

Dim LeftString As String
LeftString = Left("Prestwood", 3)
MsgBox LeftString
Posted By Mike Prestwood, Post #101607, KB Topic: Commands



Topic: OOP

-Collapse +Expand 28. VB Classic Constructors (Class_Initialize)
 

When an object instance is created from a class, VB6 calls a special parameter-less sub named Class_Initialize. Since you cannot specify parameters for this sub, you also cannot overload it.

When a class is destroyed, VB6 calls a special sub called Class_Terminate.

Posted By Mike Prestwood, Post #101828, KB Topic: OOP
-Collapse +Expand 29. VB Classic Destructor
 

When an object instance is destroyed, VB6 calls a special parameter-less sub named Class_Terminate. For example, when the variable falls out of scope. Since you cannot specify parameters for this sub, you also cannot overload it.

When an object instance is created from a class, VB6 calls a special sub called Class_Initialize.

Posted By Mike Prestwood, Post #101833, KB Topic: OOP
-Collapse +Expand 30. VB Classic Inheritance (No, but see Implements.)
 

VB classic supports only a single "level" of interface inheritance (abstract class to implimentation class). See Implements in VB's help.

Posted By Mike Prestwood, Post #101396, KB Topic: OOP
-Collapse +Expand 31. VB Classic Inheritance-Multiple (Not Supported)
 

VB classic supports only a single "level" of inheritance (abstract class to implimentation class).

Posted By Mike Prestwood, Post #101450, KB Topic: OOP
-Collapse +Expand 32. VB Classic Interfaces
 

VB6 has limited support for interfaces. You can create an interface of abstract methods and properties and then implement them in one or more descendant classes. It's a single level implementation though (you cannot inherit beyond that). The parent interface class is a pure abstract class (all methods and properites are abstract, you cannot implement any of them in the parent class).

In the single level descendant class, you have to implement all methods and properties and you cannot add any. Your first line of code is Implements InterfaceName.

Posted By Mike Prestwood, Post #101442, KB Topic: OOP
-Collapse +Expand 33. VB Classic Member Method (sub, function)
 

VB classic uses the keywords sub and function. A sub does not return a value and a function does. Many programmers like to use the optional call keyword when calling a sub to indicate the call is to a procedure.

Posted By Mike Prestwood, Post #101751, KB Topic: OOP
-Collapse +Expand 34. VB Classic Member Visibility
 

In VB Classic, the keywords Private, Friend, Public, and Static are used to set access levels for declared elements.

Posted By Mike Prestwood, Post #101825, KB Topic: OOP
Follow PrestwoodBoards on: 


©1995-2019 PrestwoodBoards  [Security & Privacy]
Professional IT Services: Coding | Websites | Computer Tech