You are on page 1of 56

TOP

Cc hm s p x p theo ABC
A H O V B I P W C J Q D K R Y E L S F M T G N U

Tn hm: Abs M t : Abs (number) L y tr tuy t i c a s Number Tham s : Number S c n tnh V d : Option Explicit Private Sub Form_Load() Dim MyNumber MyNumber = Abs(50.3)' Returns 50.3. MyNumber = Abs(-50.3)' Returns 50.3. End Sub .................................. Return Top ................................... Tn hm: AppActivate M t : AppActivate title [, wait] G i m t c a s ang ch y activate (N u c a s cha c m s x y ra l i) Tham s : title Tiu c a c a s wait Th i gian ch V d : Option Explicit Private Sub Form_Load() Dim MyAppID, ReturnValue AppActivate "Microsoft Word"' Activate Microsoft ' Ham AppActivate co the su dung gia tri do hm shell tra ve MyAppID = Shell("C:\WORD\WINWORD.EXE", 1)' Run Microsoft Word. AppActivate MyAppID' Activate Microsoft ' Word. ReturnValue = Shell("c:\EXCEL\EXCEL.EXE", 1)' Run Microsoft Excel. AppActivate ReturnValue' Activate Microsoft ' Excel. End Sub Tham kh o thm: SendKeys Shell .................................. Return Top ..................................... Tn hm: Array M t : Array(arglist) Gn cc gi tr trong arglist vo m t m ng. Tham s : arglist : ch a gi tr c a c n t o m ng, cc gi tr cch nhau b ng d u ph y (,). Gi tr u s c gn cho ch s index u, gi tr th 2 gn cho ch s index 2...

V d : Dim MyWeek, MyDay MyWeek = Array("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun") ' Return values assume lower bound set to 1 (using Option Base ' statement). MyDay = MyWeek(2)' MyDay contains "Tue". MyDay = MyWeek(4)' MyDay contains "Thu". .................................. Return Top ..................................... Tn hm: Asc M t : Asc (string) Chuy n k t thnh m Ascii Tham s : string Chu i c n chuy n V d : Option Explicit Private Sub Form_Load() Dim MyNumber MyNumber = Asc("A")' Returns 65. MyNumber = Asc("a")' Returns 97. MyNumber = Asc("Apple")' Returns 65. End Sub Tham kh o thm: Chr Return Top ..................................... .................................. Tn hm: AscW M t : AscW (string) Chuy n k t thnh m Ascii (h tr Unicode) Tham s : string Chu i c n chuy n V d : Option Explicit Private Sub Form_Load() Dim MyNumber MyNumber = AscW("A")' Returns 65. MyNumber = AscW("a")' Returns 97. MyNumber = AscW("Apple")' Returns 65. End Sub Tham kh o thm: ChrW .................................. Return Top .................................... Tn hm: Atn M t : Atn (number) L y Arctang c a s Number Tham s : Number S c n tnh V d : Option Explicit Private Sub Form_Load() Dim Pi Pi = 4 * Atn(1)' Tinh gia tri cua hang so Pi End Sub Tham kh o thm: Cos Sin Sqr Tan .................................. Return Top ....................................

Tn hm: Beep M t : Beep Pht ra ti ng Beep t loa c a h th ng V d : Option Explicit Private Sub Form_Load() Dim I 'Phat ra ba tieng beep lien tuc For I = 1 To 3 Beep Next I End Sub .................................. Return Top .................................... Tn hm: Call M t : [Call] name [argumentlist] Th c thi 1 sub, function hay 1 DLL procedure. T kha Call th ty ch n, nhng n u n c s d ng th b n ph i t argumentlist trong d u ng m ngo c () v nn c truy n i s . Tham s : name : tn th t c c n g i. argumentlist : danh sch cc i s truy n vo (ty ch n). V d : ' Call a Sub procedure. Call PrintToDebugWindow("Hello World") ' The above statement causes control to be passed to the following ' Sub procedure. Sub PrintToDebugWindow(AnyString) Debug.Print AnyString' Print to the Immediate window. End Sub ' Call an intrinsic function. The return value of the function is ' discarded. Call Shell(AppName, 1)' AppName contains the path of the ' executable file. ' Call a Microsoft Windows DLL procedure. The Declare statement must be ' Private in a Class Module, but not in a standard Module. Private Declare Sub MessageBeep Lib "User" (ByVal N As Integer) Sub CallMyDll() Call MessageBeep(0)' Call Windows DLL procedure. MessageBeep 0' Call again without Call keyword. End Sub .................................. Return Top .................................... Tn hm: CallByName M t : CallByName (object, procname, calltype[, args()]) i u khi n m t i t ng thng qua cc thu c tnh c a n Tham s : object Tn i t ng procname Tn thu c tnh calltype Phng th c giao ti p args() Gi tr m ng

V d : Option Explicit Private Sub Form_Load() Dim Result ' Gan thuoc tinh MousePointer cua Text1 = vbCrosshair CallByName Text1, "MousePointer", VbLet, vbCrosshair 'Lay thuoc tinh MousePointer hien tai cua Text1 Result = CallByName(Text1, "MousePointer", VbGet) 'Di chuyen Text1 den vi tri 100, 100 CallByName Text1, "Move", VbMethod, 100, 100 End Sub .................................. Return Top .................................... Tn hm: CBool M t : Cbool (expression) Chuy n i bi u th c expression sang ki u Boolean Tham s : expression Bi u th c c n chuy n i V d : Option Explicit Private Sub Form_Load() Dim A, B Dim Check As Boolean A = 5: B = 5'Khoi tao bien Check = CBool(A = B)' Returns True A = 0' Define variable. Check = CBool(A)' Returns False End Sub .................................. Return Top .................................... Tn hm: CByte M t : Cbyte (expression) Chuy n i bi u th c expression sang ki u Byte Tham s : expression Bi u th c c n chuy n i V d : Option Explicit Private Sub Form_Load() Dim MyDouble, MyByte MyDouble = 125.5678' khoi tao MyByte = CByte(MyDouble)' MyByte = 126. End Sub .................................. Return Top ..................................... Tn hm: CCur M t : Ccur (expression) Chuy n i bi u th c expression sang ki u Currency Tham s : expression Bi u th c c n chuy n i V d : Option Explicit Private Sub Form_Load() Dim MyDouble, MyCurr MyDouble = 543.214588' Khoi tao MyCurr = CCur(MyDouble * 2)' Chuyen doi MyDouble * 2 ' Returns (1086.429176) to a ' Returns Currency (1086.4292). End Sub .................................. Return Top .....................................

Tn hm: CDate M t : Cdate (expression) Chuy n i bi u th c expression sang ki u Date Tham s : expression Bi u th c c n chuy n i V d : Option Explicit Private Sub Form_Load() Dim MyDate, MyShortDate, MyTime, MyShortTime MyDate = "August 10, 2003"' Khoi tao MyShortDate = CDate(MyDate)'Returns 8/10/1003 MyTime = "4:35:47 PM"' Khoi tao MyShortTime = CDate(MyTime)'Returns 4:35:47 PM End Sub Return Top ..................................... .................................. Tn hm: CDbl M t : CDbl (expression) Chuy n i bi u th c expression sang ki u Double Tham s : expression Bi u th c c n chuy n i V d : Option Explicit Private Sub Form_Load() Dim MyCurr, MyDouble MyCurr = CCur(234.456784) MyDouble = CDbl(MyCurr * 8.2 * 0.01) End Sub .................................. Return Top .................................... Tn hm: CDec M t : Cdec (expression) Chuy n i bi u th c expression sang ki u Decimal Tham s : expression Bi u th c c n chuy n i V d : Option Explicit Private Sub Form_Load() Dim MyDecimal, MyCurr MyCurr = 10000000.0587 MyDecimal = CDec(MyCurr)' Returns 10000000.0587 End Sub .................................. Return Top ..................................... Tn hm: ChDir M t : ChDir Path Thay i ng d n c a cc th m c hi n hnh Tham s : Path ng d n mu n n V d : Option Explicit Private Sub Form_Load() ChDir "D:\MyPicture" End Sub Tham kh o thm: ChDrive

CurDir Dir Return Top .................................. .................................... Tn hm: ChDrive M t : ChDirve Drive Thay i a hi n hnh Tham s : Drive a mu n n V d : Option Explicit Private Sub Form_Load() ChDrive "D:\" End Sub Tham kh o thm: ChDir CurDir Dir Return Top ..................................... .................................. Tn hm: Choose M t : Choose(index, choice-1[, choice-2,... [, choice-n]]) Ch n m t gi tr trong dy gi tr cho tr c ham s : index Vi tr gi tr mu n ch n choice-1[, choice-2, ... [, choice-n]] Dy gi tr ban u V d : Option Explicit Private Sub Form_Load() Dim Result Result = Choose(1, "Speedy", "United", "Federal")'Returns Speedy Result = Choose(3, "Speedy", "United", "Federal")'Returns Federal End Sub Tham kh o thm: IIf Switch Return Top ..................................... .................................. Tn hm: Chr M t : Chr(charcode) Chuy n m Ascii thnh k t Tham s : charcode M c n chuy n V d : Option Explicit Private Sub Form_Load() Dim MyChar MyChar = Chr(65)' Returns A. MyChar = Chr(97)' Returns a. MyChar = Chr(62)' Returns >. MyChar = Chr(37)' Returns %. End Sub Tham kh o thm: Asc .................................... Return Top ..................................

Tn hm: ChrW M t : ChrW(charcode) Chuy n m Ascii thnh k t (H tr Unicode) Tham s : charcode M c n chuy n V d : Option Explicit Private Sub Form_Load() Dim MyChar MyChar = ChrW(65)' Returns A. MyChar = ChrW(97)' Returns a. MyChar = ChrW(62)' Returns >. MyChar = ChrW(37)' Returns %. End Sub Tham kh o thm: AscW Return Top ..................................... .................................. Tn hm: CInt M t : Cint (expression) Chuy n i bi u th c expression sang ki u Integer Tham s : expression Bi u th c c n chuy n i V d : Option Explicit Private Sub Form_Load() Dim MyDouble, MyInt MyDouble = 2345.5678' Khoi tao MyInt = CInt(MyDouble)' Returns 2346. End Sub .................................. Return Top ..................................... Tn hm: CLng M t : CLng (expression) Chuy n i bi u th c expression sang ki u Long Tham s : expression Bi u th c c n chuy n i V d : Option Explicit Private Sub Form_Load() Dim MyVal1, MyVal2, MyLong1, MyLong2 MyVal1 = 25427.45: MyVal2 = 25427.55' Khoi tao MyLong1 = CLng(MyVal1)'Returns 25427. MyLong2 = CLng(MyVal2)'Returns 25428. End Sub .................................. Return Top ..................................... Tn hm: Close M t : Close (#Filenum)' ng t p tin c m b ng l nh Open. Tham s : #Filenum : ch s c a t p tin. V d : Option Explicit Type Record ID As Integer Name As String * 20 End Type Private Sub Form_Load()

