61. Private Sub Command1_Click()
Dim a As Integer
Dim b As Integer
a = Text1.Text
b = Text2.Text
Label3.Caption = a & " / " & b
& " = " & a / b
Label4.Caption = a & " " & b
& " = " & a b
Label5.Caption = a & " mod " &
b & " = " & a Mod b
65. te Sub Command1_Click()
a As String
b As String
ext1.Text
ext2.Text
3.Caption = a & " > " & b & " = " & (a > b
4.Caption = a & " < " & b & " = " & (a < b
5.Caption = a & " = " & b & " = " & (a = b
6.Caption = a & " <> " & b & " = " & (a <>
ub
67. ลำาดับการคำานวณลำาดับการคำานวณ
Arithmetic Comparison Logical
Exponentiation (^) Equality (=) Not
Negation (-) Inequality (<>) And
Multiplication and division
(*, /)
Less than (<) Or
Integer division () Greater than (>) Xor
Modulus arithmetic
(Mod)
Less than or equal to
(<=)
Eqv
Addition and subtraction
(+, -)
Greater than or equal to
(>=)
Imp
String concatenation (&) Is &
83. Dim a As Date
Dim b As Date
Sub Command1_Click()
a = Time
Text1.Text = Format(a,
"HH:MM:SS")
Text2.Text =""
Label1.Caption =""
End Sub
ตัวอย่างงาน
84. Sub Command2_Click()
b = Time
Text2.Text = Format(b,
"HH:MM:SS")
End Sub
Sub Command3_Click()
Label1.Caption =
Format(b - a, "HH:MM:SS")
End Sub
ตัวอย่างงาน
94. te Sub Command1_Click()
Text1.Text = "basic" Then
Form1.Show
Unload Me
se
Text1.Text = ""
Text1.SetFocus
nd If
Sub
Private Sub Command2_C
End
End Sub
Lcase(Tex
t1.text)
ตัวอย่างงาน
99. ตัวอย่างงาน
Select Case score
Case Is >= 80: grade = "A"
Case Is >= 70: grade = "B"
Case Is >= 60: grade = "C"
Case Is >= 50: grade = "D"
Case Else: grade = "F"
End Select
Label3.Caption = Text1.Text & "
your grade is " & grade
End Sub
110. Private Sub Command1_Click()
Text1.SetFocus
If Option1.Value = True Then
Label4.Caption =
Val(Text1.Text) * Val(Text2.Text)
Else
Label4.Caption = 0.5 *
Val(Text1.Text) * Val(Text2.Text)
End If
End Sub
115. If Option3.Value = True Then
Label1.Caption = "เลขา"
ElseIf Option4.Value = True Then
Label1.Caption = "ประชาสัมพันธ์"
ElseIf Option5.Value = True Then
Label1.Caption = "แม่บ้าน"
End If
If
Sub
153. ตัวอย่างงาน
แสดงตัวเลข 1 ถึง 20
Private Sub
Form_Click()
Dim i As
Integer
i = 1
Do While (i <=
20)
Print i
i = i + 1
Private Sub
Form_Click()
Dim i As
Integer
i = 1
Do
Print i
i = i + 1
Loop Until (i
154. ตัวอย่างงาน
แสดงตัวเลข 10 ลงไปหา 1
Private Sub
Form_Click()
Dim i As
Integer
i = 10
Do While (i >=
1)
Print i
i = i - 1
Private Sub
Form_Click()
Dim i As
Integer
i = 10
Do
Print i
i = i - 1
Loop Until (i
170. ivate Sub Form_Load()
List1.AddItem "Widows"
List1.AddItem "Word"
List1.AddItem "Excel"
List1.AddItem "Access"
List1.AddItem "Power Point"
d Sub
หรื
อ
171. Private Sub Form_Load()
With List1
.AddItem "Widows"
.AddItem "Word"
.AddItem "Excel"
.AddItem "Access"
.AddItem "Power Point"
End With
End Sub
172. ate Sub List1_DblClick()
st2.AddItem List1.Text
st1.RemoveItem List1.ListIndex
Sub
ate Sub List2_DblClick()
st1.AddItem List2.Text
st2.RemoveItem List2.ListIndex
Sub
175. ate Sub Command2_Click()
List1.ListIndex >= 0 Then
List1.RemoveItem List1.ListIndex
List1.ListIndex =0
nd If
Sub
ate Sub Command3_Click()
st1.Clear ' ลบข้อมูลทั้งหมด
Sub
178. Command1_Click()
As Integer
n As Integer
List1.ListCount
0
While (i < n)
List1.Selected(i) Then
List2.AddItem (List1.List
List1.RemoveItem (i)
n = List1.ListCount
Else
i = i + 1
End If
Loop
End Sub
188. Dim n As Integer
Dim Ans As
String
Private Sub
Form_Load()
n = 0
189. e Sub Command1_Click()
= InputBox("วิชำนี้คือวิชำ ?" & Chr
"VB, Delphi", "Question")
ase(Ans) = "vb" Then
= n + 1
If
mand2.Enabled = True
mand1.Enabled = False
ub
190. te Sub Command2_Click()
= InputBox("ตัวอย่ำงนี้ใช้ฟังก์ชันใด
r(13) & "MsgBox, InputBox", "Que
Case(Ans) = "inputbox" Then
= n + 1
d If
mmand3.Enabled = True
mmand2.Enabled = False
Sub
196. Sub File1_Click()
Text1.Text = File1.Path
Text2.Text =
File1.FileName
End Sub
Drive1_Change()
r1.Path = Drive1.Drive
Sub
Sub Dir1_Change()
File1.Path = Dir1.P
End Sub
214. กำำหนดให้ Label วิ่งรอบฟ
m fac As String
vate Sub Timer1_Timer()
fac = "r" Then
Label1.Left = Label1.Left + 75
Label1.Top = Label1.Top - 50
If Label1.Top < 0 Then
fac = "t"
End If
Sub Form_Load
fac = "b"
End Sub
215. ac = "t" Then
el1.Left = Label1.Left + 75
el1.Top = Label1.Top + 50
abel1.Left >= Me.Width - 100
c = "l"
If
216. ac = "l" Then
l1.Left = Label1.Left - 50
l1.Top = Label1.Top + 50
bel1.Top >= Me.Height - 10
c = "b"
f
217. seIf fac = "b" Then
Label1.Left = Label1.Left - 50
Label1.Top = Label1.Top - 50
If Label1.Left < 0 Then
fac = "r"
End If
d If
d Sub
243. Dim d As Boolean
Dim CurrentX as
Single,CurrentY as Single
Sub
Form_MouseDown(Button
As Integer, _
Shift As Integer, X As
Single, Y As Single)
d = True
246. orm_MouseMove(Button As Integer,
As Integer, X As Single, Y As Single)
= True Then
Button = 1 Then
Line -(X, Y)
se
Circle (X, Y), 100, QBColor(Rnd * 15)
nd If
If
ub
กดเมาส์ซ้ายค้างวาดเส้น กดเมาส์
ขวาค้างวาดวงกลม
247. Sub
Form_MouseMove(Button
As Integer, _
Shift As Integer, X As
Single, Y As Single)
If d = True Then
If Button = 1 And Shift
= 1 Then
Circle (X, Y), 100,
QBColor(Rnd * 15)
กดเมาส์ซ้ายค้างวาดเส้น กดเมาส์ซ้าย
ค้างพร้อมปุ่ม Shift วาดวงกลม
259. Private Sub
cmdShowFont_Click()
dlg1.Flags = cdlCFBoth
dlg1.ShowFont
Label1.FontName=dlg1.Font
Name
Label1.FontSize=dlg1.FontSi
ze
Label1.FontBold=dlg1.FontB
การเรียกใช้ Dialog Font
274. b mnuform1_Click()
orm1.Show
d Sub
b mnuform2_Click()
orm2.Show
d Sub
Sub mnuform3_Clic
Form3.Show
End Sub
Sub mnuexit_Click(
End
End Sub
วามแตกต่างระหว่างฟอร์ม 2 และ 3
281. ขอบเขตของ Sub
Program
•Private Sub ชื่อโปรแกรม
ย่อย (parameter)
เป็นโปรแกรมที่ใช้ได้ใน
Form นั้นๆ
•Public Sub ชื่อโปรแกรม
ย่อย (parameter)
เป็นโปรแกรมที่ใช้ได้ใน
ทุกๆ Form
288. able(r As Integer, c As Integer)
As Integer
i = 1 To r + 1
ine (500, 500 * i)-(500 * (c + 1), 50
xt i
r i = 1 To c + 1
ine (500 * i, 500)-(500 * i, 500 * (r
xt i
ub
299. Private Sub
Command1_Click()
Dim n(2) As Integer
Dim i As Integer
Dim sum As Integer
sum = 0
For i = 0 To 2
n(i) =
Val(Text1(i).Text)
sum = sum + n(i)
Next i
Label4.Caption =
308. e Sub Command1_Click() ' ป
1.Recordset.AddNew
1.SetFocus
mand2.Visible = True
mand1.Visible = False
ub
309. e Sub Command2_Click() 'ป
a1.Recordset.Update
mmand1.Visible = True
mmand2.Visible = False
ub
ทำาการรันให้ ปุ่ม OK ซ้อนปุ่ม Add
310. te Sub Command3_Click() 'ปุ่ม
MsgBox("Delete " & Text2.Tex
s or No ?", 1, "Check Data")
= 1 Then
a1.Recordset.Delete
a1.Recordset.MoveFirst
d If
Sub
314. m t As String
vate Sub Command5_Click() 'ปุ่ม Find
f Option1.Value = True Then
t = "name='" & Text8.Text & "'"
ElseIf Option2.Value = True Then
If Option4.Value = True Then
t = "sex='f'"
Else
t = "sex='m'"
End If
315. f Option3.Value = True Then
= "position='" & Combo1.Text
If
ta1.Recordset.FindFirst t
f Data1.Recordset.NoMatch Th
MsgBox "Data Not Found", 16
d If
Sub
ต่อปุ่ม Find
316. Sub Command6_Click() 'ปุ่ม Fin
1.Recordset.FindNext t
Data1.Recordset.NoMatch Then
gBox "End of Data", 32
f
b
317. ตัวอย่างงาน
ame และ password เพื่อเข้าไปใช้ Databas
in และ password = siam สามารถใช้ Data
และ password = masi สามารถใช้ Databa
319. Else
MsgBox "Password or User
name not Correct", 16
Text1.SetFocus
Text1.SelStart = 0
Text1.SelLength =
Len(Text1.Text)
Text2.Text = ""
End If
Sub Command2_Click()
End
End Sub