First Steps in Programming - Basic Commands and Keywords

 


1. What constants and types are used in Basic Programming?

Here is the list of program constants and types inbuilt into Visual Basic and VBA:

Colour Constants:

vbBlack = BLACK
vbRed = LIGHT_RED
vbGreen = LIGHT_GREEN
vbYellow = LIGHT_YELLOW
vbBlue = LIGHT_BLUE
vbMagenta = LIGHT_MAGENTA
vbCyan = LIGHT_CYAN
vbWhite = WHITE

Comparison Constants:

vbBinaryCompare = BINARYCOMPARE
vbTextCompare = TEXTCOMPARE

Control Constants and Property Constants:

Alignment property...
vbLeftJustify = 0
vbRightJustify = 1
vbCenter = 2

Borderstyle property...
vbBSNone = 0
vbFixedSingle = 1
vbSizable = 2
vbFixedDialog = 3
vbFixedToolWindow = 4
vbSizableToolWindow = 5

Borderstyle property (shapes and lines)...
vbTransparent = 0
vbBSSolid = 1
vbBSDash = 2
vbBSDot = 3
vbBSDashDot = 4
vbBSDashDotDot = 5
vbBSInsideSolid = 6

Combobox object...
vbComboDropdown = 0
vbComboSimple = 1
vbComboDropdownList = 2

Checkbox object...
vbUnchecked = 0
vbChecked = 1
vbGrayed = 2

Listbox object...
vbListBoxStandard = 0
vbListBoxCheckBox = 1
vbMultiSelectNone = 0
vbMultiSelectSimple = 1
vbMultiSelectExtended = 2

Picture types...
vbPicTypeNone = 0
vbPicTypeBitmap = 1
vbPicTypeMetafile = 2
vbPicTypeIcon = 3
vbPicTypeEMetaFile = 4

Scrollbar object...
vbSBNone = 0
vbHorizontal = 1
vbVertical = 2
vbBoth = 3

Shape object...
vbShapeRectangle = 0
vbShapeSquare = 1
vbShapeOval = 2
vbShapeCircle = 3
vbShapeRoundedRectangle = 4
vbShapeRoundedSquare = 5

Window state property...
vbNormal = 0
vbMinimized = 1
vbMaximized = 2

Date and Time Constants:

vbSunday = 1
vbMonday = 2
vbTuesday = 3
vbWednesday = 4
vbThursday = 5
vbFriday = 6
vbSaturday = 7

Date Format Constants:

vbGeneralDate = 0
vbLongDate = 1
vbShortDate = 2
vbLongTime = 3
vbShortTime = 4

File Constants:

vbNormal = 0
vbReadOnly = 1
vbHidden = 2
vbSystem = 4
vbVolume = 8
vbDirectory = 16
vbArchive = 32

Msgbox Constants:

vbOKOnly = MB_OK (0x0)
vbOKCancel = MB_OKCANCEL (0x1)
vbAbortRetryIgnore = MB_ABORTRETRYIGNORE (0x2)
vbYesNoCancel = MB_YESNOCANCEL (0x3)
vbYesNo = MB_YESNO (0x4)
vbRetryCancel = MB_RETRYCANCEL (0x5)
vbCritical = MB_ICONSTOP (0x10)
vbQuestion = MB_ICONQUESTION (0x20)
vbExclamation = MB_ICONEXCLAMATION (0x30)
vbInformation = MB_ICONINFORMATION (0x40)
vbDefaultButton1 = MB_DEFBUTTON1 (0x0)
vbDefaultButton2 = MB_DEFBUTTON2 (0x100)
vbDefaultButton3 = MB_DEFBUTTON3 (0x200)
vbDefaultButton4 = MB_DEFBUTTON4 (0x300)
vbApplicationModal = MB_APPLMODAL (0x0)
vbSystemModal = MB_SYSTEMMODAL (0x1000)
vbMsgBoxHelpButton = MB_HELP (0x4000)

Msgbox Return Constants:

vbOK = 1 (IDOK)
vbCancel = 2 (IDCANCEL)
vbAbort = 3 (IDABORT)
vbRetry = 4 (IDRETRY)
vbIgnore = 5 (IDIGNORE)
vbYes = 6 (IDYES)
vbNo = 7 (IDNO)
8 (IDCLOSE)
9 (IDHELP)
10 (IDTRYAGAIN)
11 (IDCONTINUE)

Shell Constants:

