ໃນການຂຽນ Function ແລະ Sub Procedure ໃນ Visual Basic 6 ມີແບບຂຽນຄື:
1. ການສ້າງ Function
ແບບໂຄ້ງສ້າງ:
Public Function functionName (Arg As dataType,..........) As dataType ຫລື Private Function functionName (Arg As dataType,..........) As dataType
Public ແມ່ນຕົວຊີ້ບອກໃຫ້ Function ນັ້ນ ສາມາດເອີ້ນໃຊ້ໄດ້ໃນ Project ນັ້ນ
ຕົວຢ່າງໂປຣແກຣມ ໃຊ້ Function
Public Function FV(PV As Variant, i As Variant, n As Variant) As Variant 'Formula to calculate Future Value(FV) 'PV denotes Present Value FV = PV * (1 + i / 100) ^ n End Function Private Sub compute_Click() 'This procedure will calculate Future Value Dim FutureVal As Variant Dim PresentVal As Variant Dim interest As Variant Dim period As Variant PresentVal = PV.Text interest = rate.Text period = years.Text 'calling the function FutureVal = FV(PresentVal, interest, period) MsgBox ("The Future Value is" & FutureVal) End Sub
ແບບຟອມ
Image does not exist: https://image.ibb.co/eLoQDk/vbfunction.gif
2. ການສ້າງ Sub Procedure
ມີໂຄງສ້າງ
Sub ProcedureName (arguments) Statements End Sub
ຕົວຢ່າງໂປຣແກຣມ
Private Sub cmdCal_Click() Dim x As Single, y As Single x = Val(TxtNum1.Text) y = Val(TxtNum2.Text) sum x, y End Sub Sub sum(a As Single, b As Single) MsgBox ("sum=" & a + b) End Sub
ຟອມໂປຣແກຣມ
Image does not exist: https://image.ibb.co/exkcR5/vbprocedure.jpg
ຜົນ:
Image does not exist: https://image.ibb.co/dA4AeQ/vb6fig14_3b.jpg