Visual Basic - Overview

File Prefixes

form          .frm
standard    .bas
class          .cls
 

General

1 statement per line, _ is line continuation character
: allows more than 1 statement per line
' comment
&011 Octal   &H9 Hex
 

Declaring Variables

Dim variablename [As type]
String, Byte (8 bit), Integer (16 bit), Long (32 bit), Currency, Double, Single, Boolean (true|false)
Public - global, Static - retains value
eg Public variablename As Integer
eg Static variablename As String
Static Function RunningTime (num) defines all variables in function as static
Const conPi = 3.1415
Dim EmpName As String * 50

Date, Object

Arrays

Dim Counters(14) As Integer ' 15 elements 0 -> 14
or Dim Counters (1 To 15) As Integer
or Dim arrX (2) As Varient
                arrX(0) = 1
                arrX(1) = "2"
Dim MatrixA (9, 9) As Double

Dynamic Arrays:
Dim DynArray()
Redim DynArray (x+1) - this blanks out contents of DynArray
Redim Preserve Matrix (uBound (Matrix) + 1)
 

Procedures

[Private | Public][Static] Sub procedurename (args)
....
End Sub

Event Procedures
ControlName _ EventName  eg cmdPlay_Click
for Forms: Form_Click, MDIForm_Load

Functions:
[Private | Public][Static] Function procedureName (args) [As type]
....
End Sub
Return parameter - assign to name of function eg
procedurename = x + 2

Calling Procedures:
Call MyProc (arg1, arg2)
or MyProc arg1, arg2

Calling Functions:
x = myfunc
a = myfunc (a, b, c)
Procedures in forms / classes: Call form1.somesub (args)

Passed Params: default type Varient
else Function a (arg1 As String, arg2 As Integer)
pass by value: Function a (ByVal arg1 As Integer)
 

Loop Structures:

Do While condition
   statements
Loop

Do
   statements
Loop While condition

For counter = start To end [Step increment]
   statements
Next [counter ]

For Each element In group
   statements
Next element

Exit For, Exit Do, Exit Sub, Exit Function
 
 

Nicholas Applegate Structure

All database access in one module DB.BAS, using RDO.
Example database routine

Function DB_GetRestrictedAccounts(lBlockID As Long, lBrokerID As Long, _
                                    rAccounts() As PickList) As Integer
'************************************************************
' This routine calls the stored procedure batch_get_restricted_accounts
' to get a list of accounts that are restricting a given broker.
'************************************************************
Dim rdoSet As rdoResultset
Dim i As Integer

    On Error Resume Next

    msSQL = "batch_get_restricted_accounts " & lBlockID & "," & lBrokerID
    rdoErrors.Clear
    Set rdoSet = rdoLandmarkClient.OpenResultset(msSQL)

    If rdoErrors.Count = 1 then
        If rdoErrors(0).number = 40041 Then rdoErrors.Clear
    End If

    If rdoErrors.Count = 0 Then
        i = -1
        ReDim rAccounts(10)
        Do While Not rdoSet.EOF
            i = i + 1
            If i > UBound(rAccounts()) Then ReDim Preserve rAccounts(i + 10)
            rAccounts(i).lngID = Val(rdoSet(0) & "")
            rAccounts(i).sValue = rdoSet(1) & ""
            rdoSet.MoveNext
        Loop
    End If

    rdoSet.Close
    Set rdoSet = Nothing

    DB_GetRestrictedAccounts = i

    On Error GoTo 0

End Function

Another function used to default allocations to a ticket:
Sub DB_Reallocate(lTicketID As Long, dMinAllocation As Double, _
            dLotRound As Double, dOverAllocationPercent As Double, sError As String)
'***********************************************************************************
' This routine calls the stored procedure reallocate to provide default allocations.
'***********************************************************************************
    Dim bDeadlock As Boolean
 
    msSQL = "landmark..nacm_stepout_reallocate "
    msSQL = msSQL & lTicketID & ","
    msSQL = msSQL & dMinAllocation & ","
    msSQL = msSQL & dLotRound & ","
    msSQL = msSQL & dOverAllocationPercent & ","
    msSQL = msSQL & "1"                                   ' Method

    Do ' Deadlock loop
 
        DB_ExecuteSQL msSQL
 
        If rdoErrors.Count <> 0 Then
            If rdoErrors(0).number = 1205 Then ' 1205 is Deadlock error code
                bDeadlock = True
            End If
        Else
            bDeadlock = False
        End If
 
    Loop While bDeadlock = True
 
    If rdoErrors.Count = 0 Then
        sError = ""
    Else
        sError = FormatError(msSQL)
    End If
 
End Sub

Function DB_ExecuteSQL(sSQL As String) As Boolean
'**********************************************************
' This routine executes SQL statements
' and returns true if no error occurs, false otherwise
'**********************************************************
    On Error GoTo DB_ExecuteSQL_err
 
    rdoErrors.Clear
    rdoLandmarkClient.Execute sSQL
 
    On Error Resume Next
 
    If rdoErrors.Count = 1 Then
        If rdoErrors(0).number = 40041 Then rdoErrors.Clear
    End If
 
    DB_ExecuteSQL = (rdoErrors.Count = 0)

DB_ExecuteSQL_res:
    Exit Function

DB_ExecuteSQL_err:
    '''MsgBox GetRDOErrorMessage(), vbInformation
    goNATSLog.RecordRDOErrors "db.DB_ExecuteSQL"
    Resume Next

End Function

Private Function FormatError(sSQL As String) As String
    Dim iStart As Integer, i As Integer, sTemp As String

    If gbDebugMode Then
        sTemp = sSQL
        For i = 0 To rdoErrors.Count - 1
            sTemp = vbCrLf & vbCrLf & rdoErrors(i).number & vbCrLf & _
                    rdoErrors(i).description & vbCrLf & rdoErrors(i).Source
        Next 'i
    Else
        For i = 0 To rdoErrors.Count - 1
            iStart = InstrTblB2(Len(rdoErrors(i).description), _
                                rdoErrors(i).description, "]")
            If iStart > 0 Then
                sTemp = Mid$(rdoErrors(i).description, iStart + 1)
            Else
                sTemp = rdoErrors(i).description
            End If
        Next 'i
    End If
 
    FormatError = sTemp
 
End Function

The program started in a sub main () in a SUBMAIN.BAS
This opened a password window to prompt for Database Server, Username and Password
They were used to connect to the database, then an MDI Form was opened with the menus and any saved settings from the .ini file

There was a GLOBALS.BAS for structure declarations and global variables, a UTILS.BAS for utility functions, eg string handling etc, a DBGRID.BAS for True DB Grid related functions
 

Hosted by www.Geocities.ws

1