decision making in vba. current event occurs: when a form is opened when the focus leaves one record...
TRANSCRIPT
Decision making in VBA
Current Event
Occurs:
• When a form is opened
• When the focus leaves one record and moves to another
• Before the first or next record is displayed
Disable next record button
Private Sub Form_Current()
If Me.NewRecord = True Then cmdPrevRec.SetFocus cmdNextRec.Enabled = FalseEnd If
End Sub
Re-enable next record button
Private Sub Form_Current()If Me.NewRecord = True Then cmdPrevRec.SetFocus cmdNextRec.Enabled = FalseElse cmdNextRec.Enabled = TrueEnd IfEnd Sub
Disable previous record button
Private Sub Form_Current()If Me.NewRecord = True Then cmdPrevRec.Enabled = True cmdPrevRec.SetFocus cmdNextRec.Enabled = FalseElseIf Me.CurrentRecord = 1 Then cmdNextRec.Enabled = True cmdNextRec.SetFocus cmdPrevRec.Enabled = FalseElse cmdNextRec.Enabled = True cmdPrevRec.Enabled = TrueEnd IfEnd Sub
Trappable errors
• Can occur while application is running
• Each has a unique error number
• Type “Trappable errors” in VBA help to get complete listing
• When these errors occur for a form (or report) we can use the objects Error event to respond to them.
Display error number
Private Sub Form_Error(DataErr As Integer, Response As Integer) MsgBox DataErrEnd Sub
Code to handle trappable errors
Private Sub Form_Error(DataErr As Integer, Response As Integer) Select Case DataErr Case 2237 MsgBox "You have entered a title that is not in the list.” _ “
& “Click the list and choose one of the options shown." Response = acDataErrContinue Case 3314 MsgBox "You must enter both first and last name for the employee.” _ & “Fill in the missing value." Response = acDataErrContinue Case 3317 MsgBox "Employee start date must be today's date or earlier.” _
& “Please enter a valid date" Response = acDataErrContinue Case Else 'the default error handler displays VBA error message Response = acDataErrDisplay End SelectEnd Sub
Control Wizard error-handling
Private Sub cmdNextRec_Click()On Error GoTo Err_cmdNextRec_Click DoCmd.GoToRecord , , acNextExit_cmdNextRec_Click: Exit Sub
Err_cmdNextRec_Click: MsgBox Err.Description Resume Exit_cmdNextRec_Click End Sub
Function without error handling
Public Sub TestErrors()
Dim dblResult As Double dblResult = 10 / InputBox("Enter a number") MsgBox "The result is " & dblResultEnd Sub
Error handling added
Public Sub TestErrors2()
On Error GoTo Err_TestErrors
Dim dblResult As Double dblResult = 10 / InputBox("Enter a number") MsgBox "The result is " & dblResult Exit_TestErrors: Exit Sub
Err_TestErrors: Select Case Err.Number Case 11 'division by zero dblResult = 0 Resume Next Case 13 'type mismatch Resume Case Else MsgBox Err.Number & ": " & Err.Description Resume Exit_TestErrors End SelectEnd Sub
Select Case statement
Syntax
Select Case testexpression[Case expressionlist-n[statements-n]] ...[Case Else[elsestatements]]
End Select
Select Case Example
Public Function Discount(curAmount As Currency) Select Case curAmount Case Is >= 100 MsgBox "10% discount" Case Is >= 50 MsgBox "5% discount" Case Else MsgBox "No discount" End SelectEnd Function