VISUAL BASIC 6 - HÀM VÀ VÍ DỤ

download VISUAL BASIC 6 - HÀM VÀ VÍ DỤ

of 215

Transcript of VISUAL BASIC 6 - HÀM VÀ VÍ DỤ

  • 7/30/2019 VISUAL BASIC 6 - HM V V D

    1/215

    Exam CHAPTER 1

    HM ABS

    Dim MyNumber

    MyNumber = Abs(50.3)

    MyNumber = Abs(-50.3)

    HM ARRAY

    Dim MyWeek, MyDay

    MyWeek = Array("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun")

    MyDay = MyWeek(2)

    MyDay = MyWeek(4)

    HM ASC

    Dim MyNumber

    MyNumber = Asc("A")

    MyNumber = Asc("a")MyNumber = Asc("Apple")

    HM ASC

    Dim pi

    pi = 4 * Atn(1)

    MsgBox pi

    HM CALLBYNAME

    Private Sub Command1_Click()

    CallByName Text1, "MousePointer", VbLet, vbCrosshair

    Result = CallByName(Text1, "MousePointer", VbGet)CallByName Text1, "Move", VbMethod, 100, 100

    End Sub

    HM CHOOSE

    Private Sub Command1_Click()

    i = GetChoice(2)

    MsgBox i

    End Sub

    Function GetChoice(Ind As Integer)

    GetChoice = Choose(Ind, "Speedy", "United", "Federal")End Function

  • 7/30/2019 VISUAL BASIC 6 - HM V V D

    2/215

    HM CHR

    Private Sub Command1_Click()

    Dim MyChar

    MyChar = Chr(65)

    MyChar = Chr(97)

    MyChar = Chr(62)

    MyChar = Chr(37)

    End Sub

    HM COS

    Private Sub Command1_Click()

    Dim MyAngle, MySecant

    MyAngle = 1.3

    MySecant = 1 / Cos(MyAngle)

    MsgBox MySecant

    End Sub

    HM CREATEOBJECT

    Private Sub Command1_Click()

    Dim Conn, Rst

    Dim strData As String, strCon As String

    Set Conn = CreateObject("ADODB.Connection")

    Set Rst = CreateObject("ADODB.Recordset")

    strCon= "Provider=Microsoft.Jet.OLEDB.3.51;"

    strCon=strConn &"Persist Security Info=False;"

    strCon=strConn&"Data Source=C:\Northwind.mdb"

    Conn.Open strCon

    Rst.CursorLocation = 3

    Rst.Open "Select * from Customers", Conn, 0, 3

    If Rst.RecordCount > 0 Then

    Rst.MoveFirst

    Do Until Rst.EOF

    strData = strData & Rst("CompanyName")

    Rst.MoveNext

    Loop

    End If

    Rst.Close

    Conn.Close

    Set Rst = Nothing

  • 7/30/2019 VISUAL BASIC 6 - HM V V D

    3/215

    Set Conn = Nothing

    End Sub

    HM CURDIR

    Private Sub Command1_Click()

    Dim strPath As String

    strPath = CurDir()

    MsgBox strPath

    End Sub

    Private Sub Command1_Click()

    Dim strPath As String

    strPath = App.Path()

    MsgBox strPath

    End Sub

    HM DATE

    Private Sub Command1_Click()

    MsgBox Date$

    MsgBox Date

    End Sub

    HM DATEADD

    Private Sub Command1_Click()

    Dim FirstDate As Date

    Dim IntervalType As String

    Dim Number As Integer

    Dim Msg As String

    IntervalType = "m"

    FirstDate = Date

    Number= InputBox("Enter number of months to add")

    Msg = "New date: " & DateAdd(IntervalType, Number, FirstDate)

    MsgBox Msg

    IntervalType = "d"

    Number = InputBox("Enter number of day to add")

    Msg = "New date: " & DateAdd(IntervalType, Number, FirstDate)

    MsgBox Msg

    End Sub

    HM DATEDIFF

    Dim TheDate As Date

    Dim Msg

    TheDate = InputBox("Enter a date")

  • 7/30/2019 VISUAL BASIC 6 - HM V V D

    4/215

    Msg = "Days from today: " & DateDiff("d", Now, TheDate)

    MsgBox Msg

    HM DATEPART

    Dim TheDate As Date

    Dim Msg

    TheDate = InputBox("Enter a date")

    Msg = "Quarter: " & DatePart("q", TheDate)

    MsgBox Msg

    HM DATESERIAL

    Private Sub Command1_Click()

    Dim MyDate

    MyDate = DateSerial(1969, 2, 12)

    MsgBox MyDate

    MyDate = DateSerial(1969, 35, 12)

    MsgBox MyDate

    MyDate = DateSerial(1990 - 10, 8 - 2, 1 - 1)

    MsgBox MyDate

    End Sub

    HM DATEVALUE

    Private Sub Command1_Click()

    Dim MyDate

    MyDate = DateValue("February 12, 1969")

    MsgBox MyDate

    MyDate = DateValue("Feb 12, 1999")

    MsgBox MyDate

    MyDate = DateValue("25/12/2000")

    MsgBox MyDate

    MyDate = DateValue("28-12-2003")

    MsgBox MyDate

    MyDate = DateValue("28-Apr-2004") + 2

    MsgBox MyDate

    End Sub

    HM DAY

    Private Sub Command1_Click()

    Dim MyDate, MyDay

    MyDate = #12/25/2004#

    MyDay = Day(MyDate)

    MsgBox MyDay

  • 7/30/2019 VISUAL BASIC 6 - HM V V D

    5/215

    MyDate = #12/25/2004#

    MyDay = Day(MyDate)

    MsgBox MyDay

    End Sub

    HM DDB

    Private Sub Command1_Click()

    Dim Fmt, InitCost, SalvageVal

    Dim MonthLife, LifeTime

    Dim DepYear, Depr

    Const YRMOS = 12

    Fmt = "###,##0.00"

    InitCost = InputBox("Nhp tr gi ban u ca ti sn?")

    SalvageVal = InputBox("Nhp tr gi cui cng ca ti sn.")

    MonthLife = InputBox("Nhp tr gi thng s dng?")

    Do While MonthLife < YRMOS

    MsgBox " THi gian ca ti sn phi trn 1 nm."

    MonthLife = InputBox("Nhp tr gi thng s dng?")

    Loop

    LifeTime = MonthLife / YRMOS

    If LifeTime Int(MonthLife / YRMOS) Then

    LifeTime = Int(LifeTime + 1)

    End If

    DepYear = CInt(InputBox("Nhp s nm tnh khu hao."))

    Do While DepYear < 1 Or DepYear > LifeTime

    MsgBox "Bn phi nhp t nht 1 " & " nm nhng khng ln hn " & LifeTime

    DepYear = InputBox("Nhp s nm tnh khu hao.")

    Loop

    Depr = DDB(InitCost, SalvageVal, LifeTime, DepYear)

    MsgBox "Khu hao l " & DepYear & " is " & Format(Depr, Fmt) & "."

    End Sub

    HM DIR

    Private Sub Command1_Click()

    Dim MyFile, MyPath, MyName

    MyPath = "c:\"

    If MyName "." And MyName ".." Then

    If (GetAttr(MyPath & MyName) And vbDirectory) = vbDirectory Then

    Debug.Print MyName

    End If

  • 7/30/2019 VISUAL BASIC 6 - HM V V D

    6/215

    End If

    MyName = Dir

    Loop

    End Sub

    HM DOEVENTS

    Private Sub Command1_Click()

    Form2.Show

    Dim I, OpenForms

    For I = 1 To 150000

    If I Mod 1000 = 0 Then

    OpenForms = DoEvents

    End If

    Next I

    MsgBox OpenForms

    End Sub

    HM ENVIRON

    Private Sub Command1_Click()

    Dim EnvString, Indx, Msg, PathLen

    Indx = 1

    Do

    EnvString = Environ(Indx)

    If Left(EnvString, 5) = "PATH=" Then

    PathLen = Len(Environ("PATH"))

    Msg = "PATH entry = " & Indx & " and length = " & PathLen

    Exit Do

    Else

    Indx = Indx + 1

    End If

    Loop Until EnvString = ""

    If PathLen > 0 Then

    MsgBox Msg

    Else

    MsgBox "No PATH environment variable exists."

    End If

    End Sub

  • 7/30/2019 VISUAL BASIC 6 - HM V V D

    7/215

    HM EOF

    Private Sub Command1_Click()

    Dim InputData

    Open "C:\arrays.cs" For Input As #1

    Do While Not EOF(1)

    Line Input #1, InputData

    Debug.Print InputData

    Loop

    Close #1

    End Sub

    HM ERROR

    Private Sub Command1_Click()

    On Error Goto myErr

    Dim InputData

    Open "C:\arrays.cs" For Input As #1

    Do While Not EOF(1)

    Line Input #1, InputData

    Debug.Print InputData

    Loop

    Close #1

    Exit Sub

    myErr:

    Debug.Print Error(Err.Number)

    End Sub

    HM EXP

    Private Sub Command1_Click()

    Dim MyAngle, MyHSin

    MyAngle = 1.3

    MyHSin = (Exp(MyAngle) - Exp(-1 * MyAngle)) / 2

    MsgBox MyHSin

    End Sub

    HM FILEATTR

    Private Sub Command1_Click()

    On Error Resume Next

    Dim FileNum, Mode, Handle

    FileNum = 1

    Open "C:\arrays.cs" For Append As FileNum

    Mode = FileAttr(FileNum, 1)

  • 7/30/2019 VISUAL BASIC 6 - HM V V D

    8/215

    Handle = FileAttr(FileNum, 2)

    Close FileNum

    MsgBox Handle

    MsgBox Mode

    End Sub

    HM FILEDATETIME

    Private Sub Command1_Click()

    On Error Resume Next

    Dim MyStamp

    MyStamp = FileDateTime("C:\AUTOEXEC.BAT")

    MsgBox MyStamp

    End Sub

    HM FILELEN

    Private Sub Command1_Click()

    On Error Resume Next

    Dim MySize

    MySize = FileLen("C:\logo_RCV.gif")

    MsgBox MySize

    End Sub

    HM FORMAT

    Private Sub Command1_Click()

    On Error Resume Next

    Dim MyTime, MyDate, MyStr

    MyTime = #5:04:23 PM#

    MyDate = #1/27/1993#

    MyStr = Format(Time, "Long Time")

    MsgBox MyStr

    MyStr = Format(Date, "Long Date")

    MsgBox MyStr

    MyStr = Format(MyTime, "h:m:s")

    MsgBox MyStr

    MyStr = Format(MyTime, "hh:mm:ss AMPM")

    MsgBox MyStr

    MyStr = Format(MyDate, "dddd, mmm d yyyy")

    MsgBox MyStr

    MyStr = Format(23)

    MsgBox MyStr

    MyStr = Format(5459.4, "##,##0.00")

  • 7/30/2019 VISUAL BASIC 6 - HM V V D

    9/215

    MsgBox MyStr

    MyStr = Format(334.9, "###0.00")

    MsgBox MyStr

    MyStr = Format(5, "0.00%")

    MsgBox MyStr

    MyStr = Format("HELLO", "")

    MsgBox MyStr

    End Sub

    HM FORMATCURRENCY

    Private Sub Command1_Click()

    On Error Resume Next

    Dim cr, st

    cr = 100000.23

    st = FormatCurrency(cr, 2, vbTrue)

    MsgBox st

    cr = 0.23

    st = FormatCurrency(cr, 2, vbFalse)

    MsgBox st

    st = FormatCurrency(cr, 2, vbTrue)

    MsgBox st

    cr = -100000.23

    st = FormatCurrency(cr, 2, vbTrue, vbTrue)

    MsgBox st

    st = FormatCurrency(cr, 2, vbTrue, vbFalse)

    MsgBox st

    st = FormatCurrency(cr,2,vbTrue,vbFalse,vbTrue)

    MsgBox st

    st = FormatCurrency(cr,2,vbTrue,vbFalse,vbFalse)

    MsgBox st

    End Sub

    HM FORMATDATETIME

    Private Sub Command1_Click()

    On Error Resume Next

    Dim dstr, st

    dstr = Date

    st = FormatDateTime(dstr, vbGeneralDate)

  • 7/30/2019 VISUAL BASIC 6 - HM V V D

    10/215

    MsgBox st

    st = FormatDateTime(dstr, vbLongDate)

    MsgBox st

    st = FormatDateTime(dstr, vbLongTime)

    MsgBox st

    st = FormatDateTime(dstr, vbShortDate)

    MsgBox st

    st = FormatDateTime(dstr, vbShortTime)

    MsgBox st

    End Sub

    HM FORMATNUMBER

    Private Sub Command1_Click()

    On Error Resume Next

    Dim cr, st

    cr = 100000.23

    st = FormatNumber(cr, 2, vbTrue)

    MsgBox st

    cr = 0.23

    st = FormatNumber(cr, 2, vbFalse)

    MsgBox st

    st = FormatNumber(cr, 2, vbTrue)

    MsgBox st

    cr = -100000.23

    st = FormatNumber(cr, 2, vbTrue, vbTrue)

    MsgBox st

    st = FormatNumber(cr, 2, vbTrue, vbFalse)

    MsgBox st

    st = FormatNumber(cr, 2, vbTrue, vbFalse, vbTrue)

    MsgBox st

    st = FormatNumber(cr, 2, vbTrue, vbFalse, vbFalse)

    MsgBox st

    End Sub

    HM FORMATPERCENT

    Private Sub Command1_Click()

    On Error Resume Next

    Dim cr, st

    cr = 0.215

    st = FormatPercent (cr, 2, vbTrue)

  • 7/30/2019 VISUAL BASIC 6 - HM V V D

    11/215

    MsgBox st

    cr = 0.035

    st = FormatPercent (cr, 2, vbFalse)

    MsgBox st

    st = FormatPercent (cr, 2, vbTrue)

    MsgBox st

    cr = -0.3

    st = FormatNumber(cr, 2, vbTrue, vbTrue)

    MsgBox st

    st = FormatPercent (cr, 2, vbTrue, vbFalse)

    MsgBox st

    st = FormatPercent (cr, 2, vbTrue, vbFalse, vbTrue)

    MsgBox st

    st = FormatPercent (cr, 2, vbTrue, vbFalse, vbFalse)

    MsgBox st

    End Sub

    HM FREEFILE

    Private Sub Command1_Click()

    On Error Resume Next

    Dim MyIndex, FileNumber

    For MyIndex = 1 To 5

    FileNumber = FreeFile

    Open "TEST" & MyIndex For Output As #FileNumber

    Write #FileNumber, "This is a sample."

    Close #FileNumber

    Next MyIndex

    End Sub

    HM FV

    Private Sub Command1_Click()

    On Error Resume Next

    Dim Fmt, Payment, APR, TotPmts

    Dim PayType, PVal, FVal

    Const ENDPERIOD = 0, BEGINPERIOD = 1

    Fmt = "###,###,##0.00"

    Payment = InputBox("Mi thng gi bao nhiu ?)

    APR = InputBox("Li sut tin gi.")

    If APR > 1 Then APR = APR / 100

    TotPmts = InputBox("S thng gi")

  • 7/30/2019 VISUAL BASIC 6 - HM V V D

    12/215

    PayType = MsgBox("G tin vo cui thng?", vbYesNo)

    If PayType = vbNo Then

    PayType = BEGINPERIOD

    Else

    PayType = ENDPERIOD

    End If

    PVal = InputBox("S tin hin hnh")

    FVal = FV(APR / 12, TotPmts, Payment, PVal, PayType)

    MsgBox "Tin s nhn c trong tng lai " & Format(FVal, Fmt) & "."

    End Sub

    HM GETALLSETTINGS

    Private Sub Command1_Click()

    On Error Resume Next

    Dim MySet As Variant

    Dim intSettings As Integer

    SaveSetting appname:="MyApp", section:="Startup", Key:="Top", setting:=75

    SaveSetting "MyApp", "Startup", "Left", 50

    MySet = GetAllSettings(appname:="MyApp", section:="Startup")

    For intSettings = LBound(MySet, 1) To UBound(MySet, 1)

    Debug.Print MySet (intSettings, 0), MySettings(intSettings, 1)

    Next intSettings

    DeleteSetting "MyApp", "Startup"

    End Sub

    HM GETATTR

    Private Sub Command1_Click()

    On Error Resume Next

    Dim MyAttr

    MyAttr = GetAttr("TESTFILE")

    Debug.Print MyAttr And vbHidden

    MyAttr = GetAttr("TESTFILE")

    Debug.Print MyAttr And (vbHidden + vbReadOnly)

    MyAttr = GetAttr("MYDIR")

    Debug.Print MyAttr

    End Sub

  • 7/30/2019 VISUAL BASIC 6 - HM V V D

    13/215

    HM GETAUTOSERVERSETTINGS

    Private Sub Command1_Click()

    On Error Resume Next

    Dim oRegClass As New RegClass

    Dim vRC As Variant

    vRC = oRegClass.GetAutoServerSettings("HelloProj.HelloClass")

    If Not (IsEmpty(vRC)) Then

    If vRC(1) Then

    MsgBox "Hello is registered remotely on a " & "server named: " & vRC(1)

    Else

    MsgBox "Hello is registered locally."

    End If

    End If

    End Sub

    HM GETSETTING

    Private Sub Command1_Click()

    On Error Resume Next

    SaveSetting appname:="MyApp", section:="Startup", Key:="Top", setting:=75

    SaveSetting "MyApp", "Startup", "Left", 50

    Debug.Print GetSetting(appname := "MyApp", section := "Startup", _ key := "Left", default := "25")

    DeleteSetting "MyApp", "Startup"

    End Sub

    HM HEX

    Private Sub Command1_Click()

    On Error Resume Next

    Dim MyHex

    MyHex = Hex(5)

    MyHex = Hex(10)

    MyHex = Hex(459)

    End Sub

    HM HOUR

    Private Sub Command1_Click()

    On Error Resume Next

    Dim MyTime, MyHour

    MyTime = #4:35:17 PM#

    MyHour = Hour(MyTime)

    End Sub

  • 7/30/2019 VISUAL BASIC 6 - HM V V D

    14/215

    HM IIF

    Private Sub Command1_Click()

    result = CheckIt(5000)

    End Function

    Function CheckIt(TestMe As Integer)

    CheckIt = IIf(TestMe > 1000, "Large", "Small")

    End Function

    HM INPUT

    Private Sub Command1_Click()

    Dim MyChar

    Open "TESTFILE" For Input As #1

    Do While Not EOF(1)

    MyChar = Input(1, #1)

    Debug.Print MyChar

    Loop

    Close #1

    End Function

    HM INPUTBOX

    Private Sub Command1_Click()

    Dim Message, Title, Default, MyValue

    Message = "Enter a value between 1 and 3"

    Title = "InputBox Demo"

    Default = "1"

    MyValue = InputBox(Message, Title, Default)

    MyValue = InputBox(Message, Title, , , , "DEMO.HLP", 10)

    MyValue = InputBox(Message, Title, Default, 100, 100)

    End Sub

    HM INSTR

    Private Sub Command1_Click()

    Dim SearchString, SearchChar, MyPos

    SearchString = "http://www.My.Opera.Com/ManMan89"

    SearchChar = "h"

    MyPos = InStr(13, SearchString, SearchChar, 1)

    MyPos = InStr(2, SearchString, SearchChar, 0)

    MyPos = InStr(SearchString, SearchChar)

    MyPos = InStr(1, SearchString, "W")

    End Sub

  • 7/30/2019 VISUAL BASIC 6 - HM V V D

    15/215

    HM INSTRREV

    Private Sub Command1_Click()

    Dim StrCheck, StrMatch, MyPos

    StrCheck = "phamhuukhang"

    StrMatch = "h"

    MyPos = InStrRev(StrCheck, StrMatch, 4, 1)

    MsgBox MyPos

    MyPos = InStrRev(StrCheck, StrMatch, 6, 1)

    MsgBox MyPos

    MyPos = InStrRev(StrCheck, StrMatch, 10, 1)

    MsgBox MyPos

    End Sub

    HM INT V FIX

    Private Sub Command1_Click()

    Dim MyNumbe

    MyNumber = Int(99.8)

    MyNumber = Fix(99.2)

    MyNumber = Int(-99.8)

    MyNumber = Fix(-99.8)

    MyNumber = Int(-99.2)

    MyNumber = Fix(-99.2)

    End Sub

    HM IPMT

    Private Sub Command1_Click()

    On Error Resume Next

    Dim FVal, Fmt, PVal, APR, TotPmts, PayType

    Dim Period, IntPmt, TotInt, Msg

    Const ENDPERIOD = 0, BEGINPERIOD = 1

    FVal = 0

    Fmt = "###,###,##0.00"

    PVal = InputBox( "S tin bn vay?")

    APR = InputBox("T gi tng k tr?")

    If APR > 1 Then APR = APR / 100

    TotPmts = InputBox("S thng cn tr?")

    PayType = MsgBox("Bn mun tr cui thng?", vbYesNo)

    If PayType = vbNo Then

    PayType = BEGINPERIOD

    Else

  • 7/30/2019 VISUAL BASIC 6 - HM V V D

    16/215

    PayType = ENDPERIOD

    End If

    For Period = 1 To TotPmts

    IntPmt = IPmt(APR / 12, Period, TotPmts, PVal, FVal, PayType)

    TotInt = TotInt + IntPmt

    Next Period

    Msg = Format(TotInt, Fmt)

    Msg = Msg & " in interest for this loan."

    MsgBox Msg

    End Sub

    HM ISARRAY

    Private Sub Command1_Click()

    On Error Resume Next

    Dim MyArray(1 To 5) As Integer

    DimYourArray, MyCheck

    YourArray = Array(1, 2, 3)

    MyCheck = IsArray(MyArray)

    MyCheck = IsArray(YourArray)

    End Sub

    HM ISDATE

    Private Sub Command1_Click()

    On Error Resume Next

    Dim MyDate, YourDate, NoDate, MyCheck

    MyDate = "February 12, 1969"

    YourDate = #2/12/1969#

    NoDate = "Hello"

    MyCheck = IsDate(MyDate)

    MyCheck = IsDate(YourDate)

    MyCheck = IsDate(NoDate)

    End Sub

    HM ISEMPTY

    Private Sub Command1_Click()

    On Error Resume Next

    Dim MyVar, MyCheck

    MyCheck = IsEmpty(MyVar)

    MyVar = Null

    MyCheck = IsEmpty(MyVar)

    MyVar = Empty

  • 7/30/2019 VISUAL BASIC 6 - HM V V D

    17/215

    MyCheck = IsEmpty(MyVar)

    End Sub

    HM ISERROR

    Private Sub Command1_Click()

    On Error Resume Next

    Dim ReturnVal, MyCheck

    UserFunction = CVErr(32767)

    ReturnVal = UserFunction

    MyCheck = IsError(ReturnVal)

    End Sub

    HM ISNULL

    Private Sub Command1_Click()

    On Error Resume Next

    Dim MyVar, MyCheck

    MyCheck = IsNull(MyVar)

    MyVar = ""

    MyCheck = IsNull(MyVar)

    MyVar = Null

    MyCheck = IsNull(MyVar)

    End Sub

    HM ISNUMERIC

    Private Sub Command1_Click()

    On Error Resume Next

    Dim MyVar, MyCheck

    MyVar = "53"

    MyCheck = IsNumeric(MyVar)

    MyVar = "459.95"

    MyCheck = IsNumeric(MyVar)

    MyVar = "45 Help"

    MyCheck = IsNumeric(MyVar)

    End Sub

    HM ISOBJECT

    Private Sub Command1_Click()

    Dim MyInt As Integer, YourObject, MyCheck

    Dim MyObject As Object

    Set YourObject = MyObject

    MyCheck = IsObject(YourObject)

    MyCheck = IsObject(MyInt)

  • 7/30/2019 VISUAL BASIC 6 - HM V V D

    18/215

    End Sub

    HM JOIN

    Private Sub Command1_Click()

    Dim MyWeek, MyDay

    MyWeek = Array("Mon","Tue","Wed","Thu", "Fri", "Sat", "Sun")

    MyDay = Join(MyWeek)

    MsgBox MyDay

    MyDay = Join(MyWeek, ",")

    MsgBox MyDay

    End Sub

    HM LBOUND

    Private Sub Command1_Click()

    Dim MyWeek, MinElement

    MyWeek = Array("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun")

    MinElement = LBound(MyWeek)

    MsgBox MinElement

    Dim A(1 To 100, 0 To 3, -3 To 4)

    MinElement = LBound(A)

    MsgBox MinElement

    MinElement = LBound(A, 1)

    MsgBox MinElement

    MinElement = LBound(A, 2)

    MsgBox MinElement

    MinElement = LBound(A, 3)

    MsgBox MinElement

    End Sub

    HM LCASE

    Private Sub Command1_Click()

    Dim UpperCase, LowerCase

    UpperCase = "Hello My.Opera.Com/ManMan89"

    LowerCase = LCase(UpperCase)

    MsgBox LowerCase

    End Sub

  • 7/30/2019 VISUAL BASIC 6 - HM V V D

    19/215

    HM LEFT

    Private Sub Command1_Click()

    Dim AnyString, MyStr

    AnyString = "Hello World"

    MyStr = Left(AnyString, 1)

    MsgBox MyStr

    MyStr = Left(AnyString, 7)

    MsgBox MyStr

    MyStr = Left(AnyString, 20)

    MsgBox MyStr

    End Sub

    HM LEN

    Private Sub Command1_Click()

    Type CustomerRecord

    ID As Integer

    Name As String * 10

    Address As String * 30

    End Type

    Dim Customer As CustomerRecord

    Dim MyInt As Integer, MyCur As Currency

    Dim MyString, MyLen

    MyString = "Hello World"

    MyLen = Len(MyInt)

    MyLen = Len(Customer)

    MyLen = Len(MyString)

    MyLen = Len(MyCur)

    End Sub

    HM LOADPICTURE

    Private Sub Command1_Click()

    Dim Msg As String

    On Error Resume Next

    Height = 3990

    Width = 4890

    Picture1.Picture = LoadPicture("PAPER.CUR", vbLPCustom, vbLPColor, 32, 32)

    If Err Then

    MsgBox Msg

    Exit Sub

    End If

  • 7/30/2019 VISUAL BASIC 6 - HM V V D

    20/215

    Msg = "Choose OK to clear the bitmap from the picturebox."

    MsgBox Msg

    Picture1.Picture = LoadPicture()

    End Sub

    HM LOC

    Private Sub Command1_Click()

    Dim MyLocation, MyLine

    Open "TESTFILE" For Binary As #1

    Do While MyLocation < LOF(1)

    MyLine = MyLine & Input(1, #1)

    MyLocation = Loc(1)

    Debug.Print MyLine; Tab; MyLocation

    Loop

    Close #1

    End Sub

    HM LOF

    Private Sub Command1_Click()

    Dim FileLength

    Open "TESTFILE" For Input As #1

    FileLength = LOF(1)

    Close #1

    End Sub

    HM LOG

    Private Sub Command1_Click()

    Dim MyAngle, MyLog

    MyAngle = 1.3

    MyLog = Log(MyAngle + Sqr(MyAngle * MyAngle + 1))

    End Sub

    HM LTRIM, RTRIM, TRIM

    Private Sub Command1_Click()

    Dim MyString, TrimString

    MyString = ""

    TrimString = LTrim(MyString)

    TrimString = RTrim(MyString)

    TrimString = LTrim(RTrim(MyString))

    TrimString = Trim(MyString)

    End Sub

  • 7/30/2019 VISUAL BASIC 6 - HM V V D

    21/215

    HM MID

    Private Sub Command1_Click()

    Dim AnyString, MyStr

    AnyString = "Hello World"

    MyStr = Mid(AnyString, 7,1)

    MsgBox MyStr

    MyStr = Mid(AnyString, 7,6)

    MsgBox MyStr

    MyStr = Mid(AnyString, 7)

    MsgBox MyStr

    End Sub

    HM MINUTE

    Private Sub Command1_Click()

    On Error Resume Next

    Dim MyTime, MyMinute

    MyTime = #4:35:17 PM#

    MyMinute = Minute(MyTime)

    End Sub

    HM MONTH

    Private Sub Command1_Click()

    Dim MyDate, MyMonth

    MyDate = #11/25/2004#

    MyMonth = Month(MyDate)

    MsgBox MyMonth

    MyDate = #12/25/2004#

    MyMonth = Month(MyDate)

    MsgBox MyMonth

    End Sub

    HM MONTHNAME

    Private Sub Command1_Click()

    Dim MyDate, MyMonth

    MyDate = #11/25/2004#

    MyMonth = MonthName(Month(MyDate)) : MsgBox MyMonth

    MyDate = #12/25/2004#

    MyMonth = MonthName(Month(MyDate), True)

    MsgBox MyMonth

    End Sub

  • 7/30/2019 VISUAL BASIC 6 - HM V V D

    22/215

    HM MSGBOX

    Private Sub Command1_Click()

    Dim Msg, Style, Title, Help, Ctxt, Response, MyString

    Msg = "Do you want to continue ?"

    Style = vbYesNo + vbCritical + vbDefaultButton2

    Title = "MsgBox Demonstration"

    Help = "DEMO.HLP"

    Ctxt = 1000

    Response = MsgBox(Msg, Style, Title, Help, Ctxt)

    If Response = vbYes Then

    MyString = "Yes"

    Else

    MyString = "No"

    End If

    End Sub

    HM NOW

    Private Sub Command1_Click()

    Dim myCurrTime

    myCurrTime = Now

    MsgBox myCurrTime

    End Sub

    HM NPER

    Dim FVal, PVal, APR, Payment

    Dim PayType, TotPmts

    Const ENDPERIOD = 0, BEGINPERIOD = 1

    FVal = 0

    PVal = InputBox("S tin bn cn vay?")

    APR = InputBox("T gi?")

    If APR > 1 Then APR = APR / 100

    Payment = InputBox("Tr bao nhiu trong mt thng?")

    PayType = MsgBox("Tr vo cui thng?", vbYesNo)

    If PayType = vbNo Then

    PayType = BEGINPERIOD

    Else

    PayType = ENDPERIOD

    End If

    TotPmts = NPer(APR / 12, -Payment, PVal, FVal, PayType)

    If Int(TotPmts) TotPmts Then

  • 7/30/2019 VISUAL BASIC 6 - HM V V D

    23/215

    TotPmts = Int(TotPmts) + 1

    End If

    MsgBox "Bn phi " & TotPmts & " thng."

    HM NPV

    Private Sub Command1_Click()

    Dim Fmt, Guess, RetRate, NetPVal, Msg

    Static Values(5) As Double

    Fmt = "###,##0.00"

    RetRate = 0.0625

    Values(0) = -70000

    Values(1) = 22000

    Values(2) = 25000

    Values(3) = 28000

    Values(4) = 31000

    NetPVal = NPV(RetRate, Values())

    Msg = "NPV l "

    Msg = Msg & Format(NetPVal, Fmt) & "."

    MsgBox Msg

    End Sub

    HM OCT

    Private Sub Command1_Click()

    Dim MyOct

    MyOct = Oct(4)

    MyOct = Oct(8)

    MyOct = Oct(459)

    End Sub

    HM PMT

    Private Sub Command1_Click()

    Dim Fmt, FVal, PVal, APR, TotPmts

    DimPayType, Payment

    Const ENDPERIOD = 0, BEGINPERIOD = 1

    Fmt = "###,###,##0.00"

    FVal = 0

    PVal = InputBox("S tin vay?")

    APR = InputBox("Li sut?")

    If APR > 1 Then APR = APR / 100

    TotPmts = InputBox("Tr bao nhiu thng?")

    PayType = MsgBox("Tr vo cui thng", vbYesNo)

  • 7/30/2019 VISUAL BASIC 6 - HM V V D

    24/215

    If PayType = vbNo Then

    PayType = BEGINPERIOD

    Else

    PayType = ENDPERIOD

    End If

    Payment = Pmt(APR / 12, TotPmts, -PVal, FVal, PayType)

    MsgBox "Bn phi tr " & Format(Payment, Fmt) & " cho mi thng."

    End Sub

    HM PPMT

    Private Sub Command1_Click()

    Dim NL, TB, Fmt, FVal, PVal, APR

    Dim TotPmts, PayType, Payment

    Dim Msg, MakeChart, Period, P, I

    Const ENDPERIOD = 0, BEGINPERIOD = 1

    NL = Chr(13) & Chr(10)

    TB = Chr(9)

    Fmt = "###,###,##0.00"

    FVal = 0

    PVal = InputBox("S tin vay?")

    APR = InputBox("Li sut?")

    If APR > 1 Then APR = APR / 100

    TotPmts = InputBox("Tr bao nhiu thng?")

    PayType = MsgBox("Tr vo cui thng?", vbYesNo)

    If PayType = vbNo Then

    PayType = BEGINPERIOD

    Else

    PayType = ENDPERIOD

    End If

    Payment = Abs(-Pmt(APR / 12, TotPmts, PVal, FVal, PayType))

    Msg = "Tr hng thng " & Format(Payment, Fmt) & ". "

    Msg = Msg & "Tnh s tin gc?"

    MakeChart = MsgBox(Msg, vbYesNo)

    If MakeChart vbNo Then

    If TotPmts > 12 Then

    MsgBox "Only first year will be shown."

    Msg = "MonthPaymentPrincipalInterest" & NL

    For Period = 1 To TotPmts

    If Period > 12 Then Exit For

  • 7/30/2019 VISUAL BASIC 6 - HM V V D

    25/215

    P = PPmt(APR / 12, Period, TotPmts, -PVal, FVal, PayType)

    P = (Int((P + 0.005) * 100) / 100)

    I = Payment - P

    I = (Int((I + 0.005) * 100) / 100)

    Msg = Msg & Period & TB & Format(Payment, Fmt)

    Msg = Msg & TB & Format(P, Fmt) & TB & Format(I, Fmt) & NL

    Next Period

    MsgBox Msg

    End If

    End Sub

    HM PV

    Private Sub Command1_Click()

    Dim Fmt, APR, TotPmts, YrIncome

    DimFVal, PayType, PVal

    Const ENDPERIOD = 0, BEGINPERIOD = 1

    Fmt = "###,##0.00"

    APR = 0.1

    TotPmts = 12

    YrIncome = 50000

    FVal = 1000000

    PayType = BEGINPERIOD

    PVal = PV(APR, TotPmts, -YrIncome, FVal, PayType)

    MsgBox "Gi tr tng lai l " & Format(PVal, Fmt) & "."

    End Sub

    HM QBCOLOR

    Sub ChangeBackColor(ColorCode As Integer, MyForm As Form)

    MyForm.BackColor = QBColor(ColorCode)

    End Sub

    HM RATE

    Private Sub Command1_Click()

    Dim Fmt, FVal, Guess, PVal

    Dim Payment, TotPmts, PayType, APR

    Const ENDPERIOD = 0, BEGINPERIOD = 1

    FVal = 0

    Guess = 0.1

    PVal = InputBox("Bn vay bao nhiu?")

    Payment = InputBox("Tr bao nhiu thng?")

    TotPmts = InputBox("Moi thang tra bao nhieu?")

  • 7/30/2019 VISUAL BASIC 6 - HM V V D

    26/215

    PayType = MsgBox("Tr vo cui thng?", vbYesNo)

    If PayType = vbNo Then

    PayType = BEGINPERIOD

    Else

    PayType = ENDPERIOD

    End If

    APR = (Rate(TotPmts, -Payment, PVal, FVal, PayType, Guess) * 12) * 100

    MsgBox "Li sut l " & Format(CInt(APR), Fmt) & " percent."

    End Sub

    HM REPLACE

    Private Sub Command1_Click()

    Dim strSQL As String

    strSQL = "Insert into Shippers"

    End Sub

    HM RGB

    Private Sub Command1_Click()

    Dim RED, I, RGBValue, MyObject

    RED = RGB(255, 0, 0)

    I = 75

    RGBValue = RGB(I, 64 + I, 128 + I)

    MyObject.Color = RGB(255, 0, 0)

    End Sub

    HM RIGHT

    Private Sub Command1_Click()

    Dim AnyString, MyStr

    AnyString = "Hello World"

    MyStr = Right(AnyString, 1)

    MsgBox MyStr

    MyStr = Right(AnyString, 7)

    MsgBox MyStr

    MyStr = Right(AnyString, 20)

    MsgBox MyStr

    End Sub

    HM RND

    Private Sub Command1_Click()

    For I = 1 To 100

    Randomize (I)

    Debug.Print Rnd(I)

  • 7/30/2019 VISUAL BASIC 6 - HM V V D

    27/215

    Next I

    End Sub

    HM ROUND

    Private Sub Command1_Click()

    Dim Amount

    Amount = 15.1464

    MsgBox Round(Amount, 3)

    MsgBox Round(Amount, 2)

    End Sub

    HM SECOND

    Private Sub Command1_Click()

    On Error Resume Next

    Dim MyTime, MyMinute

    MyTime = #4:35:17 PM#

    MyMinute = Second(MyTime)

    End Sub

    HM SEEK

    Private Sub Command1_Click()

    Dim MyChar

    Open "D:\Csharp\arrays.cs" For Input As #1

    Do While Not EOF(1)

    MyChar = Input(1, #1)

    Debug.Print Seesk(1)

    Loop

    Close #1

    End Sub

    HM SGN

    Private Sub Command1_Click()

    Dim MyVar1, MyVar2, MyVar3, MySign

    MyVar1 = 12: MyVar2 = -2.4: MyVar3 = 0

    MySign = Sgn(MyVar1)

    MySign = Sgn(MyVar2)

    MySign = Sgn(MyVar3)

    End Sub

    HM SHELL

    Private Sub Command1_Click()

    Dim RetVal

    RetVal = Shell("C:\WINDOWS\CALC.EXE", 1)

  • 7/30/2019 VISUAL BASIC 6 - HM V V D

    28/215

    End Sub

    HM SIN

    Private Sub Command1_Click()

    Dim MyAngle, MySecant

    MyAngle = 1.3

    MySecant = 1 / Sin(MyAngle)

    MsgBox MySecant

    End Sub

    HM SLN

    Private Sub Command1_Click()

    Dim Fmt, InitCost, SalvageVal

    Dim MonthLife, LifeTime, PDepr

    Const YEARMONTHS = 12

    Fmt = "###,##0.00"

    InitCost=InputBox("Gi tr ban u ca TS?")

    SalvageVal=InputBox("Gi tr cn li ca TS?")

    MonthLife = InputBox("S thng tnh khu hao?")

    Do While MonthLife < YEARMONTHS

    MsgBox "S thng ln hn v bng 1 nm."

    MonthLife= InputBox("S thng tnh khu hao?")

    Loop

    LifeTime = MonthLife / YEARMONTHS

    If LifeTime Int(MonthLife / YEARMONTHS) Then

    LifeTime = Int(LifeTime + 1)

    End If

    PDepr = SLN(InitCost, SalvageVal, LifeTime)

    MsgBox "Gi tr khu hao l " & Format(PDepr, Fmt) & " per year."

    End Sub

    HM SPACE

    Private Sub Command1_Click()

    Dim MyString

    MyString = Space(10)

    MyString = "Hello" & Space(10) & "World"

    End Sub

    HM SPLIT

    Private Sub Command1_Click()

    Dim myString As String

    Dim MyArr() As String

  • 7/30/2019 VISUAL BASIC 6 - HM V V D

    29/215

    Dim MyArrs() As String

    Dim i As Integer

    myString = "Welcome to Saigon"

    MyArr = Split(myString)

    For i = 0 To UBound(MyArr)

    Debug.Print MyArr(i)

    Next

    myString = "Xin,chao,cac,ban"

    MyArrs = Split(myString, ",", 1)

    For i = 0 To UBound(MyArrs)

    Debug.Print MyArrs(i)

    Next

    End Sub

    HM SQR

    Private Sub Command1_Click()

    Dim MySqr

    MySqr = Sqr(4)

    MySqr = Sqr(23)

    MySqr = Sqr(0)

    MySqr = Sqr(-4)

    End Sub

    HM STR

    Private Sub Command1_Click()

    Dim MyString

    MyString = Str(459)

    MyString = Str(-459.65)

    MyString = Str(459.001)

    End Sub

    HM STRCONV

    Private Sub Command1_Click()

    Dim i As Long

    Dim x() As Byte

    x = StrConv("ABCDEFG", vbFromUnicode)

    For i = 0 To UBound(x)

    Debug.Print x(i)

    Next

    End Sub

  • 7/30/2019 VISUAL BASIC 6 - HM V V D

    30/215

    HM STRING

    Private Sub Command1_Click()

    Dim MyString As String

    MyString = String(5, "*")

    Debug.Print MyString

    MyString = String(5, 42)

    Debug.Print MyString

    MyString = String(10, "ABC")

    Debug.Print MyString

    End Sub

    Private Sub Command1_Click()

    Dim MyString As String

    MyString = "ABC"

    MyString = StrReverse(MyString)

    Debug.Print MyString

    End Sub

    HM SYD

    Private Sub Command1_Click()

    Dim Fmt, InitCost, SalvageVal

    Dim MonthLife, LifeTime, DepYear, PDepr

    Const YEARMONTHS = 12

    Fmt = "###,##0.00"

    InitCost= InputBox("Gi tr ban u ca ti sn?")

    SalvageVal = InputBox (Gi tr cn li ca ti sn sau khi

    kt thc thi k khu hao?")

    MonthLife = InputBox("S thng tnh khu hao?")

    Do While MonthLife < YEARMONTHS

    MsgBox "i sng ca ti sn l 1 nm hay ln

    hn."

    MonthLife = InputBox("S thng tnh khu hao?")

    Loop

    LifeTime = MonthLife / YEARMONTHS

    If LifeTime Int(MonthLife / YEARMONTHS) Then

    LifeTime = Int(LifeTime + 1)

    End If

    DepYear =CInt(InputBox("Gi tr khu hao ca nm?"))

    Do While DepYear < 1 Or DepYear > LifeTime

    MsgBox i sng ca ti sn l 1 nm hay ln hn " & LifeTime

  • 7/30/2019 VISUAL BASIC 6 - HM V V D

    31/215

    DepYear = CInt(InputBox("Gi tr khu hao ca nm?"))

    Loop

    PDepr =SYD(InitCost, SalvageVal, LifeTime, DepYear)

    MsgBox "Khu hao nm " & DepYear & " l " & Format(PDepr, Fmt) & "."

    End Sub

    HM TAB

    Private Sub Command1_Click()

    Open "C:\TESTFILE.txt" For Output As #1

    Print #1, "Hello"; Tab(20); "World."

    Print #1, "Hello"; Tab; "World"

    Close #1

    End Sub

    HM TAN

    Private Sub Command1_Click()

    Dim MyAngle, MyCotangent

    MyAngle = 1.3

    MyCotangent = 1 / Tan(MyAngle)

    End Sub

    HM TIME

    Private Sub Command1_Click()

    Dim MyTime

    MyTime = Time

    End Sub

    HM TIMESERIAL

    Private Sub Command1_Click()

    Dim MyTime

    MyTime = TimeSerial(16, 35, 17)

    Debug.Print MyTime

    MyTime = TimeSerial(19, -35, -17)

    Debug.Print MyTime

    End Sub

    HM TIMEVALUE

    Private Sub Command1_Click()

    Dim MyTime

    MyTime = TimeValue("4:35:17 PM")

    Debug.Print MyTime

    MyTime = TimeValue("4:35:17 AM")

    Debug.Print MyTime

  • 7/30/2019 VISUAL BASIC 6 - HM V V D

    32/215

    End Sub

    HM TYPENAME

    Private Sub Command1_Click()

    Dim NullVar, MyType, StrVar As String

    Dim IntVar As Integer, CurVar As Currency

    Dim ArrayVar(1 To 5) As Integer

    NullVar = Null

    MyType = TypeName(StrVar)

    MyType = TypeName(IntVar)

    MyType = TypeName(CurVar)

    MyType = TypeName(NullVar)

    MyType = TypeName(ArrayVar)

    End Sub

    HM UBOUND

    Private Sub Command1_Click()

    Dim Upper

    Dim MyArray(1 To 10, 5 To 15, 10 To 20)

    Dim AnyArray(10)

    Upper = UBound(MyArray, 1)

    Upper = UBound(MyArray, 3)

    Upper = UBound(AnyArray)

    End Sub

    HM UCASE

    Private Sub Command1_Click()

    Dim Upper

    Dim MyString As String

    MyString =Hello World

    Upper = UCase(MyString)

    End Sub

    HM VAL

    Private Sub Command1_Click()

    Dim MyValue

    MyValue = Val("2457")+1

    Debug.Print MyValue

    MyValue = Val(" 2 45 7")

    Debug.Print MyValue

    MyValue = Val("24 and 57")

    Debug.Print MyValue

  • 7/30/2019 VISUAL BASIC 6 - HM V V D

    33/215

    MyValue = Val("A 24 and 57")

    Debug.Print MyValue

    MyValue = Val("")

    Debug.Print MyValue

    End Sub

    HM WEEKDAY

    Private Sub Command1_Click()

    Dim MyDate, MyWeekDay

    MyDate = #2/12/1969#

    MyWeekDay = Weekday(MyDate)

    Debug.Print MyWeekDay

    MyWeekDay = Weekday(Date)

    Debug.Print MyWeekDay

    End Sub

    HM WEEKDAYNAME

    Private Sub Command1_Click()

    Dim MyDate, MyWeekDay

    MyWeekDay = 4

    MyDate = WeekdayName(MyWeekDay)

    Debug.Print MyDate

    MyDate = WeekdayName(6, True)

    Debug.Print MyDate

    End Sub

    HM YEAR

    Private Sub Command1_Click()

    Dim MyDate, MyYear

    MyDate = #2/12/2003#

    MyYear = Year(MyDate)

    Debug.Print MyDate

    End Sub

    Exam CHAPTER 2

    KHAI BAO APPACTIVATEPrivate Sub Command1_Click()

    Dim MyAppID, ReturnValue

    AppActivate "Microsoft Word"

    MyAppID = Shell("C:\WORD\WINWORD.EXE", 1)

    AppActivate MyAppID

    ReturnValue = Shell("c:\EXCEL\EXCEL.EXE", 1)

  • 7/30/2019 VISUAL BASIC 6 - HM V V D

    34/215

    AppActivate ReturnValue

    End Sub

    KHAI BAO BEEP

    Private Sub Command1_Click()

    Dim Str As String

    Str = "Welcome to My.Opera.Com/ManMan89"

    Beep

    End Sub

    KHAI BAO CALL

    Private Sub Command1_Click()

    Dim str As String

    str = " My.Opera.Com/ManMan89"

    ABC()

    Call ABCD(str)

    Dim i As Integer

    I=XYZ

    End Sub

    Sub ABC()

    MsgBox "Welcome to My.Opera.Com/ManMan89"

    End Sub

    Sub ABCD(ByVal strname As String)

    MsgBox " Welcome to " & strname

    End Sub

    Function XYZ() As Integer

    XYZ = 10

    End Function

    KHAI BAO CHDIR

    Private Sub Command1_Click()

    ChDir "MYDIR"

    ChDir "D:\WINDOWS\SYSTEM"

    End Sub

    KHAI BAO CHDRIVE

    Private Sub Command1_Click()

    ChDrive ""

    ChDrive "D:\"

    End Sub

  • 7/30/2019 VISUAL BASIC 6 - HM V V D

    35/215

    KHAI BAO CLOSE

    Private Sub Command1_Click()

    Dim I, FileName

    For I = 1 To 3

    FileName = "TEST" & I

    Open FileName For Output As #I

    Print #I, "This is a test."

    Next I

    Close

    End Sub

    Function getDBInfo() As String

    On Error GoTo err

    Dim f As Integer

    Dim strWord As String

    f = FreeFile

    Open App.Path & "\config.ini" For Input As f

    Do While Not EOF(f)

    Line Input #f, strWord

    Loop

    Close f

    getDBInfo = strWord

    Exit Function

    err:

    getDBInfo = Error

    End Function

    KHAI BAO CONST

    Const MyVar = 459

    Public Const MyString = "HELP"

    Private Const MyInt As Integer = 5

    Const MyStr = "Hello", MyDouble As Double = 3.4567

    Private Const myBoolean As Boolean = True

    Private Sub Command1_Click()

    Debug.Print MyVar

    Debug.Print MyInt

    Debug.Print MyStr

    Debug.Print MyDouble

    Debug.Print MyString

    End Sub

  • 7/30/2019 VISUAL BASIC 6 - HM V V D

    36/215

    KHAI BAO DECLARE

    Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As

    Long) As Long

    Private Sub Command1_Click()

    Dim sbuf As String, nl As Integer

    Dim Uname As String

    sbuf = String$(10, 0)

    nl = GetUserName(sbuf, 10)

    Uname = UCase(sbuf)

    If Uname "" Then

    MsgBox ("Username: " & Uname)

    End If

    End Sub

    KHAI BAO DELETESETTING

    Private Sub Command1_Click()

    SaveSetting appname:="My.Opera.Com/ManMan89", section:="Startup", Key:="Top", setting:=75

    SaveSetting "My.Opera.Com/ManMan89", "Startup", "Left", 50

    DeleteSetting "My.Opera.Com/ManMan89", "Startup"

    End Sub

    KHAI BAO DIM

    Private Sub Command1_Click()

    Dim AnyValue, MyValue

    Dim Number As Integer

    Dim AnotherVar, Choice As Boolean

    Dim DayArray(50)

    Dim Matrix(3, 4) As Integer

    Dim MyMatrix(1 To 5, 4 To 9, 3 To 5) As Double

    Dim BirthDay(1 To 10) As Date

    Dim MyArray()

    End Sub

    KHAI BAO DO..LOOP

    Private Sub Command1_Click()

    Dim Check, Counter

    Check = True: Counter = 0

    Do

    Do While Counter < 20

    Counter = Counter + 1

    Debug.Print Counter

  • 7/30/2019 VISUAL BASIC 6 - HM V V D

    37/215

    If Counter = 10 Then

    Check = False

    Exit Do

    End If

    Loop

    Loop Until Check = False

    End Sub

    KHAI BAO END

    Private Sub Command1_Click()

    Dim Password, Pword

    Password = "123456"

    Pword = InputBox("Type in your password")

    If Pword Password Then

    MsgBox "Sorry, incorrect password"

    End

    End If

    End Sub

    KHAI BAO ERASE

    Private Sub Command1_Click()

    Dim NumArray(10) As Integer

    Dim StrVarArray(10) As String

    Dim StrFixArray(10) As String * 10

    Dim VarArray(10) As Variant

    Dim DynamicArray() As Integer

    ReDim DynamicArray(10)

    Erase NumArray

    Erase StrVarArray

    Erase StrFixArray

    Erase VarArray

    Erase DynamicArray

    End Sub

    KHAI BAO ERROR

    Private Sub Command1_Click()

    On Error Resume Next

    Error 11

    Debug.Print Err.Description

    Debug.Print Err.Source

    Debug.Print Err.Number

  • 7/30/2019 VISUAL BASIC 6 - HM V V D

    38/215

    End Sub

    KHAI BAO EXIT

    Private Sub Command1_Click()

    Dim I, MyNum

    Do

    For I = 1 To 1000

    MyNum = Int(Rnd * 1000)

    Select Case MyNum

    Case 7: Exit For

    Case 29: Exit Do

    Case 54: Exit Sub

    End Select

    Next I

    Loop

    End Sub

    KHAI BAO FILECOPY

    Private Sub Command1_Click()

    Dim SourceFile, DestinationFile

    SourceFile = "C:\CONFIG.SYS"

    DestinationFile = "D:\CONFIG.SYS"

    FileCopy SourceFile, DestinationFile

    End Sub

    KHAI BAO FOR EACH..NEXT

    Private Sub Command1_Click()

    Dim Found, MyObject, MyCollection

    Found = False

    For Each MyObject In MyCollection

    If MyObject.Text = "Hello" Then

    Found = True

    Exit For

    End If

    Next

    End Sub

    KHAI BAO FOR..NEXT

    Private Sub Command1_Click()

    Dim Words, Chars, MyString

    For Words = 10 To 1 Step -1

    For Chars = 0 To 9

  • 7/30/2019 VISUAL BASIC 6 - HM V V D

    39/215

    MyString = MyString & Chars

    Next Chars

    MyString = MyString & " "

    Debug.Print MyString

    Next Words

    End Sub

    KHAI BAO FUNCTION

    Function CalculateSquareRoot( NumberArg As Double) As Double

    If NumberArg < 0 Then

    Exit Function

    Else

    CalculateSquareRoot = Sqr(NumberArg)

    End If

    End Function

    Function CalSum (ByVal FirstArg As Integer, ParamArray OtherArgs()) As Integer

    If FirstArg < 0 Then

    Exit Function

    Else

    CalSum = FirstArg+Total

    End If

    End Function

    Function MyFunc(MyArg As Integer, Optional MyArg1 As Integer = 5, Optional MyArg2 = 10) As Integer

    MyFunc MyArg + MyArg1 + MyArg2

    End Function

    Private Sub Command1_Click()

    Debug.Print CalculateSquareRoot(10)

    Debug.Print CalSum(10, 1, 2, 3, 4, 5)

    Dim RetVal

    RetVal = MyFunc(1, 2, 2)

    Debug.Print RetVal

    RetVal = MyFunc(1, , 5)

    Debug.Print RetVal

    RetVal = MyFunc(MyArg:=10, MyArg2:=7)

    Debug.Print RetVal

    RetVal = MyFunc(1)

    Debug.Print RetVal

    End Sub

  • 7/30/2019 VISUAL BASIC 6 - HM V V D

    40/215

    KHAI BAO GOTO

    Private Sub Command1_Click()

    Dim Number, MyString

    Number = 1

    If Number = 1 Then

    GoTo Line1

    Else

    GoTo Line2

    End If

    Line1:

    MyString = "Number equals 1"

    GoTo LastLine

    Line2:

    MyString = "Number equals 2"

    LastLine:

    Debug.Print MyString

    End Sub

    PHAT BIEU DIEU KHIEN IF..THEN..ELSE..END IF

    Private Sub Command1_Click()

    Dim A, B, C

    A = InputBox("Please enter value of A:")

    If IsNumeric(A) Then A = A + 1: B = B + A: C = C + B

    Debug.Print A

    B = InputBox("Please enter value of B:")

    If B > 0 Then

    A = A + B

    ElseIf B = 0 Then

    A = A - B

    Else

    A = B

    End If

    End Sub

    KHAI BAO INPUT #

    Private Sub Command1_Click()

    Dim MyString, MyNumber

    Open "C:\TESTFILE.txt" For Input As #1

    Do While Not EOF(1)

    Input #1, MyString, MyNumber

  • 7/30/2019 VISUAL BASIC 6 - HM V V D

    41/215

    Debug.Print MyString, MyNumber

    Loop

    Close #1

    End Sub

    KHAI BAO KILL

    Private Sub Command1_Click()

    Kill "C:\TestFile.txt"

    Kill "C:\*.TXT"

    End Sub

    KHAI BAO LINE INPUT #

    Private Sub Command1_Click()

    Dim MyString

    Open "C:\TESTFILE.txt" For Input As #1

    Do While Not EOF(1)

    Line Input #1, MyString

    Debug.Print MyString

    Loop

    Close #1

    End Sub

    KHAI BAO LOAD

    Private Sub Command1_Click()

    Dim Answer, Msg As String

    Unload Form1

    Msg = "Form1 cha nap. "

    Msg = Msg & "Chon Yes neu nap vao hien thi form "

    Msg = Msg & "CHon No nap form vao khong hien thi."

    Answer = MsgBox(Msg, vbYesNo)

    If Answer = vbYes Then

    Show

    Else

    Load Form1

    Msg = "Form1 ao nap. "

    Msg = Msg & "Chon OK e hien thi form."

    MsgBox Msg

    Show

    End If

    End Sub

    KHAI BAO LOCK, UNLOCK

  • 7/30/2019 VISUAL BASIC 6 - HM V V D

    42/215

    Private Sub Command1_Click()

    Dim MyRecord As Record, RecordNumber

    Open "C:\TESTFILE.txt" For Random Shared As #1 Len = Len(MyRecord)

    RecordNumber = 4

    Lock #1, RecordNumber

    Get #1, RecordNumber, MyRecord

    MyRecord.ID = 234

    MyRecord.Name = "John Smith"

    Put #1, RecordNumber, MyRecord

    Unlock #1, RecordNumber

    Close #1

    End Sub

    KHAI BAO MID

    Private Sub Command1_Click()

    Dim MyString

    MyString = "The dog jumps"

    Debug.Print MyString

    Mid(MyString, 5, 3) = "fox"

    Debug.Print MyString

    Mid(MyString, 5) = "cow"

    Debug.Print MyString

    Mid(MyString, 5) = "cow jumped over"

    Debug.Print MyString

    Mid(MyString, 5, 3) = "duck"

    Debug.Print MyString

    End Sub

    KHAI BAO MKDIR

    Private Sub Command1_Click()

    Dim MyFolder

    MyFolder = "C:\myVb6"

    MkDir MyFolder

    End Sub

    KHAI BAO NAME

    Private Sub Command1_Click()

    Dim OldName, NewName

    OldName = "OLDFILE": NewName = "NEWFILE"

    Name OldName As NewName

    OldName = "C:\MYDIR\OLDFILE"

  • 7/30/2019 VISUAL BASIC 6 - HM V V D

    43/215

    NewName = "C:\YOURDIR\NEWFILE"

    Name OldName As NewName

    End Sub

    KHAI BAO ON ERROR

    Private Sub Command1_Click()

    On Error GoTo ErrorHandler

    Open "TESTFILE" For Output As #1

    Kill "TESTFILE"

    On Error GoTo 0

    On Error Resume Next

    ObjectRef = GetObject("MyWord.Basic")

    If err.Number = 440 Or err.Number = 432 Then

    Msg = "There was an error attempting to open "

    Msg = Msg & "the Automation object!"

    MsgBox Msg, , "Deferred Error Test"

    err.Clear

    End If

    Exit Sub

    ErrorHandler:

    Select Case err.Number

    Case 55

    Close #1

    Case Else

    End Select

    Resume

    End Sub

    KHAI BAO OPEN

    Private Sub Command1_Click()

    Dim MyString

    Open "C:\TESTFILE.txt" For Input As #1

    Do While Not EOF(1)

    Line Input #1, MyString

    Debug.Print MyString

    Loop

    Close #1

    End Sub

  • 7/30/2019 VISUAL BASIC 6 - HM V V D

    44/215

    KHAI BAO OPTION BASE

    Option Base 1

    Private Sub Command1_Click()

    Dim Lower

    Dim MyArray(20), TwoDArray(3, 4)

    Dim ZeroArray(0 To 5)

    Lower = LBound(MyArray)

    Debug.Print Lower

    Lower = LBound(TwoDArray, 1)

    Debug.Print Lower

    Lower = LBound(TwoDArray, 2)

    Debug.Print Lower

    Lower = LBound(ZeroArray)

    Debug.Print Lower

    End Sub

    KHAI BAO PRINT #

    Private Sub Command1_Click()

    Open "TESTFILE" For Output As #1

    Print #1, "This is a test"

    Print #1,

    Print #1, "Zone 1"; Tab; "Zone 2"

    Print #1, "Hello"; " "; "World"

    Print #1, Spc(5); "5 leading spaces "

    Print #1, Tab(10); "Hello"

    Dim MyBool, MyDate, MyNull, MyError

    MyBool = False: MyDate = #2/12/1969#

    MyNull = Null: MyError = CVErr(32767)

    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

    End Sub

    KHAI BAO PUBLIC

    Public piAs Integer

    Private Sub Command1_Click()

    Dim AnyValue, MyValue

  • 7/30/2019 VISUAL BASIC 6 - HM V V D

    45/215

    Dim Number As Integer

    Number=5

    piAs=100+ Number

    End Sub

    Private Sub Command1_Click()

    Dim AnyValue, MyValue

    Dim Number As Integer

    Number=15

    piAs= piAs + Number

    End Sub

    KHAI BAO PUT

    Type Record

    ID As Integer

    Name As String * 20

    End Type

    Private Sub Command1_Click()

    Dim MyRecord As Record, RecordNumber

    Open "TESTFILE" For Random As #1 Len = Len(MyRecord)

    For RecordNumber = 1 To 5

    MyRecord.ID = RecordNumber

    MyRecord.Name = "My Name" & RecordNumber

    Put #1, RecordNumber, MyRecord

    Next RecordNumber

    Close #1

    End Sub

    KHAI BAO RAISEEVENT

    Private WithEvents mText As TimerState

    Private Sub Command1_Click()

    Text1.Text = "From Now"

    Text1.Refresh

    Text2.Text = "0"

    Text2.Refresh

    Call mText.TimerTask(9.84)

    End Sub

    Private Sub Form_Load()

    Command1.Caption = "Click to Start Timer"

    Text1.Text = ""

    Text2.Text = ""

  • 7/30/2019 VISUAL BASIC 6 - HM V V D

    46/215

    Label1.Caption ="The fastest 100 meters ever run took this long:"

    Set mText = New TimerState

    End Sub

    Private Sub mText_ChangeText()

    Text1.Text = "Until Now"

    Text2.Text = "9.84"

    End Sub

    Private Sub mText_UpdateTime(ByVal dblJump As Double)

    Text2.Text = Str(Format(dblJump, "0"))

    DoEvents

    End Sub

    Option Explicit

    Public Event UpdateTime(ByVal dblJump As Double)

    Public Event ChangeText()

    Public Sub TimerTask(ByVal Duration As Double)

    Dim dblStart As Double

    Dim dblSecond As Double

    Dim dblSoFar As Double

    dblStart = Timer

    dblSoFar = dblStart

    Do While Timer < dblStart + Duration

    If Timer - dblSoFar >= 1 Then

    dblSoFar = dblSoFar + 1

    RaiseEvent UpdateTime(Timer - dblStart)

    End If

    Loop

    RaiseEvent ChangeText

    End Sub

    KHAI BAO RANDOMIZE

    Private Sub Command1_Click()

    Dim MyValue

    Randomize

    MyValue = Int((6 * Rnd) + 1)

    Debug.Print MyValue

    End Sub

  • 7/30/2019 VISUAL BASIC 6 - HM V V D

    47/215

    KHAI BAO REDIM

    Private Sub Command1_Click()

    Dim MyArray() As Integer

    ReDim MyArray(5)

    For I = 1 To 5

    MyArray(I) = I

    Next I

    ReDim MyArray(10)

    For I = 1 To 10

    MyArray(I) = I

    Next I

    ReDim Preserve MyArray(15)

    End Sub

    KHAI BAO REM

    Private Sub Command1_Click()

    Dim MyStr1, MyStr2

    MyStr1 = "Hello"

    Rem Comment after a statement separated by a colon.

    MyStr2 = "Goodbye"

    End Sub

    KHAI BAO RESET

    Private Sub Command1_Click()

    Dim FileNumber

    For FileNumber = 1 To 5

    Open "TEST" & FileNumber For Output As #FileNumber

    Write #FileNumber, "Hello World"

    Next FileNumber

    Reset

    End Sub

    KHAI BAO RMDIR

    Private Sub Command1_Click()

    RmDir "C:\myFolder"

    End Sub

    KHAI BAO SAVEPICTURE

    Private Sub Command1_Click()

    Dim CX, CY, Limit

    Dim RadiusAs Integer, Msg As String

    ScaleMode = vbPixels

  • 7/30/2019 VISUAL BASIC 6 - HM V V D

    48/215

    AutoRedraw = True

    Width = Height

    CX = ScaleWidth / 2

    CY = ScaleHeight / 2

    Limit = CX

    For Radius = 0 To Limit

    Circle (CX, CY), Radius, RGB(Rnd * 255,Rnd * 255, Rnd * 255)

    DoEvents

    Next Radius

    Msg = "Choose OK to save the graphics "

    Msg = Msg & "from this form to a bitmap file."

    MsgBox Msg

    SavePicture Me.Image, "C:\TEST1.jpg"

    End Sub

    KHAI BAO SAVESETTING

    Private Sub Command1_Click()

    SaveSetting appname:="My.Opera.Com/ManMan89", section:="Startup", Key:="Top", setting:=75

    SaveSetting "My.Opera.Com/ManMan89", "Startup", "Left", 50

    DeleteSetting "My.Opera.Com/ManMan89", "Startup"

    End Sub

    KHAI BAO SEEK

    Private Sub Command1_Click()

    Dim MyRecord As Record, MaxSize, RecordNumber

    Open "TESTFILE" For Random As #1 Len = Len(MyRecord)

    MaxSize = LOF(1) \ Len(MyRecord)

    For RecordNumber = MaxSize To 1 Step -1

    Seek #1, RecordNumber

    Get #1, , MyRecord

    Next RecordNumber

    Close #1

    End Sub

    KHAI BAO SELECT CASE

    Private Sub Command1_Click()

    Dim Number

    Number = InputBox("Please enter number")

    Select Case Number

    Case 1 To 5

    Debug.Print "Between 1 and 5"

  • 7/30/2019 VISUAL BASIC 6 - HM V V D

    49/215

    Case 6, 7, 8

    Debug.Print "Between 6 and 8"

    Case 9 To 10

    Debug.Print "Greater than 8 & less than 10"

    Case Is > 10

    Debug.Print "More than 10"

    Case Else

    Debug.Print "Not more than 1"

    End Select

    End Sub

    KHAI BAO SENDKEYS

    Private Sub Command1_Click()

    Dim ReturnValue, I

    ReturnValue = Shell("CALC.EXE", 1)

    AppActivate ReturnValue

    For I = 1 To 100

    SendKeys I & "{+}", True

    Next I

    SendKeys "=", True

    SendKeys "%{F4}", True

    End Sub

    KHAI BAO SET

    Private Sub Command1_Click()

    Dim myCon, myRst

    Set myCon = CreateObject("ADODB.Connection")

    Set myRst = CreateObject("ADODB.Recordset")

    myCon.Open strCon

    Set myRst = Nothing

    Set myCon = Nothing

    End Sub

    KHAI BAO SETATTR

    Private Sub Command1_Click()

    SetAttr "c:TESTFILE.txt", vbHidden

    SetAttr "TESTFILE", vbHidden + vbReadOnly

    End Sub

    KHAI BAO STATIC

    Function KeepTotal(Number)

    Static Accumulate

  • 7/30/2019 VISUAL BASIC 6 - HM V V D

    50/215

    Accumulate = Accumulate + Number

    KeepTotal = Accumulate

    End Function

    Static Function MyFunction(Arg1, Arg2, Arg3)

    Accumulate = Arg1 + Arg2 + Arg3

    Half = Accumulate / 2

    MyFunction = Half

    End Function

    Private Sub Command1_Click()

    Dim ldTotal As Double

    ldTotal = KeepTotal(10)

    ldTotal = ldTotal + MyFunction(1, 2, 3)

    Debug.Print ldTotal

    End Sub

    KHAI BAO STOP

    Private Sub Command1_Click()

    Dim ldTotal As Double

    ldTotal = 10

    Debug.Print ldTotal

    Stop

    ldTotal = ldTotal + 20

    Debug.Print ldTotal

    End Sub

    KHAI BAO SUB

    Public Accumulate As Integer

    Sub KeepTotal(Number)

    Accumulate = Accumulate + Number

    Number = Number + 10

    End Sub

    Private Sub Command1_Click()

    Dim ldTotal As Double

    Accumulate = 0

    ldTotal = 10

    KeepTotal (ldTotal)

    ldTotal = Accumulate

    Debug.Print Accumulate

    ldTotal = Number

    Debug.Print ldTotal

  • 7/30/2019 VISUAL BASIC 6 - HM V V D

    51/215

    End Sub

    KHAI BAO TYPE

    Type EmployeeRecord

    ID As Integer

    Name As String

    Address As String

    Phone As Long

    HireDate As Date

    End Type

    Private Sub Command1_Click()

    Dim Employees(10) As EmployeeRecord

    Dim i As Integer

    For i = 0 To 9

    Employees(i).ID = i

    Employees(i).Name = "PHM Huu Khang " & i

    Next

    For i = 0 To 9

    Debug.Print Employees(i).ID & " " & Employees(i).Name

    Next

    End Sub

    KHAI BAO UNLOAD

    Private Sub Command1_Click()

    Unload frmEmployee

    Unload Me

    End Sub

    KHAI BAO WHILE..WEND

    Private Sub Command1_Click()

    Dim Counter

    Counter = 0

    While Counter < 20

    Counter = Counter + 1

    Wend

    Debug.Print Counter

    End Sub

    KHAI BAO WIDTH

    Private Sub Command1_Click()

    Dim I

    Open "TESTFILE" For Output As #1

  • 7/30/2019 VISUAL BASIC 6 - HM V V D

    52/215

    Mo tap tin xet ghi

    VBA.Width 1, 5

    For I = 0 To 9

    Print #1, Chr(48 + I);

    Next I

    Close #1

    End Sub

    KHAI BAO WITH..END WITH

    Private Sub Command1_Click()

    Dim MyObject As Object

    Set MyObject = Me

    With MyObject

    .Width = 100

    .Caption = "Hello World"

    End With

    End Sub

    KHAI BAO WRITE

    Private Sub Command1_Click()

    Open "C:\teo.txt" For Output As #1

    Write #1, "Hello World", 234

    Write #1,

    Dim MyBool, MyDate, MyNull, MyError

    MyBool = False: MyDate = #2/12/1969#

    MyNull = Null

    MyError = CVErr(32767)

    Write #1, MyBool; " is a Boolean value"

    Write #1, MyDate; " is a date"

    Write #1, MyNull; " is a null value"

    Write #1, MyError; " is an error value"

    Close #1

    End Sub

    Exam CHAPTER 3

    PHEP TOAN &

    Private Sub Command1_Click()

    Dim MyStr

    MyStr = "Hello" & " World"

    Debug.Print MyStr

    MyStr = "Check " & 123 & " Check"

  • 7/30/2019 VISUAL BASIC 6 - HM V V D

    53/215

    Debug.Print MyStr

    End Sub

    PHEP TOAN *

    Private Sub Command1_Click()

    Dim MyNum

    MyNum = 10

    Debug.Print MyStr

    MyNum = MyNum*20

    Debug.Print MyStr

    End Sub

    PHEP TOAN +

    Private Sub Command1_Click()

    Dim MyNum

    MyNum = 10

    Debug.Print MyNum

    MyNum = MyNum+20

    Debug.Print MyNum

    MyNum = "Pham" + " Huu Khang"

    Debug.Print MyNum

    End Sub

    PHEP TOAN -

    Private Sub Command1_Click()

    Dim MyNum

    MyNum = 10

    Debug.Print MyNum

    MyNum = MyNum-20

    Debug.Print MyNum

    End Sub

    PHEP TOAN /

    Private Sub Command1_Click()

    Dim MyNum

    MyNum = 100

    Debug.Print MyNum

    MyNum = MyNum/20

    Debug.Print MyNum

    MyNum = 0/MyNum

    Debug.Print MyNum

    End Sub

  • 7/30/2019 VISUAL BASIC 6 - HM V V D

    54/215

    PHEP TOAN

    Private Sub Command1_Click()

    Dim MyNum

    MyNum = 107

    Debug.Print MyNum

    MyNum = MyNum\20

    Debug.Print MyNum

    MyNum = 0\MyNum

    Debug.Print MyNum

    End Sub

    PHEP TOAN ^

    Private Sub Command1_Click()

    Dim MyNum

    MyNum = 10

    Debug.Print MyNum^2

    MyNum = MyNum^0

    Debug.Print MyNum

    MyNum = 0^MyNum

    Debug.Print MyNum

    End Sub

    PHEP TOAN AND

    Private Sub Command1_Click()

    Dim A, B, C, D, MyCheck

    A = 10: B = 8: C = 6: D = Null

    MyCheck = A > B And B > C

    Debug.Print MyCheck

    MyCheck = B > A And B > C

    Debug.Print MyCheck

    MyCheck = A > B And B > D

    Debug.Print MyCheck

    MyCheck = A And B

    Debug.Print MyCheck

    End Sub

    PHEP TOAN EQV

    Private Sub Command1_Click()

    Dim A, B, C, D, MyCheck

    A = 10: B = 8: C = 6: D = Null

    MyCheck = A > B Eqv B > C

  • 7/30/2019 VISUAL BASIC 6 - HM V V D

    55/215

    Debug.Print MyCheck

    MyCheck = B > A Eqv B > C

    Debug.Print MyCheck

    MyCheck = A > B Eqv B > D

    Debug.Print MyCheck

    MyCheck = A Eqv B

    Debug.Print MyCheck

    End Sub

    PHEP TOAN IMP

    Private Sub Command1_Click()

    Dim A, B, C, D, MyCheck

    A = 10: B = 8: C = 6: D = Null

    MyCheck = A > B Imp B > C

    Debug.Print MyCheck

    MyCheck = A > B Imp C > B

    Debug.Print MyCheck

    MyCheck = B > A Imp C > B

    Debug.Print MyCheck

    MyCheck = B > A Imp C > D

    Debug.Print MyCheck

    MyCheck = C > D Imp B > A

    Debug.Print MyCheck

    MyCheck = B Imp A

    Debug.Print MyCheck

    End Sub

    PHEP TOAN IS

    Private Sub Command1_Click()

    Dim MyObject, YourObject, ThisObject

    Dim OtherObject, ThatObject, MyCheck

    Set YourObject = MyObject

    Set ThisObject = MyObject

    Set ThatObject = OtherObject

    MyCheck = YourObject Is ThisObject

    Debug.Print MyCheck

    MyCheck = ThatObject Is ThisObject

    Debug.Print MyCheck

    MyCheck = MyObject Is ThatObject

    Debug.Print MyCheck

  • 7/30/2019 VISUAL BASIC 6 - HM V V D

    56/215

    End Sub

    PHEP TOAN MOD

    Private Sub Command1_Click()

    Dim MyNum

    MyNum = 107

    Debug.Print MyNum

    MyNum = MyNum Mod 20

    Debug.Print MyNum

    MyNum = 0 Mod MyNum

    Debug.Print MyNum

    End Sub

    PHEP TOAN NOT

    Private Sub Command1_Click()

    Dim A, B, C, D, MyCheck

    A = 10: B = 8: C = 6: D = Null

    MyCheck = Not (A > B)

    MyCheck = Not (B > A)

    MyCheck = Not (C > D)

    MyCheck = Not A

    End Sub

    PHEP TOAN OR

    Private Sub Command1_Click()

    Dim A, B, C, D, MyCheck

    A = 10: B = 8: C = 6: D = Null

    MyCheck = A > B Or B > C

    Debug.Print MyCheck

    MyCheck = B > A Or B > C

    Debug.Print MyCheck

    MyCheck = A > B Or B > D

    Debug.Print MyCheck

    MyCheck = B > D Or B > A

    Debug.Print MyCheck

    MyCheck = A Or B

    Debug.Print MyCheck

    End Sub

    PHEP TOAN XOR

    Private Sub Command1_Click()

    Dim A, B, C, D, MyCheck

  • 7/30/2019 VISUAL BASIC 6 - HM V V D

    57/215

    A = 10: B = 8: C = 6: D = Null

    MyCheck = A > B Xor B > C

    Debug.Print MyCheck

    MyCheck = B > A xOr B > C

    Debug.Print MyCheck

    MyCheck = B > A xOr C > B

    Debug.Print MyCheck

    MyCheck = B > D XOr A > B

    Debug.Print MyCheck

    MyCheck = A XOr B

    Debug.Print MyCheck

    End Sub

    Exam CHAPTER 4

    THUOC TINH ACTIONPrivate Sub Command1_Click()

    Me.CommonDialog1.Action = 1

    Me.CommonDialog1.Action = 2

    Me.CommonDialog1.Action = 3

    Me.CommonDialog1.Action = 4

    Me.CommonDialog1.Action = 5

    End Sub

    Private Sub Command1_Click()

    Me.CommonDialog1.ShowOpenMe.CommonDialog1.ShowSaveMe.CommonDialog1.ShowColor

    Me.CommonDialog1.ShowFont

    Me.CommonDialog1.ShowPrinter

    End Sub

    THUOC TINH ACTIVECONNECTION

    Khai bo v khi to i tng

    Dim rdoCn As New rdoConnection

    Dim rdoCn2 As New rdoConnection

    Dim rdoQy As New rdoQueryDim rdoRs As rdoResultset

    Dim rdoCol As rdoColumn

    Dim rdoEn As rdoEnvironment

    Khai bo trong bin c Load ca Form1

    Private Sub Form_Load()

    On Error GoTo CnEh

  • 7/30/2019 VISUAL BASIC 6 - HM V V D

    58/215

    Gn i tng RDOEnviroment

    Set rdoEn = rdoEnvironments(0)

    With rdoCn

    THUOC TINH Connect vi chui kt ni c s d liu

    .Connect = "UID=sa;PWD=concat;Database=Northwind;" & "Server=.;Driver={SQL Server}" & "DSN='';"

    .LoginTimeout = 5

    .EstablishConnection rdDriverNoPrompt, True

    rdoEn.rdoConnections.Add rdoCn

    End With

    With rdoCn2

    .Connect = "UID=sa;PWD=concat;Database=Northwind;" & "Server=.;Driver={SQL Server}" & "DSN='';"

    .LoginTimeout = 5

    .EstablishConnection rdDriverNoPrompt, True

    rdoEn.rdoConnections.Add rdoCn2

    End With

    Lit k tt c cc i tng c s d liu do ngi s dng to

    ra trong c s d liu Northwind

    With rdoQy

    Set .ActiveConnection = rdoCn

    .SQL = "Select Name, refDate " & " from Sysobjects where type = 'U' "

    .LockType = rdConcurReadOnly

    .RowsetSize = 1

    .CursorType = rdUseServer

    End With

    For Each rdoCn In rdoEn.rdoConnections

    Set rdoQy.ActiveConnection = rdoCn

    Set rdoRs = rdoQy.OpenResultset(rdOpenForwardOnly)

    With rdoRs

    For Each rdoCol In rdoRs.rdoColumns

    Debug.Print rdoCol.Name,

    Next

    Debug.Print

    Do Until rdoRs.EOF

    For Each rdoCol In rdoRs.rdoColumns

    Debug.Print rdoCol

    Next

    rdoRs.MoveNext

    Loop

  • 7/30/2019 VISUAL BASIC 6 - HM V V D

    59/215

    End With

    Next

    Exit Sub

    CnEh:

    Beep

    MsgBox Error

    End Sub

    THUOC TINH ACTIVECONTROL

    Private Sub Form_Click()

    If TypeOf Screen.ActiveControl Is TextBox Then

    Label1.Caption = Screen.ActiveControl.Text

    Else

    Label1.Caption = "Button: " + Screen.ActiveControl.Caption

    End If

    End Sub

    THUOC TINH ACTIVEFORM

    ' M trong MDI form.

    Private Sub MDIForm_Load()

    Dim NewForm As New Form1

    ' To mt i tng mi ca Form

    NewForm.Show

    End Sub

    Private Sub Command1_Click()

    ' In time trn Form ang kch hot

    ActiveForm.Print "The time is " & Format(Now, "Long Time")

    End Sub

    THUOC TINH ALIGN

    ' Thm PictureBox vo Form

    ' Thm CommandButton vo trong PictureBox

    Private Sub Command1_Click()

    If Picture1.Align = vbAlignTop Then

    Picture1.Align = vbAlignBottom

    ' Canh l cho PictureBox bn di form.

    Else

    Picture1.Align = vbAlignTop

    ' Canh l cho PictureBox trn u form.

    End If

    End Sub

  • 7/30/2019 VISUAL BASIC 6 - HM V V D

    60/215

    THUOC TINH ALIGNMENT

    ' Thm PictureBox vo Form

    ' Thm CommandButton vo trong PictureBox

    Private Sub Command1_Click()

    If Picture1.Align = vbAlignTop Then

    Picture1.Align = vbAlignBottom

    ' Canh l cho PictureBox bn di form.

    Else

    Picture1.Align = vbAlignTop

    ' Canh l cho PictureBox trn u form.

    End If

    End Sub

    THUOC TINH ALLOWROWS

    Dim strName As String

    'Thm mt iu khin DataGrid vo Form

    Private Sub DataGrid1_BeforeColEdit(ByVal ColIndex As Integer, ByVal KeyAscii As Integer, Cancel As Integer)

    If strName = "Phil" Then

    Cancel = False

    ' Nu tn l "Phil" th chng trnh cho php kch hot ni dung

    ca DataGrid.

    Else

    Cancel = True

    MsgBox "You've attempted to edit " & " column number " & ColIndex + 1 & " without the proper authorization."

    ' Khng cho php ngi khc kch hot ni dung trn DataGrid.

    End If

    End Sub

    Private Sub Form_Load()

    strName = InputBox("Please enter your name")

    DataGrid1.AllowArrows = True

    ' Cho php ngi s dng di chuyn trn DataGrid

    bng phm arrow keys.

    DataGrid1.WrapCellPointer = True

    End Sub

    THUOC TINH ALLOWVERTICAL

    Private Sub Form_Load()

    ' Gn captions nhn dng cho mi Band

    cbrMain.Bands(1).Caption = "1"

  • 7/30/2019 VISUAL BASIC 6 - HM V V D

    61/215

    cbrMain.Bands(2).Caption = "2"

    cbrMain.Bands(3).Caption = "3"

    ' Gn THUOC TINH AllowVertical ca Band 2

    cbrMain.Bands(2).AllowVertical = False

    End Sub

    Private Sub Form_Click()

    ' Thay i hng ca iu khin CoolBar

    If cbrMain.Orientation = cc3OrientationHorizontal Then

    cbrMain.Orientation = cc3OrientationVertical

    cbrMain.Align = vbAlignLeft

    Else

    cbrMain.Orientation = cc3OrientationHorizontal

    cbrMain.Align = vbAlignTop

    End If

    End Sub

    THUOC TINH ARCHIVE, HIDDEN, NORMAL, SYSTEM

    Private Sub Command1_Click()

    File1.Archive = True

    File1.Hidden = True

    File1.Normal = True

    File1.System = True

    End Sub

    THUOC TINH ARRANGE

    Private Sub Option1_Click(Index As Integer)

    ' Gn THUOC TINH Arrange vo THUOC TINH Index ca iu

    khin Option1.

    ListView1.Arrange = Index

    End Sub

    Private Sub Form_Load()

    ' nh ngha nhn cho iu khin OptionButton

    Option1(0).Caption = "No Arrange"

    Option1(1).Caption = "Align Auto Left"

    Option1(2).Caption = "Align Auto Top"

    ' Khai bo bin to i tng ListView v ImageList.

    Dim i As Integer

    Dim itmX As ListItem

    Dim imgX As ListImage

    ' Khai bo bin i tng cho i tng ListImages.

  • 7/30/2019 VISUAL BASIC 6 - HM V V D

    62/215

    ' Thm i tng ListImage vo iu khin ImageList.

    Set imgX = ImageList1.ListImages. Add(, , LoadPicture("icons\mail01a.ico"))

    ListView1.Icons = ImageList1

    ' Chn biu tng ng vi iu khin ImageList.

    ' Thm 10 i tng ListItem, mi i tng ng vi 1 Icon.

    For i = 1 To 10

    Set itmX = ListView1.ListItems.Add()

    itmX.Icon = 1

    itmX.Text = "ListItem " & i

    Next i

    End Sub

    THUOC TINH ATTACHMENTCOUNT

    Private Sub Command4_Click()

    MAPIMessages1.Compose

    MAPIMessages1.RecipDisplayName = Text1.Text

    MAPIMessages1.MsgSubject = Text2.Text

    MAPIMessages1.MsgNoteText = Text4.Text

    MAPIMessages1.AttachmentName =test.txt"

    MAPIMessages1.AttachmentPathName = "C:\test.txt"

    i = MAPIMessages1.AttachmentCount

    MAPIMessages1.ResolveName

    MAPIMessages1.Send

    End Sub

    THUOC TINH ATTACHMENTNAME

    Private Sub Command2_Click()

    MAPIMessages1.Compose

    MAPIMessages1.RecipDisplayName = Text1.Text

    MAPIMessages1.MsgSubject = Text2.Text

    MAPIMessages1.MsgNoteText = Text4.Text

    MAPIMessages1.AttachmentName =test.txt"

    MAPIMessages1.AttachmentPathName = "C:\test.txt"

    i = MAPIMessages1.AttachmentCount

    MAPIMessages1.ResolveName

    MAPIMessages1.Send

    End Sub

    THUOC TINH ATTACHMENTPATHNAME

    Private Sub Command5_Click()

    MAPIMessages1.Compose

  • 7/30/2019 VISUAL BASIC 6 - HM V V D

    63/215

    MAPIMessages1.RecipDisplayName = Text1.Text

    MAPIMessages1.MsgSubject = Text2.Text

    MAPIMessages1.MsgNoteText = Text4.Text

    MAPIMessages1.AttachmentName =test.txt"

    MAPIMessages1.AttachmentPathName = "C:\test.txt"

    i = MAPIMessages1.AttachmentCount

    MAPIMessages1.ResolveName

    MAPIMessages1.Send

    End Sub

    THUOC TINH ATTRIBUTES

    Private Sub Command1_Click()

    Dim fs, f, r

    Set fs = CreateObject("Scripting.FileSystemObject")

    Set f = fs.GetFile(fs.GetFileName(filespec))

    If f.Attributes And 32 Then

    r = MsgBox("The Archive bit is set, " & " do you want to clear it?", vbYesNo, "Set/Clear Archive Bit")

    If r = vbYes Then

    f.Attributes = f.Attributes - 32

    MsgBox "Archive bit is cleared."

    Else

    MsgBox "Archive bit remains set."

    End If

    Else

    r = MsgBox("The Archive bit is not set. " &"Do you want to set it?", vbYesNo, "Set/Clear Archive Bit")

    If r = vbYes Then

    f.Attributes = f.Attributes + 32

    MsgBox "Archive bit is set."

    Else

    MsgBox "Archive bit remains clear."

    End If

    End If

    End Sub

    THUOC TINH AUTOINCREAMENT

    Private Sub Command1_Click()

    With MSChart1

    ' Trnh by biu dng 3d vi 8 ct v 8 hng d liu.

    .ChartType = VtChChartType3dBar

    .ColumnCount = 8

  • 7/30/2019 VISUAL BASIC 6 - HM V V D

    64/215

    .AutoIncrement = False

    .RowCount = 8

    For Column = 1 To 8

    For Row = 1 To 8

    .Column = Column

    .Row = Row

    .Data = Row * 10

    Next Row

    Next Column

    .ShowLegend = True

    .SelectPart VtChPartTypePlot, index1, index2, index3, index4

    .EditCopy

    .SelectPart VtChPartTypeLegend, index1, index2, index3, index4

    .EditPaste

    End With

    End Sub

    THUOC TINH AUTOPLAY

    Private Sub Command1_Click()

    With CommonDialog1

    .Filter = "avi (*.avi)|*.avi"

    .ShowOpen

    End With

    With Animation1

    .Autoplay = True

    .Open CommonDialog1.FileName

    End With

    End Sub

    THUOC TINH AUTODRAW

    Thm iu khin PictureBox trn form

    Private Sub Form_Load()

    Picture1.ScaleHeight = 100

    ' Gn t l 100.

    Picture1.ScaleWidth = 100

    Picture1.AutoRedraw = True

    ' Gn THUOC TINH AutoRedraw.

    Picture1.ForeColor = 0

    ' Gn mu ch (ForeColor).

    Picture1.FillColor = QBColor(9)

  • 7/30/2019 VISUAL BASIC 6 - HM V V D

    65/215

    ' Gn THUOC TINH t mu (FillColor).

    Picture1.FillStyle = 0

    ' Set FillStyle.

    Picture1.Circle (50, 50), 30

    ' V hnh tr.

    Picture1.AutoRedraw = False

    ' Turn off AutoRedraw.

    End Sub

    Private Sub Picture1_Click()

    Dim I

    ' Khai bo bin

    Picture1.ForeColor = RGB(Rnd * 255, 0, 0)

    ' Chn mu ngu nhin.

    For I = 5 To 95 Step 10

    ' V ng thng

    Picture1.Line (I, 0)-(I, 100)

    Next

    End Sub

    THUOC TINH AUTOSHOWCHILDREN

    Khai bo trong MDIForm

    Private Sub MDIForm_Load()

    MDIForm1.AutoShowChildren = False

    ' Che du Form dng Child mi khi n np ln.

    Dim HideForm As New Form1

    ' Khai bo i tng Form.

    HideForm.Caption = "HideForm"

    ' gn THUOC TINH Caption.

    Load HideForm

    ' Np v che du

    MDIForm1.AutoShowChildren = True

    ' Gn THUOC TINH AutoShowChildren l True Form con

    xut hin mi khi chng np ln MDIForm.

    Dim ShowForm As New Form1

    ' Khai bo i tng form.

    ShowForm.Caption = "ShowForm"

    ' Gn THUOC TINH caption.

    Load ShowForm

    ' Np v hin th

  • 7/30/2019 VISUAL BASIC 6 - HM V V D

    66/215

    End Sub

    THUOC TINH AUTOSIZE (PANEL)

    ' Thm mt iu khin StatusBar v PictureBox vo Form

    Private Sub Form_Load()

    Dim pnlX As Panel

    ' Gn THUOC TINH tag cho mi iu khin v Form.

    Form1.Tag = "Project 1 Form"

    Command1.Tag = "A command button"

    Picture1.Tag = "Picture Box Caption"

    StatusBar1.Tag = "Application StatusBar1"

    ' Gn THUOC TINH AutoSize ca Panel u tin vi ni dung.

    StatusBar1.Panels(1).AutoSize = sbrContents

    ' Thm 2 Panel v gn ni dung cho chng.

    Set pnlX = StatusBar1.Panels.Add

    pnlX.AutoSize = sbrContents

    Set pnlX = StatusBar1.Panels.Add

    pnlX.AutoSize = sbrContents

    End Sub

    Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, y As Single)

    ' Hin th gi tr THUOC TINH Tag ca iu khin trong Panel 1,

    ' v to x,y trn Panel 2 v 3. Bi v THUOC TINH AutoSize

    ' bng Contents cn Panel ht nht s ko di.

    StatusBar1.Panels(1).Text = Form1.Tag

    StatusBar1.Panels(2).Text = "X = " & X

    StatusBar1.Panels(3).Text = "Y = " & y

    End Sub

    ' Khai bo trong bin c MouseMove ca nt Command1

    Private Sub Command1_MouseMove(Button As Integer, Shift As Integer, X As Single, y As Single)

    StatusBar1.Panels(1).Text = Command1.Tag

    StatusBar1.Panels(2).Text = "X = " & X

    StatusBar1.Panels(3).Text = "Y = " & y

    End Sub

    ' Khai bo trong bin c MouseMove ca iu khin PictureBox

    Private Sub Picture1_MouseMove(Button As Integer, Shift As Integer, X As Single, y As Single)

    StatusBar1.Panels(1).Text = Picture1.Tag

    StatusBar1.Panels(2).Text = "X = " & X

    StatusBar1.Panels(3).Text = "Y = " & y

    End Sub

  • 7/30/2019 VISUAL BASIC 6 - HM V V D

    67/215

    ' Khai bo trong bin c MouseMove ca iu khin StatusBar

    Private Sub StatusBar1_MouseMove(Button As Integer, Shift As Integer, X As Single, y As Single)

    StatusBar1.Panels(1).Text = StatusBar1.Tag

    StatusBar1.Panels(2).Text = "X = " & X

    StatusBar1.Panels(3).Text = "Y = " & y

    End Sub

    THUOC TINH BACKCOLOR, BACKCOLORBKG, BACKCOLORFIXED, BACKCOLORSEL

    Private Sub Form_Load()

    Timer1.Interval = 500

    End Sub

    Private Sub Timer1_Timer()

    MSHFlexGrid1.BackColorBkg = QBColor(Rnd * 15)

    MSHFlexGrid1.BackColorFixed = QBColor(Rnd * 10)

    MSHFlexGrid1.BackColorSel = QBColor(Rnd * 10)

    End Sub

    THUOC TINH BACKCOLOR, FORECOLOR

    Private Sub Form_Load()

    Timer1.Interval = 500

    End Sub

    Private Sub Timer1_Timer()

    BackColor = QBColor(Rnd * 15)

    ForeColor = QBColor(Rnd * 10)

    Picture1.BackColor = QBColor(Rnd * 15)

    Picture1.ForeColor = QBColor(Rnd * 10)

    End Sub

    THUOC TINH BACKCOLOR, FORECOLOR

    thc hin v d sau, bn thm iu khin Timer1 v RichTextBox1 vo Form1.

    Private Sub Form_Load()

    Timer1.Interval = 500

    End Sub

    Private Sub Timer1_Timer()

    BackColor = QBColor(Rnd * 15)

    ForeColor = QBColor(Rnd * 10)

    RichTextBox1.BackColor = QBColor(Rnd * 15)

    End Sub

  • 7/30/2019 VISUAL BASIC 6 - HM V V D

    68/215

    THUOC TINH

    BACKCOLORBAND, BACKCOLORHEADER, BACKCOLORINDENT, BACKCOLORUNPOPULATED

    Private Sub Form_Load()

    Timer1.Interval = 500

    End Sub

    Private Sub Timer1_Timer()

    MSHFlexGrid1.Cols = 3

    MSHFlexGrid1.Rows = 5

    MSHFlexGrid1.Col = 0

    MSHFlexGrid1 = "ASAS"

    MSHFlexGrid1.BackColorBand(0) = QBColor(Rnd * 2)

    MSHFlexGrid1.BackColorHeader(0) = QBColor(Rnd * 15)

    MSHFlexGrid1.BackColorIndent(0) = QBColor(Rnd * 10)

    MSHFlexGrid1.BackColorUnpopulated = QBColor(Rnd * 10)

    End Sub

    THUOC TINH BANDBORDERS

    Private Sub Form_Load()

    ' Khai bo Caption cho mi Band

    cbrMain.Bands(1).Caption = "One"

    cbrMain.Bands(2).Caption = "Two"

    cbrMain.Bands(3).Caption = "Three"

    End Sub

    Private Sub Form_Click()

    If cbrMain.BandBorders = True Then

    ' Che du ng vin

    cbrMain.BandBorders = False

    Else

    ' Trnh by ng vin

    cbrMain.BandBorders = True

    End If

    End Sub

    THUOC TINH BOF V EOF

    Private Sub Command1_Click()

    ' Khai bo v khi to i tng ADO

    Dim myCon As New ADODB.Connection

    Dim myRs As New ADODB.Recordset

    Dim strCon As String

    ' Khai bo chui kt ni c s d liu SQL Server

  • 7/30/2019 VISUAL BASIC 6 - HM V V D

    69/215

    strCon = "Provider=SQLOLEDB.1;"

    strCon=strCon & "Persist Security Info=False;"

    strCon = strCon & "UID=sa;PWD=concat;"

    strCon = strCon & "Initial Catalog=Northwind;"

    strCon = strCon & "Server.;"

    ' M kt ni c s d liu

    myCon.Open strCon

    myRs.CursorLocation = adUseClient

    ' M tp d liu

    myRs.Open "Select * from Customers", myCon, adOpenDynamic, adLockReadOnly

    If myRs.RecordCount > 0 Then

    ' Kim tra THUOC TINH EOF

    Do Until myRs.EOF

    Debug.Print myRs("CompanyName")

    myRs.MoveNext

    Loop

    End If

    Set myRs = Nothing

    Set myCon = Nothing

    End Sub

    THUOC TINH BOLD

    Private Sub Form_Click()

    Font.Bold = Not Font.Bold

    Font.Strikethrough = Not Font.Strikethrough

    Font.Italic = Not Font.Italic

    Font.Underline = Not Font.Underline

    Font.Size = 16

    If Font.Bold Then

    Print "Font weight is " & Font.Weight & " (bold)."

    Else

    Print "Font weight is " & Font.Weight & " (not bold)."

    End If

    End Sub

    THUOC TINH BOLD

    Private Sub cmdMake_Click()

    tvwDB.Sorted = True

    Set mNode = tvwDB.Nodes.Add()

    mNode.Text = "Publisher"

  • 7/30/2019 VISUAL BASIC 6 - HM V V D

    70/215

    Set mNode = tvwDB.Nodes.Add()

    mNode.Text = "Private"

    Set mNode = tvwDB.Nodes.Add(1, tvwChild)

    mNode.Text = "DEF"

    Set mNode = tvwDB.Nodes.Add(1, tvwChild)

    mNode.Text = "GHI"

    Set mNode = tvwDB.Nodes.Add(2, tvwChild)

    mNode.Text = "ABC-1"

    mNode.Bold = True

    Set mNode = tvwDB.Nodes.Add(2, tvwChild)

    mNode.Text = "ABC-2"

    Set mNode = tvwDB.Nodes.Add(3, tvwChild)

    mNode.Text = "DEF-1"

    Set mNode = tvwDB.Nodes.Add(3, tvwChild)

    mNode.Text = "DEF-2"

    Set mNode = tvwDB.Nodes.Add(1, tvwChild)

    mNode.Text = "ABCD"

    End Sub

    THUOC TINH BOOKMARK

    Private Sub DataGrid1_UnboundDeleteRow(Bookmark As Variant)

    For i% = Bookmark + 1 To RowCount - 1

    For j% = 0 To MAXCOLS - 1

    UserData(j%, i% - 1) = UserData(j%, i%)

    Next j%

    Next i%

    End Sub

    THUOC TINH BORDERCOLOR

    Private Sub Command1_Click()

    Shape1.BorderColor = &H80000001

    End Sub

    THUOC TINH BORDERWIDTH

    Private Sub Form_Load()

    Combo1(0).Width = 1440 * 1.5

    Load Combo1(1)

    Combo1(1).Top = Combo1(0).Top + Combo1(0).Height * 1.5

    Combo1(1).Visible = True

    For i = 0 To 6

    Combo1(0).AddItem "BorderStyle = " & i

  • 7/30/2019 VISUAL BASIC 6 - HM V V D

    71/215

    Next i

    For i = 1 To 10

    Combo1(1).AddItem "BorderWidth = " & i

    Next i

    Combo1(0).ListIndex = 1

    Combo1(1).ListIndex = 0

    End Sub

    Private Sub Combo1_Click(Index As Integer)

    If Index = 0 Then

    Shape1.BorderStyle = Combo1(0).ListIndex

    Else

    Shape1.BorderWidth = Combo1(1).ListIndex + 1

    End If

    End Sub

    THUOC TINH BOTTOMMARGIN, TOPMARGIN

    Private Sub ChangeMargins(Optional fraction As Long)

    If fraction = 0 Then fraction = 8

    With DataReport1

    .TopMargin = 1000

    .BottomMargin = 1000

    .LeftMargin = .Width / fraction

    .RightMargin = .Width / fraction

    End With

    End Sub

    THUOC TINH BOUNDTEXT

    Private Sub DataCombo1_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)

    Dim recSource As String

    recSource = "SELECT * FROM Products " & " WHERE SupplierID = " & DataCombo1.BoundText

    Adodc1.RecordSource = recSource

    Adodc1.Refresh

    End Sub

    Private Sub Form_Load()

    ' S dng chung chui kt ni c s d liu cho hai iu khin

    ' ADO Data. Nu li pht sinh th kim tra ng dn ca tp

    ' tin Nwind.mdb.

    Dim ConnectionString As String

    ConnectionString = "Driver={Microsoft " &" Access Driver (*.mdb)};Dbq=C:\Program " &" Files\Microsoft Visual

    Studio\VB98\ " &" Nwind.mdb;Uid=;Pwd="

  • 7/30/2019 VISUAL BASIC 6 - HM V V D

    72/215

    ' Lu rng,c hai iu khin ADO Data,

    ' Recordsource phi s dng chungSupplierID.

    ' y l trng d liu thng thng c s dng

    ' bng cch thay i trong iu khin DataCombo

    ' khi recordset tr v bi ADODC1. ADODC2.

    With Adodc1

    .RecordSource = "SELECT ProductName, " & "ProductID, SupplierID FROM Products"

    .ConnectionString = ConnectionString

    .Refresh

    .Caption = "Products"

    .Visible = False

    End With

    With Adodc2

    .RecordSource = "SELECT CompanyName," & "SupplierID FROM Suppliers"

    .ConnectionString = ConnectionString

    .Refresh

    .Caption = "Suppliers"

    .Visible = False

    End With

    Set DataGrid1.DataSource = Adodc1

    With DataCombo1

    Set .DataSource = Adodc1

    .DataField = "SupplierID"

    ' Ct d liu thay i.

    .BoundColumn = "SupplierID"

    ' Ct d liu trong ADODC2 thay i DataField.

    Set .RowSource = Adodc2

    .ListField = "CompanyName"

    ' Ct d liu trnh by bng DataCombo.

    End With

    ' ng b gia iu khin DataGrid v DataCombo bng

    ' cch di chuyn n mu tin u tin ca ADODC1.

    Adodc1.Recordset.MoveFirst

    End Sub

    THUOC TINH BUTTONHEIGHT, BUTTONWIDTH

    Private Sub Form_Load()

    ' To bin i tng cho ImageList.

    Dim imgX As ListImage

  • 7/30/2019 VISUAL BASIC 6 - HM V V D

    73/215

    ' Np pictures vo iu khin ImageList

    Set imgX = ImageList1.ListImages. Add(, "open", LoadPicture("Graphics\open.bmp"))

    Set imgX = ImageList1.ListImages. Add(, "save", LoadPicture("Graphics\save.bmp"))

    Toolbar1.ImageList = ImageList1

    ' Create object variable for the Toolbar.

    Dim btnX As Button

    ' Thm i tng button vo Buttons collection bng cch

    ' s dng phng thc Add. Sau khi thm mi button, bn

    ' gn hai THUOC TINH Description v ToolTipText.

    Toolbar1.Buttons.Add , , , tbrSeparator

    Set btnX = Toolbar1.Buttons.Add(, "open", , tbrDefault, "open")

    btnX.ToolTipText = "Open File"

    btnX.Description = btnX.ToolTipText

    Set btnX = Toolbar1.Buttons.Add(, "save", , tbrDefault, "save")

    btnX.ToolTipText = "Save File"

    btnX.Description = btnX.ToolTipText

    Set btnX = Toolbar1.Buttons.Add(, , , tbrSeparator)

    ' Nt k tip c kiu Placeholder style.

    ' iu khin ComboBox nm trn u cant ny.

    Set btnX = Toolbar1.Buttons.Add(, "combo1", , tbrPlaceholder)

    btnX.Width = 1500

    Show

    ' Hin th formtip tc cu hnh iu khin ComboBox.

    ' Cu hnh iu khin ComboBox cng v tr vi

    ' i tng Button vi kiu PlaceHolder (key = "combo1").

    With Combo1

    .Width = Toolbar1.Buttons("combo1").Width

    .Top = Toolbar1.Buttons("combo1").Top

    .Left = Toolbar1.Buttons("combo1").Left

    .AddItem "Black"

    ' Thm mu vo cho text.

    .AddItem "Blue"

    .AddItem "Red"

    .ListIndex = 0

    End With

    End Sub

    Private Sub Form_Resize()

    ' Configure ComboBox control.

  • 7/30/2019 VISUAL BASIC 6 - HM V V D

    74/215

    With Combo1

    .Width = Toolbar1.Buttons("combo1").Width

    .Top = Toolbar1.Buttons("combo1").Top

    .Left = Toolbar1.Buttons("combo1").Left

    End With

    End Sub

    Private Sub toolbar1_ButtonClick(ByVal Button As Button)

    ' s dng THUOC TINH Key vi pht biu SelectCase ch nh

    hnh ng ca nt c chn

    Select Case Button.Key

    Case Is = "open"

    ' M tp tin.

    MsgBox "Add code to open file here!"

    Case Is = "save"

    ' Lu tp tin

    MsgBox "Add code to save f ile here!"

    End Select

    End Sub

    Private Sub Combo1_Click()

    ' Thay i mu nn ca form bng cch chn trong ComboBox.

    Select Case Combo1.ListIndex

    Case 0

    Form1.BackColor = vbBlack

    Case 1

    Form1.BackColor = vbBlue

    Case 2

    Form1.BackColor = vbRed

    End Select

    End Sub

    THUOC TINH CANGROW

    If MsgBox("Do you want to show abbreviated titles?",vbYesNo + vbDefaultButton2) = vbYes Then

    ptNwind.Sections("Products_Detail").Controls("txtProductName").CanGrow = False

    Else

    rptNwind.Sections("Products_Detail").Controls("txtProductName").CanGrow = True

    End If

    THUOC TINH CAPTION

    Private Sub Command1_Click()

    ' Kim tra Caption, sau thay i gi tr cho THUOC TINH ny

  • 7/30/2019 VISUAL BASIC 6 - HM V V D

    75/215

    If Command1.Caption = "Clicked" Then

    Command1.Caption = "OK"

    Else

    Command1.Caption = "Clicked"

    End If

    End Sub

    THUOC TINH CELLALIGNMENT

    Thm mt iu khin MSHFlexGrid vo Form

    Sub Form1_Load()

    MSHFlexGrid1.CellAlignment = flexAlignLeftCenter

    End Sub

    THUOC TINH CELLBACKCOLOR, CELLFORECOLOR

    Private Sub Form_Load()

    Timer1.Interval = 500

    MSHFlexGrid1.Text = "Focus Here"

    End Sub

    Private Sub Timer1_Timer()

    MSHFlexGrid1.CellBackColor = QBColor(Rnd * 15)

    MSHFlexGrid1.CellForeColor = QBColor(Rnd * 10)

    End Sub

    THUOC TINH CELLFONTBOLD

    Private Sub Form_Load()

    Timer1.Interval = 500

    MSHFlexGrid1.Text = "Focus Here"

    End Sub

    Private Sub Timer1_Timer()

    MSHFlexGrid1.CellFontBold = 1

    MSHFlexGrid1.CellBackColor = QBColor(Rnd * 15)

    MSHFlexGrid1.CellForeColor = QBColor(Rnd * 10)

    End Sub

    THUOC TINH CELLFONTITALIC

    Private Sub Form_Load()

    Timer1.Interval = 500

    MSHFlexGrid1.Text = "Focus Here"

    End Sub

    Private Sub Timer1_Timer()

    MSHFlexGrid1.CellFontItalic = 1

    MSHFlexGrid1.CellBackColor = QBColor(Rnd * 15)

  • 7/30/2019 VISUAL BASIC 6 - HM V V D

    76/215

    MSHFlexGrid1.CellForeColor = QBColor(Rnd * 10)

    End Sub

    THUOC TINH CELLFONTNAME

    Private Sub Form_Load()

    Timer1.Interval = 500

    MSHFlexGrid1.Text = "Focus Here"

    End Sub

    Private Sub Timer1_Timer()

    MSHFlexGrid1.CellFontBold = 1

    MSHFlexGrid1.CellFontName = Screen.Fonts(3)

    MSHFlexGrid1.Text = Screen.Fonts(3)

    ' Hin th Font Name

    MSHFlexGrid1.CellBackColor = QBColor(Rnd * 15)

    MSHFlexGrid1.CellForeColor = QBColor(Rnd * 10)

    End Sub

    THUOC TINH CELLFONTSIZE

    Private Sub Form_Load()

    Timer1.Interval = 500

    MSHFlexGrid1.Text = "Focus Here"

    End Sub

    Private Sub Timer1_Timer()

    MSHFlexGrid1.CellFontItalic = 1

    MSHFlexGrid1.CellFontName = Screen.Fonts(3)

    MSHFlexGrid1.Text = Screen.Fonts(3)

    MSHFlexGrid1.CellFontSize = 12

    MSHFlexGrid