Option Base 1 ' Set default array subscripts to 1.

Type Record    ' Define user-defined type.
    ID As Integer
    Name As String * 20
End Type

Sub sub1()
Dim Lower
Dim MyArray(20), TwoDArray(3, 4) ' Declare array variables.
Dim ZeroArray(0 To 5) ' Override default base subscript.
' Use LBound function to test lower bounds of arrays.
Lower = LBound(MyArray) ' Returns 1.
Lower = LBound(TwoDArray, 2) ' Returns 1.
Lower = LBound(ZeroArray) ' Returns 0.
End Sub

Sub sub2()
Open "TESTFILE" For Binary Access Write Lock Write As #1
' Close before reopening in another mode.
Close #1
End Sub

Sub sub3()
Dim SearchString, SearchChar, MyPos
SearchString = "XXpXXpXXPXXP"   ' String to search in.
SearchChar = "P"    ' Search for "P".

' A textual comparison starting at position 4. Returns 6.
MyPos = InStr(4, SearchString, SearchChar, 1)

' A binary comparison starting at position 1. Returns 9.
MyPos = InStr(1, SearchString, SearchChar, 0)

' Comparison is binary by default (last argument is omitted).
MyPos = InStr(SearchString, SearchChar)    ' Returns 9.

MyPos = InStr(1, SearchString, "W")    ' Returns 0.
End Sub

Sub sub4()
Dim MyPath
MyPath = CurDir    ' Returns "C:\WINDOWS\SYSTEM".
MyPath = CurDir("C")    ' Returns "C:\WINDOWS\SYSTEM".
MyPath = CurDir("D")    ' Returns "D:\EXCEL".
End Sub

Sub sub5()
Dim X
X = Dir("SomePath", MacID("TEXT"))
End Sub
    ' Parameter rate is a ByVal parameter because the procedure should
    ' not change the value of the corresponding argument in the
    ' calling code.

    ' The calculated value of the debt parameter, however, should be
    ' reflected in the value of the corresponding argument in the
    ' calling code. Therefore, it must be declared ByRef.
Sub Calculate(ByVal rate As Double, ByRef debt As Double)
  debt = debt + (debt * rate / 100)
End Sub

Sub Main()
  ' Two interest rates are declared, one a constant and one a
        ' variable.
        Const highRate As Double = 12.5
        Dim lowRate As Double
        lowRate = highRate * 0.6

        Dim initialDebt As Double
        initialDebt = 4999.99
        ' Make a copy of the original value of the debt.
        Dim debtWithInterest As Double
        debtWithInterest = initialDebt

        ' Calculate the total debt with the high interest rate applied.
        ' Argument highRate is a constant, which is appropriate for a
        ' ByVal parameter. Argument debtWithInterest must be a variable
        ' because the procedure will change its value to the calculated
        ' total with interest applied.
        Calculate highRate, debtWithInterest
        ' Format the result to represent currency, and display it.
                
        Dim debtString As String
        debtString = Format(debtWithInterest, "C")
        Debug.Print ("What I owe with high interest: " & debtString)

        ' Repeat the process with lowRate. Argument lowRate is not a
        ' constant, but the ByVal parameter protects it from accidental
        ' or intentional change by the procedure.

        ' Set debtWithInterest back to the original value.
        debtWithInterest = initialDebt
        Calculate lowRate, debtWithInterest
        debtString = Format(debtWithInterest, "C")
        Debug.Print ("What I owe with low interest:  " & debtString)

        
End Sub
Sub sub6()
Dim demoStr1, demoStr2 As String
demoStr1 = "Hello"
Rem Comment after a statement using REM.
demoStr2 = "Goodbye" ' Comment after a statement using the ' character.
Rem This entire line is a comment.
' This entire line is also a comment.
End Sub

Sub sub7()
Dim MyString
MyString = "0123456789"   ' Initialize string.
RSet MyString = "Right->" ' MyString contains " Right->".
Dim MyString
MyString = "0123456789" ' Initialize string.
LSet MyString = "<-Left" ' MyString contains "<-Left ".
Dim A, B, Check
A = 5: B = 5 ' Initialize variables.
Check = CBool(A = B) ' Check contains True.
 
A = 0 ' Define variable.
Check = CBool(A) ' Check contains False.
Dim MyDouble, MyByte
MyDouble = 125.5678 ' MyDouble is a Double.
MyByte = CByte(MyDouble) ' MyByte contains 126.
Dim MyDouble, MyCurr
MyDouble = 543.214588 ' MyDouble is a Double.
MyCurr = CCur(MyDouble * 2) ' Convert result of MyDouble * 2
 ' (1086.429176) to a
 ' Currency (1086.4292).
 Dim MyDate, MyShortDate, MyTime, MyShortTime
