Conditions in VBA


Option Compare Database
Sub IfTest()

Dim curTotal As Currency, curComm As Currency

curTotal = InputBox("Please Enter the Sale Amount")
if curTotal >= 10000 Then
    curComm = curTotal * 0.08
ElseIf curTotal >= 1000 Then curComm = curTotal * 0.06
Else
    curComm = curTotal * 0.05
End If

MsgBox(curComm)

End Sub


' for more than two condition check use case statements
Option Compare Database
Sub CalcComm()

Dim curTotal As Currency, curComm As Currency

curTotal = InputBox("Please Enter the Sale Amount")

Select Cast curTotal

Case Is >= 10000
    curComm = curTotal * 0.08
Case Is >= 1000
    curComm = curTotal * 0.06
Case Is >= 500
    curComm = curTotal * 0.05
Case Else
    curComm = curTotal * 0.08
End Select

MsgBox(curComm)

End Sub
Option Compare Database
Option Explicit



Private Sub Volume_GotFocus()


Dim frm As Form, textBoxVolume As TextBox, textBoxCapacity As TextBox, textBoxQuantity As TextBox, comboBoxUOM As ComboBox, longItemIndex As Long

' Set uomControl = Forms!orders.UOM
Set frm = Forms!orders
Set textBoxCapacity = frm.Capacity
Set textBoxQuantity = frm.Quantity
Set textBoxVolume = frm.Volume
Set comboBoxUOM = frm.UOM
' longItemIndex = ComboBoxValue()
' If longItemIndex = 1 Then
If comboBoxUOM = 1 Then
textBoxVolume = textBoxCapacity * textBoxQuantity
Else: If comboBoxUOM = 2 Then InputBox ("Type Your Volume")
End If
End Sub

CSng()

textBoxVolume.Enabled = False

Option Compare Database
Option Explicit


Private Sub Volume_GotFocus()


Dim frm As Form, textBoxVolume As TextBox, textBoxCapacity As TextBox, textBoxQuantity As TextBox, comboBoxUOM As ComboBox, longItemIndex As Long, customVolume As Double

' Set uomControl = Forms!orders.UOM
Set frm = Forms!orders
Set textBoxCapacity = frm.Capacity
Set textBoxQuantity = frm.Quantity
Set textBoxVolume = frm.Volume
Set comboBoxUOM = frm.UOM
' longItemIndex = ComboBoxValue()
' If longItemIndex = 1 Then
Select Case comboBoxUOM
Case Is = 1
textBoxVolume = textBoxCapacity * textBoxQuantity
Case Is = 2
customVolume = CSng(InputBox("Type Your Volume"))
textBoxVolume = customVolume
textBoxQuantity = customVolume / textBoxCapacity
Case Is = 3
textBoxVolume = textBoxCapacity * textBoxQuantity
Case Else
MsgBox ("Success")
End Select
End Sub
Previous
Next