vbHide = 0 (SW_HIDE)
vbNormalFocus = 1 (SW_SHOWNORMAL)
vbMinimizedFocus = 2 (SW_SHOWMINIMIZED)
vbMaximizedFocus = 3 (SW_SHOWMAXIMIZED)
vbNormalNoFocus = 4 (SW_SHOWNOACTIVATE)
vbMinimizedNoFocus = 6 (SW_MINIMIZE)

Show mode Constants:

vbModeless = 0
vbModal = 1

String Constants:

vbCr = Chr(13)
vbCrLf = Chr(13) & Chr(10)
vbFormFeed = Chr(12)
vbLf = Chr(10)
vbNewLine = vbCrLf
vbNullChar = Chr(0)
vbNullString = C NULL pointer
vbTab = Chr(9)
vbVerticalTab = Chr(11)

vbUpperCase = 1
vbLowerCase = 2
vbProperCase = 3

VarType Constants:

vbEmpty = 0 (empty records and unions)
vbNull = 1
vbInteger = 2
vbLong = 3
vbSingle = 4
vbDouble = 5
vbCurrency = 6
vbDate = 7
vbString = 8
vbObject = 9
vbError = 10
vbBoolean = 11
vbVariant = 12
vbDataObject = 13
vbDecimal = 14
vbByte = 17
vbArray = 8192

Type equivalents:

boolean = true/false (1 byte) 

enum = 255 values (1 byte) 

integer = signed 4 bytes 

long = integer 

currency = fixed point (8 bytes) 

double = floating point (8 bytes) 

single = double 

numeric = integer | currency | double 

date = string YYYY-MM-DD hh:mm:ss

collection = list 

type TError = record
  number as integer
  source as string (project name)
  description as string
  helpfile as string
  helpcontext as integer
end type

2. What keywords are used?

Here is the list of keywords inbuilt into Visual Basic and VBA:

Empty = 0 (intended for a Variant with nothing in it)

False = 0

Me = refers to current form

Nothing = COM object null reference

Null = Constant for databases

True = -1 (in Basic)

3. What functions are used?

Here is the list of functions inbuilt into Visual Basic. This includes some functions from VBscript and excludes COM functions (COM functions are implemented separately by importing their type libraries):

Abs

function Abs(byval number as numeric) as numeric

Array

function Array(byval expr as any,...) as array

Asc

function Asc(byval str as string) as integer

Atn

function Atn(byval number as numeric) as double

CBool

function CBool(byval expr as numeric|string) as boolean

CByte

function CByte(byval expr as numeric) as integer

CCur

function CCur(byval value as numeric) as currency

CDate

function CDate(byval value as numeric|string) as date

CDbl

function CDbl(byval value as numeric) as double

Choose

function Choose(byval index as integer, byval expr as any,...) as any

Chr

function Chr(byval value as integer) as string

CInt

function CInt(byval value as numeric) as integer

CLng

function CLng(byval value as numeric) as long

Command

function Command() as string

Cos

function Cos(byval number as numeric) as double

CSng

function CSng(byval expr as numeric) as single

CStr

function CStr(byval expr as any) as string

Curdir

function Curdir([byval drive as string]) as string

CVerr

function CVerr(byval errno as integer) as TError

Date

function Date() as string|date

DateAdd

function DateAdd(byval intervalType as string, byval intervalAmount as integer, byval oldDate as date) as date

DateDiff

function DateDiff(byval intervalType as string, byval date1 as date, byval date2 as date) as integer

DatePart

function DatePart(byval intervalType as string, byval date1 as date) as integer

DateSerial

function DateSerial(byval yyyy as integer, byval mm as integer, byval dd as integer) as date

DateValue

function DateValue(byval datestr as string) as date

Day

function Day(byval expr as string|date) as integer

DDB

function DDB(byval initialCost as double, byval residualValue as double, byval assetLife as double, byval period as double) as double

Dir

function Dir([byval pattern as string [,byval mode as integer]]) as string

DoEvents

function DoEvents() as integer

Environ

function Environ(byval envvar as string|integer) as string

Eof

function Eof(byval filenumber as integer) as boolean

Error

function Error([byval errornumber as integer]) as string

Escape

function Escape(byval expr as string) as string [VBscript]

Eval

function Eval(byval expr as string) as numeric [VBscript]

Exp

function Exp(byval value as numeric) as double

FileAttr