MyDate = "February 12, 1969" ' Define date.
MyShortDate = CDate(MyDate) ' Convert to Date data type.
 
MyTime = "4:35:47 PM" ' Define time.
MyShortTime = CDate(MyTime) ' Convert to Date data type.
Dim MyCurr, MyDouble
MyCurr = CCur(234.456784) ' MyCurr is a Currency.
MyDouble = CDbl(MyCurr * 8.2 * 0.01) ' Convert result to a Double.

Dim MyDecimal, MyCurr
MyCurr = 10000000.0587 ' MyCurr is a Currency.
MyDecimal = CDec(MyCurr) ' MyDecimal is a Decimal.

Dim MyDouble, MyInt
MyDouble = 2345.5678 ' MyDouble is a Double.
MyInt = CInt(MyDouble) ' MyInt contains 2346.

Dim MyVal1, MyVal2, MyLong1, MyLong2
MyVal1 = 25427.45: MyVal2 = 25427.55 ' MyVal1, MyVal2 are Doubles.
MyLong1 = CLng(MyVal1) ' MyLong1 contains 25427.
MyLong2 = CLng(MyVal2) ' MyLong2 contains 25428.

Dim MyDouble1, MyDouble2, MySingle1, MySingle2
' MyDouble1, MyDouble2 are Doubles.
MyDouble1 = 75.3421115: MyDouble2 = 75.3421555
MySingle1 = CSng(MyDouble1) ' MySingle1 contains 75.34211.
MySingle2 = CSng(MyDouble2) ' MySingle2 contains 75.34216.

Dim MyDouble, MyString
MyDouble = 437.324 ' MyDouble is a Double.
MyString = CStr(MyDouble) ' MyString contains "437.324".


Dim MyInt, MyVar
MyInt = 4534 ' MyInt is an Integer.
MyVar = CVar(MyInt & 0)   ' MyVar contains the string
 ' 4534000.
 
 
End Sub

Sub sub8()
Dim MyDate
MyDate = #2/12/1969#
MyDate = #2/12/1970#
MyDate = #2/12/1971#
MyDate = #2/11/1969#
MyDate = #2/10/1969#
MyDate = #3/12/1969#
MyDate = #4/12/1969#
End Sub

Sub sub9()
Dim MyChar
Open "TESTFILE" For Input As #1    ' Open file for reading.
Do While Not EOF(1)    ' Loop until end of file.
    MyChar = Input(1, #1)    ' Read next character of data.
    Debug.Print Seek(1)    ' Print byte position to the Immediate window.
Loop
Close #1    ' Close file.

Sub sub10()
Dim MyRecord As Record, RecordNumber    ' Declare variables.
' Open sample file for random access.
Open "TESTFILE" For Random Shared As #1 Len = Len(MyRecord)
RecordNumber = 4    ' Define record number.
Lock #1, RecordNumber    ' Lock record.
Get #1, RecordNumber, MyRecord    ' Read record.
MyRecord.ID = 234    ' Modify record.
MyRecord.Name = "John Smith"
Put #1, RecordNumber, MyRecord    ' Write modified record.
Unlock #1, RecordNumber    ' Unlock current record.
Close #1    ' Close file.
End Sub

Sub sub11()
Dim MyString, FirstWord, LastWord, MidWords
MyString = "Mid Function Demo"    ' Create text string.
FirstWord = Mid(MyString, 1, 3)    ' Returns "Mid".
LastWord = Mid(MyString, 14, 4)    ' Returns "Demo".
MidWords = Mid(MyString, 5)    ' Returns "Function Demo".

' Assign Boolean, Date, Null and Error values.
Dim MyBool, MyDate, MyNull, MyError
MyBool = False: MyDate = #2/12/1969#: MyNull = Null
MyError = CVErr(32767)
' True, False, Null, and Error are translated using locale settings of
' your system. Date literals are written using standard short date
' format.
Print #1, MyBool; " is a Boolean value"
Print #1, MyDate; " is a date"
Print #1, MyNull; " is a null value"
Print #1, MyError; " is an error value"
Close #1 ' Close file.

End Sub

Sub sub12()
Dim OldName, NewName
OldName = "OLDFILE": NewName = "NEWFILE" ' Define file names.
Name OldName As NewName ' Rename file.
End Sub