Dim MyRecord As Record Open "TESTFILE" For Random As #1 Len = Len(MyRecord)' Mo file Do While Not EOF(1)' Lap cho den khi ket thuc file Get #1, , MyRecord' Doc mau tin Debug.Print Seek(1)' In so mau tin 'Seek #1, 3 'Tr t i v tr m u tin th 3 Loop Close #1' Dong file. End Sub Tham kh o thm: Open Return Top ..................................... .................................. Tn hm: Command M t : Command L y i s truy n t dng l nh Ghi ch: V d n u b n c ng d ng tn MyApp th khi b n ch y: MyApp.exe caulacbovb ---> i s b n nh n c l: caulacbovb V d : Function GetCommandLine(Optional MaxArgs) 'Declare variables. Dim C, CmdLine, CmdLnLen, InArg, I, NumArgs 'See if MaxArgs was provided. If IsMissing(MaxArgs) Then MaxArgs = 10 'Make array of the correct size. ReDim ArgArray(MaxArgs) NumArgs = 0: InArg = False 'Get command line arguments. CmdLine = Command() CmdLnLen = Len(CmdLine) 'Go thru command line one character 'at a time. For I = 1 To CmdLnLen C = Mid(CmdLine, I, 1) 'Test for space or tab. If (C <> " " And C <> vbTab) Then 'Neither space nor tab. 'Test if already in argument. If Not InArg Then 'New argument begins. 'Test for too many arguments. If NumArgs = MaxArgs Then Exit For NumArgs = NumArgs + 1 InArg = True End If 'Concatenate character to current argument. ArgArray(NumArgs) = ArgArray(NumArgs) & C Else 'Found a space or tab. 'Set InArg flag to False. InArg = False End If Next I 'Resize array just enough to hold arguments. ReDim Preserve ArgArray(NumArgs) 'Return Array in Function name. GetCommandLine = ArgArray() End Function .................................. Return Top .....................................

Tn hm: Const M t : [Public | Private] Const constname [As type] = expression Khai bo 1 h ng s . T kha [Public | Private] ty ch n dng nh ngha ph m vi s d ng c a h ng s Tham s : constname : tn h ng s . [As type] : ki u d li u (ty ch n). N u khng khai bo s l y ki u d li u ph h p v i gi tr hi n c. expression : gi tr miu t . V d : ' Constants are Private by default. Const MyVar = 459 Const Pi = 3.14 ' Declare Public constant. Public Const MyString = "HELP" ' Declare Private Integer constant. Private Const MyInt As Integer = 5 ' Declare multiple constants on same line. Const MyStr = "Hello", MyDouble As Double = 3.4567
Return Top .................................... .................................. Tn hm: Cos M t : Cos (number) L y Cos c a s Number Tham s : Number S c n tnh V d : Option Explicit Private Sub Form_Load() Dim MyAngle, MySecant MyAngle = 1.3' Khoi tao MySecant = 1 / Cos(MyAngle)' Tinh secant End Sub Tham kh o thm: Atn Sin Sqr Tan Return Top.................................... .................................. Tn hm: CSng M t : CSng (expression) Chuy n i bi u th c expression sang ki u Single Tham s : expression Bi u th c c n chuy n i V d : Option Explicit Private Sub Form_Load() Dim MyDouble1, MyDouble2, MySingle1, MySingle2 MyDouble1 = 75.3421115: MyDouble2 = 75.3421555' khoi tao MySingle1 = CSng(MyDouble1)' Returns 75.34211. MySingle2 = CSng(MyDouble2)' Returns 75.34216. End Sub .................................. Return Top ....................................

Tn hm: CStr M t : CStr (expression) Chuy n i bi u th c expression sang ki u String Tham s : expression Bi u th c c n chuy n i V d : Option Explicit Private Sub Form_Load() Dim MyDouble, MyString MyDouble = 437.324' Khoi tao MyString = CStr(MyDouble)' Returns "437.324". End Sub Return Top ..................................... .................................. Tn hm: CurDir M t : CurDir ([Drive]) Xem ng d n hi n t i trn m t a Tham s : Drive a mu n xem V d : Option Explicit Private Sub Form_Load() Dim MyPath ' Gia su duong dan hien tai tren o C la "C:\WINDOWS\SYSTEM" . ' Gia su duong dan hien tai tren o D la "D:\EXCEL". ' Gia su o dia hien hanh la o C MyPath = CurDir' Returns "C:\WINDOWS\SYSTEM". MyPath = CurDir("C")' Returns "C:\WINDOWS\SYSTEM". MyPath = CurDir("D")' Returns "D:\EXCEL". End Sub Tham kh o thm: ChDir ChDrive Dir Return Top .................................. .................................... Tn hm: CVar M t : Cvar (expression) Chuy n i bi u th c expression sang ki u Variant Tham s : expression Bi u th c c n chuy n i V d : Option Explicit Private Sub Form_Load() Dim MyInt, MyVar MyInt = 4534' MyInt is an Integer. MyVar = CVar(MyInt & "000")' MyVar contains the string 4534000. End Sub .................................. Return Top .................................... Tn hm: CVErr M t : CVErr(errornumber) Tr v con s ki u l i nh ngha b i ng i dng. Tham s : errornumber : s l i. V d : ' Call CalculateDouble with an error-producing argument. Sub Test()

Debug.Print CalculateDouble("345.45robert") End Sub ' Define CalculateDouble Function procedure. Function CalculateDouble(Number) If IsNumeric(Number) Then CalculateDouble = Number * 2' Return result. Else CalculateDouble = CVErr(2001)' Return a user-defined error End If' number. End Function Return Top .................................. .................................... Tn hm: Date M t : Hm: Date Tr v ngy hi n t i c a h th ng. L nh: Date = date t l i ngy hi n t i cho h th ng. V d : 'L y ngy hi n t i c a h th ng: Dim MyDate MyDate = Date' MyDate contains the current system date. ' t l i ngy hi n t i cho h th ng: Dim MyDate MyDate = #February 12, 1985#' Assign a date. Date = MyDate' Change system date. Return Top ..................................... .................................. Tn hm: DateAdd M t : DateAdd(interval, number, date) Thm th i gian Tham s : interval n v th i gian number Th i gian thm vo date Th i gian c n thm vo Ghi ch: Dng cho tham s interval yyyy Year q Quarter m Month y Day of year d Day w Weekday ww Week h Hour n Minute s Second V d : Option Explicit Private Sub Form_Load() Dim FirstDate As Date Dim IntervalType As String

Dim Number As Integer Dim Msg IntervalType = "m"' "m" gan don vi la thang FirstDate = InputBox("Nhap vao thoi gian")' Vi du 08/10/2003 Number = InputBox("Nhap vao so thang ban muon them")'3 ' Returns 11/10/2003 Msg = "New date: " & DateAdd(IntervalType, Number, FirstDate) MsgBox Msg End Sub Return Top .................................. .................................... Tn hm: DateDiff M t : DateDiff(interval, date1, date2[, firstdayofweek[, firstweekofyear]]) Tnh kho ng th i gian gi a date1 v date2 Tham s : interval n v th i gian date1 Th i i m 1 date2 Th i i m 2 firstdayofweek Ngy u tin trong tu n firstweekofyear Tu n u tin trong nm Ghi ch: Dng cho tham s interval yyyy Year q Quarter m Month y Day of year d Day w Weekday ww Week h Hour n Minute s Second Dng cho tham s firstdayofweek vbUseSystem = 0 S d ng NLS API setting. VbSunday = 1 Sunday (default) vbMonday = 2 Monday vbTuesday = 3 Tuesday vbWednesday = 4 Wednesday vbThursday = 5 Thursday vbFriday = 6 Friday vbSaturday = 7 Saturday Dng cho tham s firstweekofyear

vbUseSystem 0 S d ng NLS API setting. vbFirstJan1 = 1 B t u t i tu n th nh t trong thng m t (default) vbFirstFourDays = 2 B t u t i tu n th nh t v c t nh t 4 ngy trong nm vbFirstFullWeek = 3 B t u v i c tu n th nh t trong nm V d : Option Explicit Private Sub Form_Load() Dim TheDate As Date Dim Msg TheDate = InputBox("Enter a date") Msg = "Days from today: " & DateDiff("d", Now, TheDate) MsgBox Msg End Sub Tham kh o thm: DatePart Return Top ..................................... .................................. Tn hm: DatePart M t : DatePart (interval, date [,firstdayofweek [, firstweekofyear]]) Rt b t k thng tin ngy thng v th i gian t gi tr date Tham s : interval n v th i gian date Th i gian firstdayofweek Ngy u tin trong tu n firstweekofyear Tu n u tin trong nm Ghi ch: Dng cho tham s interval yyyy Year q Quarter m Month y Day of year d Day w Weekday ww Week h Hour n Minute s Second Dng cho tham s firstdayofweek vbUseSystem = 0 S d ng NLS API setting. VbSunday = 1 Sunday (default) vbMonday = 2 Monday vbTuesday = 3 Tuesday vbWednesday = 4

Wednesday vbThursday = 5 Thursday vbFriday = 6 Friday vbSaturday = 7 Saturday Dng cho tham s firstweekofyear vbUseSystem 0 S d ng NLS API setting. vbFirstJan1 = 1 B t u t i tu n th nh t trong thng m t (default) vbFirstFourDays = 2 B t u t i tu n th nh t v c t nh t 4 ngy trong nm vbFirstFullWeek = 3 B t u v i c tu n th nh t trong nm V d : Option Explicit Private Sub Form_Load() Dim TheDate As Date Dim Msg TheDate = InputBox("Enter a date:")' 08/10/2003 Msg = "Quarter: " & DatePart("d", TheDate)' Returns 10 Msg = "Quarter: " & DatePart("m", TheDate)' Returns 08 MsgBox Msg End Sub Tham kh o thm: DateDiff Return Top .................................... .................................. Tn hm: DateSerial M t : DateSerial (year, month, day) Chuy n cc thng s r i r c c a ngy thng thnh m t chu i ngy thng Tham s : year Nm month Thng day Ngy V d : Option Explicit Private Sub Form_Load() Dim MyDate MyDate = DateSerial(2003, 8, 10)' Return 8/10/2003 End Sub Tham kh o thm: TimeSerial Return Top .................................. .................................... Tn hm: DateValue M t : DateValue(date) Chuy n ngy thng d ng chu i sang gi tr Tham s : date Ngy thng c n chuy n V d : Option Explicit Private Sub Form_Load() Dim MyDate MyDate = DateValue("August 10, 2003")' Return 8/10/2003 End Sub

Tham kh o thm: TimeValue Return Top ..................................... .................................. Tn hm: Day M t : Day(date) L y thng tin ngy t chu i date Tham s : date Ngy thng V d : Option Explicit Private Sub Form_Load() Dim MyDate, MyDay MyDate = #8/10/2003#' Khoi tao MyDay = Day(MyDate)' Returns 10 End Sub Tham kh o thm: Month Year Return Top .................................. .................................... Tn hm: Declare M t : [Public | Private] Declare Sub name Lib "libname" [Alias "aliasname"] [([arglist])] hay [Public | Private] Declare Function name Lib "libname" [Alias "aliasname"] [([arglist])] [As type] Khai bo tham chi u t i 1 th t c Sub hay Function trong 1 file DLL (dynamic-link library). T kha [Public | Private] ty ch n ch nh ph m vi s d ng. Tham s : name : tn Sub hay Function c n g i. libname : ch r file DLL c ch a Sub hay Function c g i. aliasname : ch r b danh c a Sub hay Function trong file DLL. arglist : danh sch cc i s . As type : ki u d li u tr v c a Function. Ghi ch: N u s d ng Function b n ph i ch r ki u d li u tr v . Pht bi u Declare ch c s d ng c p module. V d : ' In Microsoft Windows (16-bit): Declare Sub MessageBeep Lib "User" (ByVal N As Integer) ' Assume SomeBeep is an alias for the procedure name. Declare Sub MessageBeep Lib "User" Alias "SomeBeep"(ByVal N As Integer) ' Use an ordinal in the Alias clause to call GetWinFlags. Declare Function GetWinFlags Lib "Kernel" Alias "#132"() As Long ' In 32-bit Microsoft Windows systems, specify the library USER32.DLL, ' rather than USER.DLL. You can use conditional compilation to write ' code that can run on either Win32 or Win16. #If Win32 Then Declare Sub MessageBeep Lib "User32" (ByVal N As Long) #Else Declare Sub MessageBeep Lib "User" (ByVal N As Integer) #End If .................................. Return Top ..................................... Tn hm: DeleteSetting M t : DeleteSetting appname, section [, key] Xa registry Tham s : appname Tn registry section