function FileAttr(byval filenumber as integer [,byval unused as integer]) as integer

FileDateTime

function FileDateTime(byval pathname as string) as date

Filelen

function Filelen(byval filename as string) as integer

Filter

function Filter(byval strings as array, byval strSearch as string [,byval fInclude as boolean [,byval iCompareMode as integer]]) as array

Fix

function Fix(byval number as numeric) as integer

Format

function Format(byval expr as any [, byval format as string]) as string

FormatCurrency

function FormatCurrency(byval expr as currency [,byval iFractionalDigits as integer [,byval iLeadingDigit as integer [,byval iUseBrackets as integer [,byval iGroupDigits as integer]]]]) as string

FormatDateTime

function FormatDateTime(byval datestr as date [,byval iFormat as integer]) as string

FormatNumber

function FormatNumber(byval expr as numeric [,byval iFractionalDigits as integer [,byval iLeadingDigit as integer [,byval iUseBrackets as integer [,byval iGroupDigits as integer]]]]) as string

FormatPercent

function FormatPercent(byval expr as numeric [, byval iFractionalDigits as integer [, byval iLeadingDigit as integer [, byval iUseBrackets as integer [, byval iGroupDigits as integer]]]]) as string

Freefile

function Freefile([byval range as integer]) as integer

FV

function FV(byval interestRatePerPeriod as double, byval totalPayments as integer, byval eachPayment as double [,byval presentValue as double [,byval iPayAtPeriodStart as integer]]) as double

GetAllSettings

function GetAllSettings(byval applicationName as string, byval sectionName as string) as array

GetAttr

function GetAttr(byval filename as string) as integer

GetSetting

function GetSetting(byval applicationName as string, byval sectionName as string, byval keyName as string [,byval defaultValue as string]) as string

Hex

function Hex(byval number as numeric) as string

Hour

function Hour(byval expr as numeric|string|date) as integer

IIf

function IIf(byval expr as boolean, byval valueIfTrue as any, byval valueIfValse as any) as any

Input

function Input(byval numberChars as integer, byval filenumber as integer) as string

InputBox

function InputBox(byval prompt as string [,byval title as string] [,byval default as string] [,byval xpos as integer, byval ypos as integer] [,byval helpfile as string, byval helpcontext as integer]) as string

Instr

function Instr([byval start as integer,] byval str as string, byval substr as string [,byval iCompareMode as integer]) as integer

Instrrev

function Instrrev([byval start as integer,] byval str as string, byval substr as string [,byval iCompareMode as integer]) as integer

Instrrev

function Instrrev(byval str as string, byval substr as string [,byval start as integer [,byval iCompareMode as integer]]) as integer

Int

function Int(byval number as numeric) as integer

IPmt

function IPmt(byval interestRatePerPeriod as double, byval paymentPeriod as double, byval totalPayments as integer, byval presentValue as double [,byval finalValue as double [,byval iPayAtPeriodStart as integer]]) as double

IRR

