parry
MrExcel MVP
- Joined
- Aug 20, 2002
- Messages
- 3,355
Hi, I am wondering whether I have done things properly with a form, and wanted to see if there is a better way.
Theres a form detailing revenue info and has a field where you can enter a rebate amount. If this value is entered then two other fields (AuthBy and Comments) fields must be completed.
Q1: Is the AfterUpdate event on the form the correct event? I want to ensure that saving/moving to another record will kick off the code.
Q2: In order to do the if statements to check the value of a control, I had to keep setting focus to it all the time. Is there another way? I also had problems with the last control being selected always being the FeeCharged field so decided to use the WhereAmI bit in the code.
Any suggestions appreciated.
Theres a form detailing revenue info and has a field where you can enter a rebate amount. If this value is entered then two other fields (AuthBy and Comments) fields must be completed.
Q1: Is the AfterUpdate event on the form the correct event? I want to ensure that saving/moving to another record will kick off the code.
Q2: In order to do the if statements to check the value of a control, I had to keep setting focus to it all the time. Is there another way? I also had problems with the last control being selected always being the FeeCharged field so decided to use the WhereAmI bit in the code.
Any suggestions appreciated.
Code:
Private Sub Form_AfterUpdate()
Dim WhereAmI As Control
Set WhereAmI = ActiveControl
'If RebateAmt > 0 then both AuthBy and Comments fields are compulsory
'Note: SetFocus is required before reading value of control.
RebateAmt.SetFocus
If RebateAmt.Text > 0 Then
'check if Authby is blank
AuthBy.SetFocus
If AuthBy.Text = "" Then
MsgBox "Please enter the name of the person authorising the rebate.", vbExclamation, _
"Authorised By name not entered"
Exit Sub
End If
'check if Comments is blank
Comments.SetFocus
If Comments.Text = "" Then
MsgBox "Please enter a reason for the rebate.", vbExclamation, _
"Comment Required"
Exit Sub
End If
End If
'check if Fee Charged is negative
FeeCharged.SetFocus
If FeeCharged.Value < 0 Then
RebateAmt.SetFocus
MsgBox "Are you trying to bankrupt us?" & Chr(10) & "How about reducing the rebate.", 16, _
"Fee Charged cannot be negative!"
End If
'Return focus to the last active control before error.
WhereAmI.SetFocus
End Sub