Tn section key Tn key V d : Option Explicit Private Sub Form_Load() ' luu mot thong tin registry moi SaveSetting appname:="MyApp", section:="Startup", _ Key:="Top", setting:=75 SaveSetting "MyApp", "Startup", "Left", 50 ' Xoa key Startup trong MyApp DeleteSetting "MyApp", "Startup" End Sub Tham kh o thm: GetAllSettings GetSetting SaveSetting Return Top ..................................... .................................. Tn hm: Dir M t : Dir [(pathname [, attributes])] Tham s : pathname ng d n mu n ki m tra attributes Thu c tch c a file hay folder mu n ki m tra Ghi ch: Dng cho tham s attributes vbNormal = 0 Khng quan tm n thu c tnh c a cc h s vbReadOnly = 1 Ch quan tm n cc h s c thu c tnh ch c vbHidden = 2 Ch quan tm n cc h s c thu c tnh n vbVolume = 8 Ch quan tm n cc h s khng c thu c tnh vbDirectory 16 Ch quan tm n cc th m c V d : Option Explicit Private Sub Form_Load() Dim MyFile, MyPath, MyName ' Myfile = WIN.INI neu file do co ton tai MyFile = Dir("C:\WINDOWS\WIN.INI") ' Myfile = file dau tien trong thu muc C:Windows\ co duoi la INI MyFile = Dir("C:\WINDOWS\*.INI") ' Goi lai viec kiem tra MyFile = Dir ' Myfile = file dau tien trong thu muc C:Windows\ co duoi la TXT va co thuoc tinh an MyFile = Dir("*.TXT", vbHidden) MyPath = "c:\"' Gan MyPath = "C:\" MyName = Dir(MyPath, vbDirectory)' Gan MyName= ten thu muc dau tien trong MyPath Do While MyName <> ""' Bat dau vong lap 'Bo qua cac thu muc hien tai va thu muc xung quanh If MyName <> "." And MyName <> ".." Then ' Su dung su so sanh phan theo Bit de chac chac MyName la mot thu muc If (GetAttr(MyPath & MyName) And vbDirectory) = vbDirectory Then Debug.Print MyName End If End If MyName = Dir' Nhay den thu muc ke tiep Loop End Sub

Tham kh o thm: ChDir ChDrive CurDir Return Top .................................. .................................... Tn hm: Environ M t : Environ({envstring | number}) Tm ng d n environment c a h th ng Tham s : envstring | number Tn c a environment V d : Option Explicit Private Sub Form_Load() Dim EnvString, Indx, Msg, PathLen' Declare variables. Indx = 1 Do EnvString = Environ(Indx)' Lay ten environment ' Kiem tra duong dan co ton tai khong If Left(EnvString, 5) = "PATH=" Then PathLen = Len(Environ("PATH"))' lay chieu dai duong dan Msg = "PATH entry = " & Indx & " and length = " & PathLen Exit Do Else Indx = Indx + 1' Tiep tu voi cac environment khac End If Loop Until EnvString = "" If PathLen > 0 Then MsgBox Msg Else MsgBox "Khong co Path environment nao ton tai." End If End Sub .................................. Return Top ..................................... Tn hm: EOF M t : EOF(filenumber) Ki m tra xem file k t thc cha Tham s : filenumber Tn file (d ng s #n) V d : Option Explicit Private Sub Form_Load() Dim InputData Open "MYFILE" For Input As #1' Mo file de doc Do While Not EOF(1)' Kiem tra xem file da ket thuc chua Line Input #1, InputData' Doc tung dong Debug.Print InputData' in ra tung dong Loop Close #1' Dong file End Sub Tham kh o thm: Loc LOF ..................................

Return Top

....................................

Tn hm: Exp M t : Exp(number) Hm m Tham s : Number S c n tnh V d : Option Explicit Private Sub Form_Load() Dim MyAngle, MyHSin MyAngle = 1.3'Khoi tao ' Tinh hyperbolic sine. MyHSin = (Exp(MyAngle) - Exp(-1 * MyAngle)) / 2 End Sub Tham kh o thm: Log Return Top ..................................... .................................. Tn hm: FileAttr M t : FileAttr(filenumber, returntype) Xem thu c tnh c a file ang m Tham s : filenumber Tn file (d ng s #n) returntype Lo i gi tr tr v Ghi ch: ********Gi tr tr v ********* Input = 1 Nh p Output = 2 Xu t Random = 4 Ng u nhin Append = 8 Thm vo Binary = 32 Nh phn V d : Option Explicit Private Sub Form_Load() Dim FileNum, Mode, Handle FileNum = 1' Khoi tao Open "TESTFILE" For Append As FileNum' Mo File Mode = FileAttr(FileNum, 1)' Returns 8 (Append file) Handle = FileAttr(FileNum, 2)' Returns file handle. Close FileNum' Close file. End Sub .................................. Return Top .................................... Tn hm: FileCopy M t : FileCopy source, destination Copy file t th m c ny sang th m c khc Tham s : source ng d n ngu n destination ng d n ch V d : Option Explicit Private Sub Form_Load() FileCopy "D:\MyFile.txt", "C:\MyFile.txt"

End Sub Tham kh o thm: FileLen Kill Return Top .................................. .................................... Tn hm: FileDateTime M t : FileDateTime (pathname) Tr v chu i ngy gi m File c t o ho c ngy gi ch nh s a cu i cng Tham s : pathname ng d n file mu n ki m tra V d : Option Explicit Private Sub Form_Load() Dim MyStamp ' Gia su C:\TestFile.txt duoc chinh sua lan cuoi vao ngay 01/08/2003 4:10:45 PM MyStamp = FileDateTime("C:\TestFile.txt")' MyStamp = "01/08/2003 4:10:45 PM" End Sub Return Top .................................. .................................... Tn hm: FileLen M t : FileLen (pathname) Tra ve so Byte cua File Tham s : pathname ng d n file mu n l y kch th c V d : Option Explicit Private Sub Form_Load() FileLen "D:\MyFile.txt"'Return so byte cua MyFile.txt End Sub Tham kh o thm: FileCopy Kill Return Top .................................... .................................. Tn hm: Filter M t : Filter (sourcearray, match [, include [, compare]]) L c m ng sourcesrray v i gi tr l c l match Tham s : sourcearray M ng c n l c match Gi tr l c include L c o (True ho c False) compare Ch r ki u d li u so snh trong qu trnh l c Ghi ch: Dng cho tham s compare vbUseCompareOption = 1 Ch ty ch n, VB s t ng l a l ai d li u thch h p vbBinaryCompare = 0 So snh nh phn vbTextCompare = 1 So snh chu i vbDatabaseCompare = 2 So snh d li u V d : Option Explicit Private Sub Form_Load() Dim selNames() As String

Dim Names(1 To 5) As String Names(1) = "A" Names(2) = "B" Names(3) = "C" Names(4) = "D" Names(5) = "E" selNames = Filter(Names, "A")' Returns "A" selNames = Filter(Names, "B", False)'Returns "A" , "C", "D", "E" End Sub Return Top .................................. .................................... Tn hm: Fix M t : Fix (number) L y ph n nguyn c a s Number Tham s : Number S c n tnh V d : Option Explicit Private Sub Form_Load() Dim MyNumber MyNumber = Int(99.8)' Returns 99. MyNumber = Fix(99.2)' Returns 99. MyNumber = Int(-99.8)' Returns -100. MyNumber = Fix(-99.8)' Returns -99. MyNumber = Int(-99.2)' Returns -100. MyNumber = Fix(-99.2)' Returns -99. End Sub Tham kh o thm: Int Round Return Top .................................... .................................. Tn hm: Format M t : Format (expression [, format [, firstdayofweek [, firstweekofyear]]]) Chuy n bi u th c expression theo nh d ng mong mu n Tham s : expression Bi u th c c n nh d ng format Ki u nh d ng firstdayofweek Ngy u tin trong tu n firstweekofyear Tu n u tin trong nm Ghi ch: Dng cho tham s firstdayofweek vbUseSystem = 0 S d ng NLS API setting. VbSunday = 1 Sunday (default) vbMonday = 2 Monday vbTuesday = 3 Tuesday vbWednesday = 4 Wednesday vbThursday = 5 Thursday vbFriday = 6 Friday vbSaturday = 7 Saturday Dng cho tham s firstweekofyear

vbUseSystem 0 S d ng NLS API setting. vbFirstJan1 = 1 B t u t i tu n th nh t trong thng m t (default) vbFirstFourDays = 2 B t u t i tu n th nh t v c t nh t 4 ngy trong nm vbFirstFullWeek = 3 B t u v i c tu n th nh t trong nm V d : Option Explicit Private Sub Form_Load() Dim MyTime, MyDate, MyStr MyTime = #5:04:23 PM# MyDate = #August 10, 2003# ' Tra ve gio hien tai theo format Long Time MyStr = Format(Time, "Long Time") ' Tra ve ngay hien tai theo format Long Date MyStr = Format(Date, "Long Date") MyStr = Format(MyTime, "h:m:s")' Returns "17:4:23". MyStr = Format(MyTime, "hh:mm:ss AMPM")' Returns "05:04:23 PM". MyStr = Format(MyDate, "dddd, mmm d yyyy")' Returns "Sunday, Aug 10 2003". MyStr = Format(23)' Returns "23". ' Mot so dinh dang theo nguoi dung MyStr = Format(5459.4, "##,##0.00")' Returns "5,459.40". MyStr = Format(334.9, "###0.00")' Returns "334.90". MyStr = Format(5, "0.00%")' Returns "500.00%". MyStr = Format("HELLO", "<")' Returns "hello". MyStr = Format("This is it", ">")' Returns "THIS IS IT End Sub Tham kh o thm: Time Date Year Return Top .................................... .................................. Tn hm: FormatCurrency M t : FormatCurrency (Expression [,NumDigitsAfterDecimal [,IncludeLeadingDigit [,UseParensForNegativeNumbers [,GroupDigits]]]]) Chuy n bi u th c expression theo nh d ng Currency Tham s : Expression Bi u th c c n nh d ng NumDigitsAfterDecimal S s l sau d u ph y IncludeLeadingDigit Tham s ph khng c n thi t UseParensForNegativeNumbers Tham s ph khng c n thi t GroupDigits Tham s ph khng c n thi t V d : Option Explicit Private Sub Form_Load() Dim MyStr MyStr = FormatCurrency("12345", 5)'Returns $12,345.00000 MyStr = FormatCurrency("12.345", 5)'Returns $12.34500 MyStr = FormatCurrency("12.345", 3)'Returns $12.345 End Sub Tham kh o thm: Str Val Return Top.................................... .................................. Tn hm: FormatDateTime M t :

FormatDateTime(Date[,NamedFormat]) Tham s : Date Ngy gi c n nh d ng NamedFormat Ki u nh d ng Ghi ch: Dng cho tham s NamedFormat vbGeneralDate = 0 GeneralDate vbLongDate = 1 LongDate vbShortDate = 2 ShortDate vbLongTime = 3 LongTime vbShortTime = 4 ShortTime V d : Option Explicit Private Sub Form_Load() Dim MyTime, MyDate, MyStr MyTime = #3:08:56 PM# MyDate = #8/10/2003# MyStr = FormatDateTime(MyTime, vbShortTime)'Returns "15:08" MyStr = FormatDateTime(MyDate, vbLongDate)'Returns "Tueday, August 10, 2003" End Sub Tham kh o thm: Date Time Year Return Top .................................... .................................. Tn hm: FormatNumber M t : FormatNumber (Expression [,NumDigitsAfterDecimal [,IncludeLeadingDigit [,UseParensForNegativeNumbers [,GroupDigits]]]]) Chuy n bi u th c expression theo nh d ng Number Tham s : Expression Bi u th c c n nh d ng NumDigitsAfterDecimal S s l sau d u ph y IncludeLeadingDigit Tham s ph khng c n thi t UseParensForNegativeNumbers Tham s ph khng c n thi t GroupDigits Tham s ph khng c n thi t V d : Option Explicit Private Sub Form_Load() Dim MyStr MyStr = FormatNumber("12345", 5)'Returns 12,345.00000 MyStr = FormatNumber("12.345", 5)'Returns 12.34500 MyStr = FormatNumber("12.345", 3)'Returns 12.345 End Sub Tham kh o thm: Str Val Return Top.................................... .................................. Tn hm: FormatPercent M t : FormatPercent(Expression[,NumDigitsAfterDecimal [,IncludeLeadingDigit [,UseParensForNegativeNumbers [,GroupDigits]]]])

Chuy n bi u th c expression theo nh d ng Percent Tham s : Expression Bi u th c c n nh d ng NumDigitsAfterDecimal S s l sau d u ph y IncludeLeadingDigit Tham s ph khng c n thi t UseParensForNegativeNumbers Tham s ph khng c n thi t GroupDigits Tham s ph khng c n thi t V d : Option Explicit Private Sub Form_Load() Dim MyStr MyStr = FormatPercent("12345", 5)'Returns 1,234,500.00000% MyStr = FormatPercent("12.345", 5)'Returns 1,234.50000% MyStr = FormatPercent("12.345", 3)'Returns 1,234.500% End Sub Tham kh o thm: Str Val Return Top .................................. .................................... Tn hm: FreeFile M t : FreeFile [(rangenumber)] Gi i phng file Tham s : rangenumber Tn file (d ng s #n) V d : Option Explicit Private Sub Form_Load() Dim MyIndex, FileNumber For MyIndex = 1 To 5 FileNumber = FreeFile' Gan FileNumber = Trong Open "TEST" & MyIndex For Output As #FileNumber' mo file Write #FileNumber, "This is a sample."' Ghi vo file Close #FileNumber' Dong file Next MyIndex End Sub .................................. Return Top .................................... Tn hm: Get M t : Get [#Filenum, position, ByteArray] L y thng tin t t p tin c m theo Binary t i v tr xc nh b i Position v lu vo ByteArray. S byte l y ra ty thu c vo kch th c c a m ng ByteArray. M i khi l y ra 1 byte con tr t p tin t ng chuy n t i v tr byte k ti p. V d : Dim Str as String * 4 Get #Filenum, 3, Str ---> l y 4 byte b t u t byte th 3 lu vo Str. (n u c cu l nh Get ti p theo m tham s Position b tr ng, th v tr b t u l y ra s l byte th 8) Dim noidung() As Byte On Error Resume Next Open fname For Binary Access Read As #1 ReDim noidung(LOF(1)) Get #1, , noidung' c ton b n i dung file vo bi n Close #1 Tham kh o thm: Put Return Top.................................... ..................................

Tn hm: GetAllSettings M t : GetAllSettings (appname, section) L y t t c thng tin registry c a appname Tham s : appname Tn registry section Tn section V d : Option Explicit Private Sub Form_Load() Dim MySettings As Variant, intSettings As Integer ' luu mot thong tin registry moi SaveSetting appname:="MyApp", section:="Startup", _ Key:="Top", setting:=75 SaveSetting "MyApp", "Startup", "Left", 50 'Lay thong tin registry MySettings = GetAllSettings(appname:="MyApp", section:="Startup") 'Duyet qua va in ra tung hang trong MyApp For intSettings = LBound(MySettings, 1) To UBound(MySettings, 1) Debug.Print MySettings(intSettings, 0), MySettings(intSettings, 1) Next intSettings 'Xoa registry DeleteSetting "MyApp", "Startup" End Sub Tham kh o thm: DeleteSetting GetSetting SaveSetting Return Top .................................... .................................. Tn hm: GetAttr M t : GetAttr (pathname) Cho bi t thu c tnh hi n t i File Tham s : pathname ng d n file mu n gn thu c tnh V d : Option Explicit Private Sub Form_Load() Dim MyAttr ' Gia su TESTFILE co thuoc tinh an MyAttr = GetAttr("TESTFILE")' Returns 2. ' Gia su TESTFILE co thuoc tinh an va thuoc tinh chi doc MyAttr = GetAttr("TESTFILE")' Returns 3 = 2 + 1 ' Gia su MYDIR la thu muc MyAttr = GetAttr("MYDIR")' Returns 16. End Sub Tham kh o thm: SetAttr .................................. Return Top .................................... Tn hm: GetSetting M t : GetSetting (appname, section, key [, default]) L y thng tin registry Tham s : appname Tn registry section Tn section key Tn key

default Gi tr tr v khi hm khng tm th y thng tin registry mu n l y V d : Option Explicit Private Sub Form_Load() Dim MySettings As Variant 'Luu mot thong tin registry moi SaveSetting "MyApp", "Startup", "Top", 75 SaveSetting "MyApp", "Startup", "Left", 50 'In ra man hinh thong tin registry vua tao Debug.Print GetSetting(appname:="MyApp", section:="Startup", _ Key:="Left", Default:="25") 'Xoa registry DeleteSetting "MyApp", "Startup" End Sub Tham kh o thm: DeleteSetting GetAllSettings SaveSetting Return Top .................................. Tn hm: Hex M t : Hex(number) Chuy n s Number sang d ng hexadecimal (h th Tham s : Number S c n chuy n V d : Option Explicit Private Sub Form_Load() Dim MyHex MyHex = Hex(5)' Returns 5. MyHex = Hex(10)' Returns A. MyHex = Hex(459)' Returns 1CB End Sub Tham kh o thm: Oct Return Top .................................. Tn hm: Hour M t : Hour (time) L y thng tin gi t chu i time Tham s : time Th i gian V d : Option Explicit Private Sub Form_Load() Dim MyTime, MyHour MyTime = #4:35:17 PM#' Khoi tao MyHour = Hour(MyTime)' Returns 16 End Sub Tham kh o thm: Minute Second .................................. Return Top

....................................

p l c phn)