function IRR(byval arrCashFlow as array [,byval estimate as double) as double

IsArray

function IsArray(byval expr as any) as boolean

IsDate

function IsDate(byval expr as date|string) as boolean

IsEmpty

function IsEmpty(byval expr as any) as boolean

IsError

function IsError(byval expr as integer) as boolean

IsMissing

function IsMissing(byval expr as any) as boolean

IsNull

function IsNull(byval expr as any) as boolean

IsNumeric

function IsNumeric(byval expr as string) as boolean

Join

function Join(byval arrStrings as array [,byval strSeparator as string]) as string

Lbound

function Lbound(byval expr as array [,byval dimension as integer]) as integer

Lcase

function Lcase(byval str as string) as string

Left

function Left(byval str as string, byval count as integer) as string

Len

function Len(byval str as string|any) as integer

Like

function Like(byval test as string, byval pattern as string [,byval iCompareMode as integer]) as boolean

LoadPicture

function LoadPicture([byval picturefile as string]) as picture|control

Loc

function Loc(byval filenumber as integer) as integer

Lof

function Lof(byval filenumber as integer) as integer

Log

function Log(byval number as numeric) as double

Ltrim

function Ltrim(byval str as string) as string

Mid

function Mid(byval str as string, byval start as integer [,byval count as integer]) as string

Minute

function Minute(byval expr as string|date) as integer

MIRR

function MIRR(byval arrCashFlow as array, byval borrowingRate as double, byval lendingRate as double) as double

Month

function Month(byval expr as string|date) as integer

MonthName

function MonthName(byval expr as integer [,byval fAbbreviate as boolean]) as string

Msgbox

function Msgbox(byval prompt as string [,byval buttons as integer] [,byval title as string] [,byval helpfile as string, byval helpcontext as integer]) as integer

Now

function Now() as date

Nper

function Nper(byval interestRatePerPeriod as double, byval eachPayment as double, byval presentValue as double [,byval futureValue as double [,byval iPayAtPeriodStart as integer]]) as double

NPV

function NPV(byval discountRate as double, byval arrCashFlow as array) as double

Oct

function Oct(byval number as numeric) as string

Partition

function Partition(byval value as integer, byval startNumber as integer, byval endNumber as integer, byval interval as integer) as string

Pmt

function Pmt(byval interestRatePerPeriod as double, byval totalPayments as integer, byval startValue as double [,byval endValue as double [,byval iPayAtPeriodStart as integer]]) as double

PPmt

function PPmt(byval interestRatePerPeriod as double, byval iDesiredPeriod as integer, byval totalPayments as integer, byval startValue as double [,byval endValue as double [,byval iPayAtPeriodStart as integer]]) as double

PV

function PV(byval interestRatePerPeriod as double, byval totalPayments as integer, byval eachPayment as double [,byval endValue as double [,byval iPayAtPeriodStart as integer]]) as double

QBColor

function QBColor(byval index as integer) as integer

Rate

function Rate(byval totalPayments as integer, byval eachPayment as double, byval startValue as double [,byval endValue as double [,byval iPayAtPeriodStart as integer [,byval initialEstimate as double]]]) as double

Replace

function Replace(byval str as string, byval old as string, byval new as string [,byval start as integer [,byval count as integer [,byval iCompareMode as integer]]]) as string

RGB

function RGB(byval red as integer, byval green as integer, byval blue as integer) as integer

Right

function Right(byval str as string, byval count as integer) as string

Rnd

function Rnd() as double

Round

function Round(byval number as numeric [,byval iFractionalPlaces as integer]) as integer

Rtrim

function Rtrim(byval str as string) as string

Second

function Second(byval expr as string|date) as integer

Seek

function Seek(byval filenumber as integer) as integer

Sgn

function Sgn(byval number as numeric) as integer

Shell

function Shell(byval filename as string [,byval args as string] [,byval showmode as integer]) as integer

Sin

function Sin(byval number as numeric) as double

SLN

function SLN(byval startValue as double, byval endValue as double, byval lifetime as double) as double

Space

function Space(byval length as integer) as string

Split

function Split(byval expr as string [,byval strSeparator as string [,byval count as integer [,byval iCompareMode as integer]]]) as array

Sqr

function Sqr(byval number as numeric) as double

Str

function Str(byval expr as numeric) as string

Strcomp

function Strcomp(byval string1 as string, byval string2 as string [,byval iCompareMode as integer]) as integer

Strconv

function Strconv(byval string1 as string, byval conversion as integer) as string

String

function String(byval count as integer, byval charCode as integer|string) as string

StrReverse

function StrReverse(byval str as string) as string

Switch

function Switch(byval boolexp1 as boolean, byval result1 as any, ...) as any

SYD

function SYD(byval startValue as double, byval endValue as double, byval lifetime as double, byval period as double) as double

Tan

function Tan(byval angle as numeric) as double

Time

function Time() as string|date

Timer

function Timer() as numeric

TimeSerial

function TimeSerial(byval hour as integer, byval minute as integer, byval second as integer) as string|date

TimeValue

function TimeValue(byval timestr as string) as date

Trim

function Trim(byval str as string) as string

TypeName

function TypeName(byval expr as any) as string

Ubound

function Ubound(byval expr as array [,byval dimension as integer]) as integer

Ucase

function Ucase(byval str as string) as string

Unescape

function Unescape(byval expr as string) as string [VBscript]

Val

function Val(byval string1 as string) as numeric

VarType

function VarType(byval expr as any) as integer

Weekday

function Weekday(byval expr as string|date) as integer

WeekdayName

function WeekdayName(byval expr as integer [,byval fAbbreviate as boolean]) as string

Year

function Year(byval expr as string|date) as integer

4. What statements are used?

Here is the list of statements inbuilt into Visual Basic, including some functions from VBscript. All Basic statements that correspond to calls to library code or to other executable code have been made into subs, whereas Basic statements corresponding to control structures are kept as such:

AppActivate

sub AppActivate(byval title as string [,byval waitmode as integer])

Beep

sub Beep()

Call

Call name(arguments)

Chdir

sub Chdir(byval newdir as string)

ChDrive

Sub ChDrive(byval newdrive as string)

Class

Class name
statements
End Class

Close

Sub Close(byval filenumber as integer,...)

Const

[public|private] Const name [as type] = expression

Date

Date = string|date

Declare

[public|private] Declare Sub id Lib strDllName [Alias strEntryPointName] ( [ arguments ] )

[public|private] Declare Function id Lib strDllName [Alias strEntryPointName] ( [ arguments ] ) as type

DeleteSetting

sub DeleteSetting(byval ApplicationName as string, byval SectionName as string [,byval key as string])

Dim

(Dim|Public|Private) name [as type]

(Dim|Public|Private) name ( [int to] int[, [int to] int]* ) [as type]

Do

Do (while|until) expression
statements
[exit do
statements]
Loop

Do
statements
[exit do
statements]
Loop (while|until) expression

End

sub End()

Enum

[public|private] enum enumName
[
id = expr
]+
end enum

Erase

sub Erase(byref arrayvar as array)

Error

sub Error(byval errornumber as integer)

Filecopy

sub Filecopy(byval sourcefile as string, byval destfile as string)

For Each

For Each name in ( arrayvar | collectionvar )
statements
[exit for
statements]
Next [ name ]

For

For id = startexp to endexp [ step expr ]
statements
[exit for
statements]
Next [id]

Function

[public|friend|private] Function name ( arguments ) [as type]
[
statements
name = expression
exit function
]*
end function

Get

sub Get(byval filenumber as integer [,byval recordnumber as integer] ,byref dest as any)

If

if expr then
statements
[ elseif expr then
statements ]
[ else
statements ]
end if

Input

sub Input(byval filenumber as integer, byref id as any,...)

Kill

sub Kill(byval filepattern as string)

Let

[Let] id = expression

LineInput

sub LineInput(byval filenumber as integer, byref expr as string)

Load

sub Load(byval obj as form)

Lset

Lset strId = strExp

Lset id = id

Mid

sub Mid(byref strVar as string, byval start as integer [,byval count as integer], byval strNew as string)

Mkdir

sub Mkdir(byval dir as string)

Name

sub Name(byval oldfile as string, byval newfile as string)

On error

On error goto 0

On error goto label

On error resume next

Open

sub Open(byval filename as string, byval mode as enum, byval access as enum, byref filenum as integer [,byval recordLength as integer])

Option

Option Base (0|1)

Option Compare (Binary|Text)

Option Explicit

Print

sub Print(byval filenumber as integer [,expr as any]*)

Property Get

[public|friend|private] Property Get name ( arguments ) [as type]
[
statements
name = expression
exit property
]*
end property

Property Let

[public|friend|private] Property (Let|Set) name ( arguments )
[
statements
exit property
]*
end property

Put

sub Put(byval filenumber as integer [,byval recordNumber as integer], byval expr as any)

Randomize

Randomize

Redim

Redim [preserve] arrayvar ( arguments )

Rem

Rem

Reset

Reset

Resume

Resume [0 | next]

Rmdir

sub Rmdir(byval dirname as string)

Rset

Rset strId = strExp

SavePicture

sub SavePicture(byval picture as Control, byval bmpfile as string)

SaveSetting

sub SaveSetting(byval ApplicationName as string, byval SectionName as string, byval key as string, byval value as string)

Seek

sub Seek(byval filenumber as integer, byval filepos as integer)

Select Case

Select Case expression
[
case caselabel [,caselabel]*
statements
]*
[
case else
statements
]
End Select

caselabel = expr | expr to expr | is relationalOperator expr

Sendkeys

sub Sendkeys(byval strKeys as string [, byval fWait as boolean])

SetAttr

sub SetAttr(byval filename as string, byval attributes as integer)

Stop

Stop

Sub

[public|friend|private] Sub name ( arguments )
[
statements
exit sub
]*
end sub

Time

Time = strExp | dateExp

Type

[public|private] Type id
[
id [(subscripts)] as type
]+
end type

Unload

sub Unload(byval obj as Form)

While

While expression
statements
wend

With

With id
statements
End With

Write

sub Write(byval filenumber as integer [,byval expr as any]*)