13
Decision making in VBA

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

Embed Size (px)

Citation preview

Page 1: 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

Decision making in VBA

Page 2: 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

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

Page 3: 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

Disable next record button

Private Sub Form_Current()

If Me.NewRecord = True Then cmdPrevRec.SetFocus cmdNextRec.Enabled = FalseEnd If

End Sub

Page 4: 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

Re-enable next record button

Private Sub Form_Current()If Me.NewRecord = True Then cmdPrevRec.SetFocus cmdNextRec.Enabled = FalseElse cmdNextRec.Enabled = TrueEnd IfEnd Sub

Page 5: 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

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

Page 6: 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

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.

Page 7: 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

Display error number

Private Sub Form_Error(DataErr As Integer, Response As Integer) MsgBox DataErrEnd Sub

Page 8: 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

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

Page 9: 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

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

Page 10: 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

Function without error handling

Public Sub TestErrors()

Dim dblResult As Double dblResult = 10 / InputBox("Enter a number") MsgBox "The result is " & dblResultEnd Sub

Page 11: 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

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

Page 12: 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

Select Case statement

Syntax

Select Case testexpression[Case expressionlist-n[statements-n]] ...[Case Else[elsestatements]]

End Select

Page 13: 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

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