....................................

....................................

Tn hm: IIf M t : Iif (expr, truepart, falsepart) C u trc r nhnh 2 i u ki n Tham s : expr Bi u th c i u ki n truepart Gi tr tr v n u bi u th c ng falsepart Gi tr tr v n u bi u th c sai V d : Option Explicit Private Sub Form_Load() Dim Result, TestMe TestMe = 1100 Result = IIf(TestMe > 1000, "Large", "Small")' Returns Large End Sub Tham kh o thm: Choose Switch Return Top.................................... .................................. Tn hm: Input M t : Input (number, [#]filenumber) c d li u t filenumber Tham s : number Chi u di c n l y [#]filenumber Tn file (d ng s #n) V d : Option Explicit Private Sub Form_Load() Dim MyChar Open "TESTFILE" For Input As #1' Mo file. Do While Not EOF(1) MyChar = Input(1, #1)' Lay 1 ki tu Debug.Print MyChar' In ra man hinh Loop Close #1' Dong file. End Sub Tham kh o thm: Line Input Return Top .................................... .................................. Tn hm: InputBox M t : InputBox(prompt[, title] [, default] [, xpos] [, ypos] [, helpfile][, context]) Hi n th h p tho i nh p li u, giao ti p v i ng i dng Tham s : prompt: Gi tr m b n mu n hi n th trong InputBox title Tiu c a InputBox default Gi tr m c nh khi InputBox xu t hi n xpos, ypos V tr xu t hi n c a InputBox helpfile ng d n file help context Ng c nh c a file help

V d : Option Explicit Private Sub Form_Load() Dim SinhNhat As String 'Hien thi hop thoai nhap lieu SinhNhat = InputBox("Nhap vao ngay sinh cua ban", "nhap lieu", "01/01/1981", 0, 0) If IsDate(SinhNhat) Then' kiem tra xem chuoi ngay sinh co phai la chuoi ngay thang khong MsgBox "Happy Birthday", VbOKOnly Else' neu chuoi ngay sinh khong phai dang ngay thang MsgBox "Day khong phai la ngay sinh", vbExclamation End If End Sub Tham kh o thm: MsgBox Return Top.................................... .................................. Tn hm: InStr M t : InStr([start, ]string1, string2[, compare]) Tm chu i string2 trong chu i string1, tm t v tr start Tham s : start V tr tm string1 Chu i tm ki m string2 Gi tr c n tm compare Ch r ki u d li u so snh trong qu trnh tm ki m Ghi ch: Dng cho tham s compare vbUseCompareOption = 1 Ch ty ch n, VB s t ng l a l ai d li u thch h p vbBinaryCompare = 0 So snh nh phn vbTextCompare = 1 So snh chu i vbDatabaseCompare = 2 So snh d li u V d : Option Explicit Private Sub Form_Load() Dim SearchString, SearchChar, MyPos SearchString = "XXpXXpXXPXXP"' String to search in. SearchChar = "P"' Search for "P". ' So sanh theo cua text tu vi tri 4 MyPos = InStr(4, SearchString, SearchChar, 1)' Returns 6. ' So sanh theo Binary MyPos = InStr(1, SearchString, SearchChar, 0)' Returns 9. ' So sanh theo Binary, do mac dinh la 0 MyPos = InStr(SearchString, SearchChar)' Returns 9. MyPos = InStr(1, SearchString, "W")' Returns 0. End Sub Tham kh o thm: InStrRev Return Top .................................... .................................. Tn hm: InStrRev M t : InstrRev(stringcheck, stringmatch[, start[, compare]]) Tm chu i stringmatch trong ch i stringcheck Tham s : stringmatch Chu i tm ki m

stringcheck Gi tr c n tm start Tm n k t th start trong chu i stringcheck compare Ch r ki u d li u so snh trong qu trnh tm ki m Ghi ch: Dng cho tham s compare vbUseCompareOption = 1 Ch ty ch n, VB s t ng l a l ai d li u thch h p vbBinaryCompare = 0 So snh nh phn vbTextCompare = 1 So snh chu i vbDatabaseCompare = 2 So snh d li u V d : Option Explicit Private Sub Form_Load() Dim SearchString, SearchChar, MyPos SearchString = "XXpXXpXXPXXP"' String to search in. SearchChar = "Xp"' Search for "Xp". MyPos = InStrRev(SearchString, SearchChar, 4, 1)'Returns 2 MyPos = InStrRev(SearchString, SearchChar, 10, 1)'Returns 8 MyPos = InStrRev(SearchString, SearchChar, , 1)'Returns 11 End Sub Tham kh o thm: InStr Return Top .................................... .................................. Tn hm: Int M t : Int(number) L y ph n nguyn c a s Number, c cht khc bi t so v i hm Fix khi Number l s m Tham s : Number S c n tnh V d : Option Explicit Private Sub Form_Load() Dim MyNumber MyNumber = Int(99.8)' Returns 99. MyNumber = Fix(99.2)' Returns 99. MyNumber = Int(-99.8)' Returns -100. MyNumber = Fix(-99.8)' Returns -99. MyNumber = Int(-99.2)' Returns -100. MyNumber = Fix(-99.2)' Returns -99. End Sub Tham kh o thm: Fix Round Return Top ................................... .................................. Tn hm: IsArray M t : IsArray(varname) Ki m tra xem bi n varname c ph i l m t m ng hay khng Tham s : varname Bi n mu n ki m tra V d : Option Explicit Private Sub Form_Load() Dim MyArray(1 To 5) As Integer Dim YourArray

Dim MyCheck As Boolean YourArray = Array(1, 2, 3)' Su dung ham Array MyCheck = IsArray(MyArray)' Returns True. MyCheck = IsArray(YourArray)' Returns True. End Sub Return Top .................................. .................................... Tn hm: IsDate M t : IsDate(expression) Ki m tra xem bi u th c expression c ph i l ngy thng khng Tham s : expression Bi u th c mu n ki m tra V d : Option Explicit Private Sub Form_Load() Dim MyDate Dim YourDate Dim NoDate Dim MyCheck As Boolean MyDate = "August 10, 2003": YourDate = #8/10/2003#: NoDate = "Hello" MyCheck = IsDate(MyDate)' Returns True. MyCheck = IsDate(YourDate)' Returns True. MyCheck = IsDate(NoDate)' Returns False. End Sub Tham kh o thm: MsgBox InputBox Return Top .................................. .................................... Tn hm: IsEmpty M t : IsEmpty(expression) Ki m tra xem bi u th c expression c ph i empty khng Tham s : expression Bi u th c mu n ki m tra V d : Option Explicit Private Sub Form_Load() Dim MyVar Dim MyCheck As Boolean MyCheck = IsEmpty(MyVar)' Returns True. MyVar = Null' gan MyVar = Null MyCheck = IsEmpty(MyVar)' Returns False. MyVar = Empty' Assign Empty. MyCheck = IsEmpty(MyVar)' Returns True. End Sub .................................. Return Top .................................... Tn hm: IsMissing M t : IsMissing(argname) Ki m tra xem argname co t n t i hay khng Tham s : argname Tn tham s mu n ki m tra V d : Option Explicit Private Sub Form_Load() Dim ReturnValue ' The following statements call the user-defined function procedure. ReturnValue = ReturnTwice()' Returns Null. ReturnValue = ReturnTwice(2)' Returns 4. End Function

'Khai bao mot ham Function ReturnTwice(Optional A) ' Viec khai bao tu khoa Optional truoc tham so A de bao cho VB biet ' tham so A co truyen vao hay khong cung duoc If IsMissing(A) Then' kiem tra xem tham so a co duoc truyen vao khong ' Neu gia tri A khong duoc truyen vao thi tra ve Null ReturnTwice = Null Else' neu tham so a duoc truyen vao ReturnTwice = A * 2 End If End Function .................................. Return Top .................................... Tn hm: IsNull M t : IsNull(expression) Ki m tra xem bi u th c expression c Null khng Tham s : expression Bi u th c mu n ki m tra V d : Option Explicit Private Sub Form_Load() Dim MyVar Dim MyCheck As Boolean MyCheck = IsNull(MyVar)' Returns False. MyVar = "" MyCheck = IsNull(MyVar)' Returns False. MyVar = Null MyCheck = IsNull(MyVar)' Returns True. End Sub .................................. Return Top .................................... Tn hm: IsNumeric M t : IsNumeric(expression) Ki m tra xem bi u th c expression c ph i l s khng Tham s : expression Bi u th c mu n ki m tra V d : Option Explicit Private Sub Form_Load() Dim MyVar Dim MyCheck As Boolean MyVar = "53"' Assign value. MyCheck = IsNumeric(MyVar)' Returns True. MyVar = "459.95"' Assign value. MyCheck = IsNumeric(MyVar)' Returns True. MyVar = "45 Help"' Assign value. MyCheck = IsNumeric(MyVar)' Returns False. End Sub .................................. Return Top .................................... Tn hm: Join M t : Join(sourcearray[, delimiter]) N i t t c cc chu i trong m ng sourcearray v i d u phn cch delimiter Tham s : sourcearray M ng c n n i thnh chu i delimiter D u phn cch gi a cc ph n t m ng V d : Option Explicit Private Sub Form_Load()

Dim aSplit() As String Dim Names(1 To 5) As String Dim MyStr As String Names(1) = "A" Names(2) = "B" Names(3) = "C" Names(4) = "D" Names(5) = "E" MyStr = Join(Names(), "/")'Returns "A/B/C/D/E" aSplit = Split(MyStr, "/", 5)'Returns aSplit(0)="A",...,aSplit(4)="E" End Sub Tham kh o thm: Split Return Top .................................... .................................. Tn hm: Kill M t : Kill pathname Xa file Tham s : pathname ng d n File mu n xa, c th dng *.* V d : Option Explicit Private Sub Form_Load() Kill "D:\MyFile.txt" End Sub Tham kh o thm: FileCopy FileLen Return Top .................................... .................................. Tn hm: LCase M t : LCase(string) Chuy n ton b ch trong chu i string thnh ch th ng Tham s : string Chu i c n chuy n V d : Option Explicit Private Sub Form_Load() Dim UpperCase, LowerCase UpperCase = "Hello WORLD 1234"' Khoi tao LowerCase = LCase(UpperCase)' Returns "hello world 1234". End Sub Tham kh o thm: Ucase Return Top .................................. .................................... Tn hm: Left M t : Left(string, length) C t chu i string t tri sang length k t Tham s : string Chu i c n c t length Chi u di chu i mu n c t V d : Option Explicit Private Sub Form_Load() Dim AnyString, MyStr AnyString = "Hello World"' Khoi tao MyStr = Left(AnyString, 1)' Returns "H". MyStr = Left(AnyString, 7)' Returns "Hello W".

MyStr = Left(AnyString, 20)' Returns "Hello World". End Sub Tham kh o thm: Len Mid Right Return Top .................................... .................................. Tn hm: Len M t : Len(string | varname) m s k t c trong chu i string ho c bi n varname Tham s : string | varname Chu i hay bi n c n tnh chi u di V d : Option Explicit Private Type CustomerRecord ID As Integer' Len(ID)=2 Name As String * 10 Address As String * 30 End Type Private Sub Form_Load() Dim Customer As CustomerRecord Dim MyInt As Integer, MyCur As Currency Dim MyString, MyLen MyString = "Hello World"' Khoi tao MyLen = Len(MyInt)' Returns 2. MyLen = Len(Customer)' Returns 42. MyLen = Len(MyString)' Returns 11. MyLen = Len(MyCur)' Returns 8. End Sub Tham kh o thm: Left Mid Right Return Top .................................. .................................... Tn hm: Like M t : "String1" Like "String2" So snh 2 chu i cho php s d ng bi t ng (nh dng k t i di n trong Dos) tr tr v = True n u tng h p. Tham s : String1, String2 : l hai chu i k t c n so snh. Ghi ch: Hm Like m c nh cng l hm nh y k t , theo thi t l p Option Compare form ho c module V d : "abcd" Like "*bcd" = True "abcd" Like "a?cd" = True "a1cd" Like "a#cd" = True Return Top .................................... .................................. Tn hm: Line Input M t : Line Input #filenumber, varname c d li u theo t ng hng Tham s : #filenumber Tn file (d ng s #n) varname Bi n ch a thng tin Ghi ch: Trong l nh Line Input b n khng c php cho c tr c ti p vo m t property c a

m t i t ng. Ch ng h n n u b n vi t: Line Input #1, Text1.Text s sinh ra l i. B n c n ph i c qua m t bi n trung gian r i m i gn l i vo property. V d : Option Explicit Private Sub Form_Load() Dim TextLine Open "TESTFILE" For Input As #1' Mo file. Do While Not EOF(1) Line Input #1, TextLine' Doc tung dong gan vao bien TextLine Debug.Print TextLine' In ra man hinh Loop Close #1' Dong file. End Sub Tham kh o thm: Input Return Top ................................... .................................. Tn hm: Loc M t : Loc(filenumber) Tr v v tr byte ang c Tham s : filenumber Tn file (d ng s #n) V d : Option Explicit Private Sub Form_Load() Dim MyLocation, MyLine Open "TESTFILE" For Binary As #1' Mo file Do While MyLocation < LOF(1)' Lap cho den khi MyLocation = chieu dai #1 MyLine = MyLine & Input(1, #1)' Doc tung ki tu MyLocation = Loc(1)' Cap nhat lai vi tri byte Debug.Print MyLine; Tab; MyLocation' in ra man hinh Loop Close #1' Dong file. End Sub Tham kh o thm: EOF LOF Seek Return Top .................................... .................................. Tn hm: Lock M t : Lock [#Filenum, Expression] Kho t p tin khng cho ng i khc truy c p khi App c a b n ang m . Tham s th hai chuyn bi t v tr kho. N u b qua tham s ty ch n ny, l nh Lock s kho ton b t p tin. i v i cc t p tin m theo truy c p tu n t l nh Lock s kho ton b t p tin b t k kho ng do tham s 2 qui nh. V d : Lock #Filenum, 1 To 100 ---> s kho 100 byte t byte th 1 Unlock #Filenum, 1 To 100 ---> s m kho 100 byte t byte th 1 Tham kh o thm: Unlock Return Top .................................... .................................. Tn hm: LOF M t : LOF(filenumber) L y kch th c c a file Tham s : filenumber Tn file (d ng s #n) V d :

Option Explicit Private Sub Form_Load() Dim FileLength Open "TESTFILE" For Input As #1' Mo file FileLength = LOF(1)' Lay kich thuoc file Close #1' Dong file. End Sub Tham kh o thm: EOF Loc Return Top .................................... .................................. Tn hm: Log M t : Log(number) Hm logarithm Tham s : Number S c n tnh V d : Option Explicit Private Sub Form_Load() Dim MyAngle, MyLog MyAngle = 1.3' Khoi tao ' Tinh nghinh dao hyperbolic sine. MyLog = Log(MyAngle + Sqr(MyAngle * MyAngle + 1)) End Sub Tham kh o thm: Exp Return Top .................................... .................................. Tn hm: LTrim M t : LTrim(string) C t b t t c cc k t tr ng bn tri chu i string Tham s : String Chu i c n c t V d : Option Explicit Private Sub Form_Load() Dim MyString, TrimString MyString = " <-Trim-> "' Khoi tao TrimString = LTrim(MyString)' Returns = "<-Trim-> ". TrimString = RTrim(MyString)' Returns = " <-Trim->". TrimString = LTrim(RTrim(MyString))' Returns = "<-Trim->". TrimString = Trim(MyString)' Returns = "<-Trim->". End Sub Tham kh o thm: RTrim Trim Return Top .................................. .................................... Tn hm: Mid M t : Mid(string, start[, length]) C t chu i string t v tr start v c t length k t Tham s : string Chu i c n c t start V tr b t u c t length Chi u di chu i mu n c t

V d : Option Explicit Private Sub Form_Load() Dim AnyString, MyStr AnyString = "Hello World"' Khoi tao MyStr = Mid(AnyString, 1, 3)' Returns "Hel". MyStr = Mid(AnyString, 4, 4)' Returns "lo W". MyStr = Right(AnyString, 3)' Returns "llo World". End Sub Tham kh o thm: Left Len Right Return Top ................................... .................................. Tn hm: Minute M t : Minute(time) L y thng tin pht t chu i time Tham s : time Th i gian V d : Option Explicit Private Sub Form_Load() Dim MyTime, MyHour MyTime = #4:35:17 PM#' Khoi tao MyHour = Hour(MyTime)' Returns 35 End Sub Tham kh o thm: Hour Second Return Top .................................... .................................. Tn hm: MkDir M t : MkDir path T o th m c Tham s : path ng d n ni mu n t o th m c V d : Option Explicit Private Sub Form_Load() MkDir "D:\My Folder" End Sub Tham kh o thm: RmDir Return Top .................................. .................................... Tn hm: Month M t : Month(date) L y thng tin thng t chu i date Tham s : date Ngy thng V d : Option Explicit Private Sub Form_Load() Dim MyDate, MyMonth MyDate = #8/10/2003#' Khoi tao MyMonth = Day(MyDate)' Returns 08 End Sub Tham kh o thm:

Day Year Return Top.................................... .................................. Tn hm: MonthName M t : MonthName(month[, abbreviate]) Chuy n i thng t d ng s sang d ng chu i Tham s : month Thng abbreviate Vi t t t V d : Option Explicit Private Sub Form_Load() Dim MyMonth MyMonth = MonthName("4", True)'Returns Apr MyMonth = MonthName("4")' Returns April End Sub Return Top .................................... .................................. Tn hm: MsgBox M t : MsgBox(prompt[, buttons] [, title] [, helpfile][, context]) Hi n th h p tho i thng bo, giao ti p v i ng i dng Tham s : prompt Gi tr m b n mu n hi n th trong MsgBox buttons S nt hi n th trong MsgBox title Tiu c a MsgBox helpfile ng d n file help context Ng c nh c a file help Ghi ch: Cho tham s Button vbOKOnly = 0 Ch hi n th nt OK vbOKCancel = 1 Hi n th 2 nt OK v Cancel vbAbortRetryIgnore = 2 Hi n th 3 nt Abort, Retry v Ignore vbYesNoCancel = 3 Hi n th 3 nt Yes, No v Cancel vbYesNo = 4 Hi u th 2 nt Yes v No vbRetryCancel = 5 Hi n th 2 nt Retry v Cancel vbCritical = 16 Hi n th Icon Critical vbQuestion = 32 Hi n th Icon Question vbExclamation = 48 Hi n th Icon Exclamation vbInformation = 64 Hi n th Icon Information vbDefaultButton = 10 Gn m c nh cho nt u tin c Focus c a chu t vbDefaultButton2 = 256 Gn m c nh cho nt th hai c Focus c a chu t vbDefaultButton3 =512 Gn m c nh cho nt th ba c Focus c a chu t vbDefaultButton4 = 768

Gn m c nh cho nt th t c Focus c a chu t vbApplicationModal = 0 Gn thu c tnh Modal(lun n m trn) cho MsgBox c p ng d ng vbSystemModal = 4096 Gn thu c tnh Modal(lun n m trn) cho MsgBox c p h th ng vbMsgBoxHelpButton = 16384 Thm nt Help vo trong MsgBox VbMsgBoxSetForeground = 65536 Set Foreground cho MsgBox vbMsgBoxRight = 524288 Canh ph i title trong MsgBox vbMsgBoxRtlReading = 1048576 Cho php title xu t hi n t ph i sang tri ********Gi tr tr v ********* vbOK = 1 OK vbCancel = 2 Cancel vbAbort = 3 Abort vbRetry = 4 Retry vbIgnore = 5 Ignore vbYes = 6 Yes vbNo = 7 No V d : Option Explicit Private Sub Form_Load() Dim SinhNhat As String 'Hien thi hop thoai nhap lieu SinhNhat = InputBox("Nhap vao ngay sinh cua ban", "nhap lieu", "01/01/1981", 0, 0) If IsDate(SinhNhat) Then' kiem tra xem chuoi ngay sinh co phai la chuoi ngay thang khong MsgBox "Happy Birthday", VbOKOnly Else' neu chuoi ngay sinh khong phai dang ngay thang MsgBox "Day khong phai la ngay sinh", vbExclamation End If End Sub Tham kh o thm: Input Box Return Top .................................. .................................... Tn hm: Name M t : Name OldName As NewName i tn t p tin Tham s : OldName: tn t p tin c NewName: tn t p tin m i V d : Vo Project / References v ch n Microsoft Access 10.0 Object Library. Thm o n code sau vo Form: Private Sub Form_Unload(Cancel As Integer) 'Nn CSDL tn MyData.mdb v t o 1 CSDL m i tn DB2.mdb DBEngine.CompactDatabase App.Path & "\MyData.mdb", App.Path & "\DB2.mdb" 'Xa MyData.mdb Kill "MyData.mdb" ' i tn DB2.mdb thnh MyData.mdb Dim OldName Dim NewName OldName = "DB2.mdb": NewName = "MyData.mdb" Name OldName As NewName

End Sub Return Top ................................... .................................. Tn hm: Now M t : Now() L y ngy gi hi n t i c a h th ng V d : Option Explicit Private Sub Form_Load() Dim TheDate As Date Dim Msg TheDate = InputBox("Enter a date") Msg = "Days from today: " & DateDiff("d", Now, TheDate) MsgBox Msg End Sub .................................. Return Top .................................... Tn hm: Oct M t : Oct(number) Chuy n s Number sang d ng octal (h bt phn) Tham s : Number S c n chuy n V d : Option Explicit Private Sub Form_Load() Dim MyOct MyOct = Oct(4)' Returns 4. MyOct = Oct(8)' Returns 10. MyOct = Oct(459)' Returns 713 End Sub Tham kh o thm: Hex Return Top ................................... .................................. Tn hm: Open M t : Open [PathName as String] For [Mode] As [ID File] M i khi th y l nh Open, VB s s n sng cho cc thao tc c v ghi ln File c cung c p tham s Tham s : [PathName] : tn v ng d n c a t p tin c n thao tc. [Mode] : c nm ch m t p tin c t tham s [Mode] [ID File] : ch s c a t p tin ang thao tc. Ghi ch: Cc d ng th c c b n c a l nh Open : Open [Pathname] For Input As Filenum' m File v ch c c thng tin, khng ghi ln c. Open [PathName] For Output As Filenum' m File xu t thng tin. Khi c m theo d ng ny m i thng tin c trn File s b m t. Open [PathName] For Append As Filenum' m File c v ghi ti p ln c. Open [PathName] For Random As Filenum' m v truy c p ng u nhin cc b n ghi v cc tr ng trn File (ph i bi t c c u trc c a cc b n ghi) Open [PathName] For Binary As Filenum' c ghi theo Byte. y l d ng t ng qut v linh ho t nh t. V d : Option Explicit Private Sub Form_Load() Dim TextLine Open "TESTFILE" For Input As #1' Mo file. Do While Not EOF(1) Line Input #1, TextLine' Doc tung dong gan vao bien TextLine Debug.Print TextLine' In ra man hinh Loop

Close #1' Dong file. End Sub Tham kh o thm: Print Line Input EOF LOF Return Top .................................. .................................... Tn hm: Print M t : Print #filenumber, [outputlist] Vi t n i dung m i vo file. Tham s : #filenumber Tn file (d ng s #n) outputlist Danh sch cc bi n m b n mu n l y n i dung c a chng vi t vo file. V d : Option Explicit Private Sub Form_Load() Open "TESTFILE" For Output As #1' Mo file de nhap Print #1, "This is a test"'Dong 1 Print #1,' Nhap vao 1 dong trang Print #1, "Zone 1"; Tab; "Zone 2"' Zone 1 cach Zone 2 1 khoang tab Print #1, "Hello"; " "; "World"' Returns "Hello World" Print #1, Spc(5); "5 leading spaces "' Returns " 5 leading spaces" Print #1, Tab(10); "Hello"' 1 khoang tab 10 khoang trang Close #1' Dong file End Sub Tham kh o thm: Reset Return Top .................................. .................................... Tn hm: Put M t : Put [#Filenum, Position, ByteArray] t n i dung c a m ng [ByteArray] vo v tr byte th [Position] c a t p tin c ch s [#Filenum]. Ghi ch: L nh Put s ghi ln m i th v ch dng cho truy c p Random v Binary. V d : Put #filenum, , Str ---> s ghi 4 byte b t u v tr byte th 1 Dim fnum As Long Dim b() As Byte fnum = FreeFile() Open fname For Binary Access Write As #fnum str = "N i dung t p tin" b = str Put #fnum, , b Close #fnum Tham kh o thm: Get .................................. Return Top .................................... Tn hm: QBColor M t : QBColor(color) Chuy n gi tr color sang cc mu c b n Tham s : color Tn c a mu Ghi ch: Dng cho tham s color 0 = Black 1 = Blue

2 = Green 3 = Cyan 4 = Red 5 = Magenta 6 = Yellow 7 = White 8 = Gray 9 = Light Blue 10 = Light Green 11 = Light Cyan 12 = Light Red 13 = Light Magenta 14 = Light Yellow 15 = Bright White V d : Option Explicit Private Sub Form_Load() Me.BackColor = QBColor(1)'Returns Blue End Sub Tham kh o thm: RGB Return Top .................................... .................................. Tn hm: Randomize M t : Randomize [number] Kh i t o b pht s ng u nhin Tham s : number Gi i h n trn c a dy s ng u nhin (khng c n thi t) V d : Option Explicit Private Sub Form_Load() Dim MyValue Randomize' khoi tao bo phat so ngau nhien MyValue = Int((6 * Rnd) + 1)'Returns tu 1 den 6 End Sub Tham kh o thm: Rnd Return Top .................................. .................................... Tn hm: Replace M t : Replace(expression, find, replace[, start[, count[, compare]]]) Thay th chu i find trong chu i expression thnh chu i replace Tham s : expression Chu i thay th find Gi tr thay th start V tr b t u tm gi tr thay th trong chu i thay th count Tm n th t th count trong chu i thay th compare Ch r ki u d li u so snh trong qu trnh tm ki m Ghi ch: Dng cho tham s compare vbUseCompareOption = 1 Ch ty ch n, VB s t ng l a l ai d li u thch h p vbBinaryCompare = 0 So snh nh phn vbTextCompare = 1 So snh chu i vbDatabaseCompare = 2 So snh d li u

V d : Option Explicit Private Sub Form_Load() Dim AnyString, MyStr AnyString = "Hello World"' Khoi tao MyStr = Replace(AnyString, "Hel", "AAAAA", 1, -1, vbTextCompare)'Returns "AAAAAlo World" MyStr = Replace(AnyString, "Wor", "BBB", 1, -1, vbTextCompare)'Returns "Hello BBBld" End Sub Return Top .................................... .................................. Tn hm: Reset M t : Reset ng t t c cc file c m b ng l nh Open. V d : Option Explicit Private Sub Form_Load() Dim FileNumber For FileNumber = 1 To 5 Open "TEST" & FileNumber For Output As #FileNumber' M file Write #FileNumber, "Hello World"' Ghi d li u vo file. Next FileNumber Reset' ng file v c p nh t d li u vo file End Sub Tham kh o thm: Print Return Top..................................... .................................. Tn hm: RGB M t : RGB(red, green, blue) Chuy n i cc gi tr n s c thnh mu pha Tham s : red Gi tr t 0 - 255 green Gi tr t 0 - 255 blue Gi tr t 0 - 255 Ghi ch: Dung cho 3 tham so red ,green, blue Black = 0, 0, 0 Blue = 0, 0, 255 Green = 0, 255, 0 Cyan = 0, 255, 255 Red = 255, 0, 0 Magenta = 255, 0, 255 Yellow = 255, 255, 0 White = 255, 255, 255 V d : Option Explicit Private Sub Form_Load() Me.BackColor = RGB(255, 0, 0)'Returns Red End Sub Tham kh o thm: QBColor Return Top .................................. .................................... Tn hm: Right M t : Right(string, length) C t chu i string t ph i sang length k t Tham s : string

Chu i c n c t length Chi u di chu i mu n c t V d : Option Explicit Private Sub Form_Load() Dim AnyString, MyStr AnyString = "Hello World"' Khoi tao MyStr = Right(AnyString, 1)' Returns "d". MyStr = Right(AnyString, 7)' Returns "o World". MyStr = Right(AnyString, 20)' Returns "Hello World". End Sub Tham kh o thm: Left Len Mid Return Top .................................... .................................. Tn hm: RmDir M t : RmDir path Xa th m c Tham s : path ng d n th m c mu n xa V d : Option Explicit Private Sub Form_Load() RmDir "D:\My Folder" End Sub Tham kh o thm: MkDir Return Top .................................... .................................. Tn hm: Rnd M t : Rnd[(number)] Pht m t s ng u nhin trong kho ng t 0 n number Tham s : number Gi i h n trn c a dy s ng u nhin V d : Option Explicit Private Sub Form_Load() Dim MyValue Randomize' khoi tao bo phat so ngau nhien MyValue = Int((6 * Rnd) + 1)'Returns tu 1 den 6 End Sub Tham kh o thm: Randomize Return Top .................................... .................................. Tn hm: Round M t : Round(expression [,numdecimalplaces]) Lm trn Tham s : expression Bi u th c c n lm trn numdecimalplaces S s l sau d u ph y V d : Option Explicit Private Sub Form_Load() Dim MyRound MyRound = Round(1.2345, 3)'Returns 1.234

MyRound = Round(1.2346, 3)'Returns 1.235 MyRound = Round(12.345, 1)'Returns 12.3 End Sub Tham kh o thm: Int Fix Return Top.................................... .................................. Tn hm: RTrim M t : RTrim(string) C t b t t c cc k t tr ng bn ph i chu i string Tham s : String Chu i c n c t V d : Option Explicit Private Sub Form_Load() Dim MyString, TrimString MyString = " <-Trim-> "' Khoi tao TrimString = LTrim(MyString)' Returns = "<-Trim-> ". TrimString = RTrim(MyString)' Returns = " <-Trim->". TrimString = LTrim(RTrim(MyString))' Returns = "<-Trim->". TrimString = Trim(MyString)' Returns = "<-Trim->". End Sub Tham kh o thm: LTrim Trim Return Top .................................... .................................. Tn hm: SaveSetting M t : SaveSetting appname, section, key, setting Lu thng tin registry Tham s : appname Tn registry section Tn section key Tn key V d : Option Explicit Private Sub Form_Load() ' luu mot thong tin registry moi SaveSetting appname:="MyApp", section:="Startup", _ Key:="Top", setting:=75 SaveSetting "MyApp", "Startup", "Left", 50 ' Xoa key Startup trong MyApp DeleteSetting "MyApp", "Startup" End Sub Tham kh o thm: DeleteSetting GetAllSettings GetSetting .................................. Return Top .................................... Tn hm: Second M t : Second(time) L y thng tin giy t chu i time Tham s : time Th i gian V d : Option Explicit

Private Sub Form_Load() Dim MyTime, MyHour MyTime = #4:35:17 PM#' Khoi tao MyHour = Hour(MyTime)' Returns 17 End Sub Tham kh o thm: Hour Minute Return Top ................................. .................................. Tn hm: Seek M t : Seek(filenumber) Tr v v tr hi n t i c a con tr t p tin. Ho c: Seek [#Filenum, Position] D ch chuy n con tr t p tin n v tr qui nh b i tham s Position Tham s : filenumber Tn file (d ng s #n) V d : Option Explicit Type Record ID As Integer Name As String * 20 End Type Private Sub Form_Load() Dim MyRecord As Record Open "TESTFILE" For Random As #1 Len = Len(MyRecord)' Mo file Do While Not EOF(1)' Lap cho den khi ket thuc file Get #1, , MyRecord' Doc mau tin Debug.Print Seek(1)' In so mau tin 'Seek #1, 3 'Tr t i v tr m u tin th 3 Loop Close #1' Dong file. End Sub Tham kh o thm: Loc .................................. Tn hm: SendKeys M t : SendKeys string[, wait] Gi l p m t s ki n nh n Tham s : string Chu i phm mu n g i i wait Th i gian ch Ghi ch: Dng cho tham s key BACKSPACE = {BACKSPACE}, BREAK = {BREAK} CAPS LOCK = {CAPSLOCK} DEL or DELETE = {DELETE} DOWN ARROW = {DOWN} END = {END} ENTER = {ENTER} or ~ ESC = {ESC} HELP = {HELP} HOME = {HOME} INS or INSERT = {INSERT} LEFT ARROW = {LEFT} NUM LOCK = {NUMLOCK}

Return Top

....................................

phm

{BS}, or {BKSP}

or {DEL}

or {INS}

PAGE DOWN = {PGDN} PAGE UP = {PGUP} PRINT SCREEN = {PRTSC} RIGHT ARROW = {RIGHT} SCROLL LOCK = {SCROLLLOCK} TAB = {TAB} UP ARROW = {UP} F1 = {F1} F2 = {F2} F3 = {F3} F4 = {F4} F5 = {F5} F6 = {F6} F7 = {F7} F8 = {F8} F9 = {F9} F10 = {F10} F11 = {F11} F12 = {F12} F13 = {F13} F14 = {F14} F15 = {F15} F16 = {F16} SHIFT = + CTRL = ^ ALT = % V d : Option Explicit Private Sub Form_Load() Dim ReturnValue, I ReturnValue = Shell("CALC.EXE", 1)' Run Calculator. AppActivate ReturnValue' Activate Calculator. 'Gui mot day phep cong tu 1 den 100 For I = 1 To 100' SendKeys I & "{+}", True Next I SendKeys "=", True' Tinh ket qua SendKeys "%{F4}", True' ALT+F4 de dong chuong trinh End Sub Tham kh o thm: AppActivate Shell Return Top .................................. .................................... Tn hm: SetAttr M t : SetAttr pathname, attributes Gn thu c tnh cho File v th m c Tham s : pathname ng d n File ho c th m c mu n gn thu c tnh attributes Thu c tnh mu n gn Ghi ch: Dng cho tham s attributes vbNormal = 0 File Bnh th ng vbReadOnly = 1 File ch c vbHidden = 2 File n vbSystem = 4 File h th ng vbArchive 32 File lu tr

V d : Option Explicit Private Sub Form_Load() SetAttr "TESTFILE", vbHidden' Gan thuoc tinh an cho TESTFILE SetAttr "TESTFILE", vbHidden + vbReadOnly' Gan thuoc tinh an va chi doc cho TESTFILE End Sub Tham kh o thm: GetAttr Return Top ..................................... .................................. Tn hm: Shell M t : Shell(pathname[,windowstyle]) Kh i ng m t chng trnh ng d ng khc Tham s : pathname ng d n t p tin ch y c a chng trnh windowstyle Ch hi n th c a trnh ng d ng Ghi ch: Dng cho tham s windowstyle vbHide = 0 Ch n vbNormalFocus = 1 Ch bnh th ng c Focus vbMinimizedFocus = 2 Ch thu nh c Focus vbMaximizedFocus = 3 Ch phng to vbNormalNoFocus = 4 Ch bnh th ng khng c Focus vbMinimizedNoFocus = 6 Ch thu nh khng c Focus V d : Option Explicit Private Sub Form_Load() Dim MyAppID, ReturnValue AppActivate "Microsoft Word"' Activate Microsoft ' Ham AppActivate co the su dung gia tri do hm shell tra ve MyAppID = Shell("C:\WORD\WINWORD.EXE", 1)' Run Microsoft Word. AppActivate MyAppID' Activate Microsoft ' Word. ReturnValue = Shell("c:\EXCEL\EXCEL.EXE", 1)' Run Microsoft Excel. AppActivate ReturnValue' Activate Microsoft ' Excel. End Sub Tham kh o thm: AppActivate SendKeys Return Top .................................. .................................... Tn hm: Sin M t : Sin(number) L y Sin c a s Number Tham s : Number S c n tnh V d : Option Explicit Private Sub Form_Load() Dim MyAngle, MyCosecant MyAngle = 1.3' Khoi tao

MyCosecant = 1 / Sin(MyAngle)' Tinh cosecant. End Sub Tham kh o thm: Atn Cos Sqr Tan Return Top ..................................... .................................. Tn hm: Space M t : Space(number) Tr v chu i number kho ng tr ng Tham s : number S kho ng tr ng V d : Option Explicit Private Sub Form_Load() Dim MyString MyString = Space(10)'Returns " " MyString = "Hello" & Space(10) & "World"' Returns "Hello World" End Sub Return Top ..................................... .................................. Tn hm: Split M t : Split(expression[, delimiter[, limit[, compare]]]) Tch chu i expression thnh cc chu i con v i d u phn cch delimiter Tham s : expression Chu i c n tch delimiter D u phn cch gi a cc t trong chu i c n tch limit S t c n tch, tnh t tri sang compare Ch r ki u d li u so snh trong qu trnh tm ki m Ghi ch: Dng cho tham s compare vbUseCompareOption = 1 Ch ty ch n, VB s t ng l a l ai d li u thch h p vbBinaryCompare = 0 So snh nh phn vbTextCompare = 1 So snh chu i vbDatabaseCompare = 2 So snh d li u V d : Option Explicit Private Sub Form_Load() Dim aSplit() As String Dim Names(1 To 5) As String Dim MyStr As String Names(1) = "A" Names(2) = "B" Names(3) = "C" Names(4) = "D" Names(5) = "E" MyStr = Join(Names(), "/")'Returns "A/B/C/D/E" aSplit = Split(MyStr, "/", 5)'Returns aSplit(0)="A",...,aSplit(4)="E" End Sub Tham kh o thm: Join Return Top ................................... ..................................

Tn hm: Sqr M t : Sqr(number) L y cn b c 2 c a s Number Tham s : Number S c n tnh V d : Option Explicit Private Sub Form_Load() Dim MySqr MySqr = Sqr(4)' Returns 2. MySqr = Sqr(23)' Returns 4.79583152331272. MySqr = Sqr(0)' Returns 0. MySqr = Sqr(-4)' Loi, do khong the lay can cua 1 so am End Sub Tham kh o thm: Sin Cos Atn Tan Return Top .................................... .................................. Tn hm: Str M t : Str(number) Chuy n i s Number sang chu i Tham s : Number S mu n chuy n V d : Option Explicit Private Sub Form_Load() Dim MyString MyString = Str(459)' Returns " 459". MyString = Str(-459.65)' Returns "-459.65". MyString = Str(459.001)' Returns " 459.001". End Sub Tham kh o thm: FormatCurrency FormatNumber FormatPercent Val .................................. Return Top .................................... Tn hm: StrComp M t : StrComp(string1, string2[, compare]) So snh hai chu i string1 v string2 Tham s : string1 Chu i so snh 1 string2 Chu i so snh 2 compare Ch r ki u d li u so snh trong qu trnh tm ki m Ghi ch: Dng cho tham s compare vbUseCompareOption = 1 Ch ty ch n, VB s t ng l a l ai d li u thch h p vbBinaryCompare = 0 So snh nh phn

vbTextCompare = 1 So snh chu i vbDatabaseCompare = 2 So snh d li u ********Gi tr tr v ********* string1 nh hn string2 = -1 string1 b ng string2 = 0 string1 l n hn string2 = 1 string1 ho c string2 Null = Null V d : Option Explicit Private Sub Form_Load() Dim MyStr1, MyStr2, MyComp MyStr1 = "ABCD": MyStr2 = "abcd"' Khoi tao MyComp = StrComp(MyStr1, MyStr2, 1)' Returns 0. MyComp = StrComp(MyStr1, MyStr2, 0)' Returns -1. MyComp = StrComp(MyStr2, MyStr1)' Returns 1. End Sub Return Top .................................. .................................... Tn hm: StrConv M t : StrConv(string, conversion, LCID) Chuy n chu i string sang m t nh d ng khc (v d Unicode) Tham s : string Chu i c n chuy n conversion Ch mu n chuy n Ghi ch: Dung cho tham so conversion vbUpperCase = 1 Chuy n sang ch hoa vbLowerCase = 2 Chuy n sang ch th ng vbProperCase 3 Chuy n sang ch vi t hoa ch ci u tin c a m i t vbWide* = 4* Chuy n t narrow (single-byte) characters sang wide (double-byte) characters. vbNarrow* = 8* Chuy n t wide (double-byte) characters sang narrow (single-byte) characters. vbKatakana** = 16** Chuy n t Hiragana characters sang Katakana characters. vbHiragana** = 32** Chuy n t Katakana characters sang Hiragana characters. vbUnicode = 64 Chuy n sang ch Unicode s d ng code m c nh c a h th ng vbFromUnicode = 128 Chuy n t Unicode sang m m c nh c a h th ng V d : Option Explicit Private Sub Form_Load() Dim i As Long Dim x() As Byte x = StrConv("ABCDEFG", vbFromUnicode)' Convert string. For i = 0 To UBound(x) Debug.Print x(i) Next End Sub .................................. Return Top .................................... Tn hm: String M t : String (Num as Long, character)

Tr v m t chui (theo d ng variant) g m cc k t l p l i. K t l p l i l k t u c a bi u th c chu i c truy n tham s th hai c a hm (character). Tham s : Tham s (Num) xc nh n s l n l p l i k t (character). V d : str = string (5, "a") str = string (5, "abc") str = string (5, 97) c 3 v d ny u cho ra chu i "aaaaa" Tham kh o thm: Space Return Top .................................. .................................... Tn hm: StrReverse M t : StrReverse(expression) o chu i expression Tham s : expression Chu i c n o V d : Option Explicit Private Sub Form_Load() Dim sReverse As String sReverse = StrReverse("1234567")' Returns "7654321" End Sub Return Top ..................................... .................................. Tn hm: Switch M t : Switch(expr-1, value-1[, expr-2, value-2 [, expr-n,value-n]]) C u trc r nhnh a i u ki n Tham s : expr Bi u th c value Gi tr tr v n u ng V d : Option Explicit Function MatchUp(CityName As String) MatchUp = Switch(CityName = "London", "English", CityName _ = "Rome", "Italian", CityName = "Paris", "French") End Function Private Sub Form_Load() Dim Result Result = MatchUp("London")' Returns English Result = MatchUp("Rome")' Returns Italian Result = MatchUp("Paris")' Returns French End Sub Tham kh o thm: Choose Iif Return Top ..................................... .................................. Tn hm: Tan M t : Tan(Number) L y tang c a s Tham s : Number S c n tnh V d :

Number

Option Explicit Private Sub Form_Load() Dim MyAngle, MyCotangent MyAngle = 1.3' Khoi tao MyCotangent = 1 / Tan(MyAngle)' Tinh cotangent. End Sub Tham kh o thm: Cos Sin Sqr Atn Return Top ..................................... .................................. Tn hm: TimeSerial M t : TimeSerial(hour, minute, second) Chuy n cc thng s r i r c c a th i gian thnh m t chu i th i gian Tham s : hour Gi minute Pht second Giy V d : Option Explicit Private Sub Form_Load() Dim MyTime MyTime = TimeSerial(16, 35, 17)' Returns 4:35:17 PM. End Sub Tham kh o thm: DateSerial Return Top ..................................... .................................. Tn hm: TimeValue M t : TimeValue(time) Chuy n th i gian d ng chu i sang gi tr Tham s : time Th i gian c n chuy n V d : Option Explicit Private Sub Form_Load() Dim MyTime MyTime = TimeValue("4:35:17 PM")' Returns 4:35:17 PM End Sub Tham kh o thm: DateValue Return Top ..................................... .................................. Tn hm: Trim M t : Trim(string) C t b t t c cc k t tr ng d bn tri, bn ph i v Tham s : String Chu i c n c t V d : Option Explicit Private Sub Form_Load() Dim MyString, TrimString MyString = " <-Trim-> "' Khoi tao

gi a chu i string

TrimString = LTrim(MyString)' Returns = "<-Trim-> ". TrimString = RTrim(MyString)' Returns = " <-Trim->". TrimString = LTrim(RTrim(MyString))' Returns = "<-Trim->". TrimString = Trim(MyString)' Returns = "<-Trim->". End Sub Tham kh o thm: LTrim Rtrim Return Top .................................. .................................... Tn hm: TypeName M t : TypeName(varname) Xem tn ki u d li u c a bi n varname Tham s : varname Bi n mu n xem Ghi ch: ********Gi tr tr v ********* object type L m t objecttype Byte Ki u Byte Integer Ki u Integer Long Ki u Long integer Single Ki u Single Double Ki u Double Currency Ki u Currency Decimal Ki u Decimal Date Ki u Date String Ki u String Boolean Ki u Boolean Error Gi tr l i Empty Cha kh i t o Null Khng c d li u Object L m t object Unknown Cha bi t Nothing Khng thu c lo i no V d : Option Explicit Private Sub Form_Load() Dim NullVar, MyType, StrVar As String, IntVar As Integer, CurVar As Currency Dim ArrayVar(1 To 5) As Integer NullVar = Null' Gan gia tri Null cho bien NullVar MyType = TypeName(StrVar)' Returns "String". MyType = TypeName(IntVar)' Returns "Integer". MyType = TypeName(CurVar)' Returns "Currency". MyType = TypeName(NullVar)' Returns "Null". MyType = TypeName(ArrayVar)' Returns "Integer()". End Sub .................................. Return Top .....................................

Tn hm: UCase M t : UCase(string) Chuy n ton b ch trong chu i string thnh ch hoa Tham s : string Chu i c n chuy n V d : Option Explicit Private Sub Form_Load() Dim UpperCase, LowerCase LowerCase = "Hello WORLD 1234"' Khoi tao UpperCase = UCase(UpperCase)' Returns "HELLO WORLD 1234". End Sub Tham kh o thm: Lcase Return Top ..................................... .................................. Tn hm: Unlock M t : Unlock [#Filenum, Expression] M kho t p tin, tham s s d ng nh Lock. Ghi ch: C n b o m lo i b t t c cc kho v i cu l nh Unlock tng ng tr c khi ng t p tin ho c thot kh i chng trnh(cc i s ph i tng h p chnh xc). N u khng t p tin c th b r i lo n. V d : Lock #Filenum, 1 To 100 ---> s kho 100 byte t byte th 1 Unlock #Filenum, 1 To 100 ---> s m kho 100 byte t byte th 1 Tham kh o thm: Lock Return Top ..................................... .................................. Tn hm: Val M t : Val(string) Chuy n chu i string thnh s Tham s : String Chu i c n chuy n V d : Option Explicit Private Sub Form_Load() Dim MyValue MyValue = Val("2457")' Returns 2457. MyValue = Val(" 2 45 7")' Returns 2457. MyValue = Val("24 and 57")' Returns 24. End Sub Tham kh o thm: FormatCurrency FormatNumber FormatPercent Str Return Top .................................... .................................. Tn hm: VarType M t : VarType(varname) Xem gi ki u d li u c a bi n varname Tham s : varname Bi n mu n xem Ghi ch: ********Gi tr tr v ********* vbEmpty = 0

Cha kh i t o vbNull = 1 Khng c d li u vbInteger = 2 Ki u Integer vbLong = 3 Ki u Long integer vbSingle = 4 Ki u Single vbDouble = 5 Ki u Double vbCurrency = 6 Ki u Currency vbDate = 7 Ki u Date vbString = 8 Ki u String vbObject = 9 L m t Object vbError = 10 Gi tr l i vbBoolean = 11 Ki u Boolean vbVariant = 12 Ki u Variant vbDataObject = 13 L m t DataObject vbDecimal = 14 Ki u Decimal vbByte = 17 Ki u Byte vbUserDefinedType = 36 1 ki u xc nh trong Variant vbArray = 8192 Ki u Array V d : Option Explicit Private Sub Form_Load() Dim IntVar, StrVar, DateVar, MyCheck ' Khoi tao cac bien IntVar = 459: StrVar = "Hello World": DateVar = #2/12/1969# MyCheck = VarType(IntVar)' Returns 2. MyCheck = VarType(DateVar)' Returns 7. MyCheck = VarType(StrVar)' Returns 8. End Sub .................................. Return Top ..................................... Tn hm: Weekday M t : Weekday(date, [firstdayofweek]) Xem t i th i i m date l th m y Tham s : date Ngy thng firstdayofweek Ngy u tin trong tu n Ghi ch: Dng cho tham s firstdayofweek vbUseSystem = 0 S d ng NLS API setting. VbSunday = 1 Sunday (default) vbMonday = 2 Monday vbTuesday = 3 Tuesday

vbWednesday = 4 Wednesday vbThursday = 5 Thursday vbFriday = 6 Friday vbSaturday = 7 Saturday ********Gi tr tr v ********* VbSunday = 1 Sunday vbMonday = 2 Monday vbTuesday = 3 Tuesday vbWednesday = 4 Wednesday vbThursday = 5 Thursday vbFriday = 6 Friday vbSaturday = 7 Saturday V d : Option Explicit Private Sub Form_Load() Dim MyDate, MyWeekDay, MyNameWeekDay MyDate = #8/10/2003#' Khoi tao MyWeekDay = Weekday(MyDate)' Returns 1 MyNameWeekDay = WeekdayName(MyWeekDay, True)'Returns "Sun" MyNameWeekDay = WeekdayName(MyWeekDay)'Returns "Sunday" End Sub Tham kh o thm: WeekdayName Return Top .................................... .................................. Tn hm: WeekdayName M t : WeekdayName(weekday, abbreviate, firstdayofweek) Chuy n weekday t gi tr sang chu i Tham s : weekday Ngy c n chuy n abbreviate Vi t t t firstdayofweek Ngy u tin trong tu n Ghi ch: Dng cho tham s firstdayofweek vbUseSystem = 0 S d ng NLS API setting. VbSunday = 1 Sunday (default) vbMonday = 2 Monday vbTuesday = 3 Tuesday vbWednesday = 4 Wednesday vbThursday = 5 Thursday vbFriday = 6 Friday vbSaturday = 7 Saturday V d :

Option Explicit Private Sub Form_Load() Dim MyDate, MyWeekDay, MyNameWeekDay MyDate = #8/10/2003#' Khoi tao MyWeekDay = Weekday(MyDate)' Returns 1 MyNameWeekDay = WeekdayName(MyWeekDay, True)'Returns "Sun" MyNameWeekDay = WeekdayName(MyWeekDay)'Returns "Sunday" End Sub Tham kh o thm: WeekDay Return Top ................................... .................................. Tn hm: Write M t : Write [#Filenum, Expression] Ghi ln File Tham s : #Filenum : ch s c a t p tin c n ghi. Expression : gi tr mu n ghi. V d : Option Explicit Private Sub Form_Load() Dim MyIndex, FileNumber For MyIndex = 1 To 5 FileNumber = FreeFile' Gan FileNumber = Trong Open "TEST" & MyIndex For Output As #FileNumber' mo file Write #FileNumber, "This is a sample."' Ghi vo file Close #FileNumber' Dong file Next MyIndex End Sub Tham kh o thm: Open FreeFile Return Top ..................................... .................................. Tn hm: Year M t : Year(date) L y thng tin nm t chu i date Tham s : date Ngy thng V d : Option Explicit Private Sub Form_Load() Dim MyDate, MyYear MyDate = #8/10/2003#' Khoi tao MyYear = Day(MyDate)' Returns 2003 End Sub Tham kh o thm: Day Month

You might also like