Events and SetFocus before reading control

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.


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
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Hi Parry, you need to use the BeforeUpdate event of the form to prevent records being saved with inappropriate or missing values.

Denis
 
Upvote 0
Cheers thanks mate. What about reading values in fields on the form? In Excel, I sont need to set focus to the field to read its contents but it appears this is the case in Access. Is this correct?
 
Upvote 0
parry said:
Cheers thanks mate. What about reading values in fields on the form? In Excel, I sont need to set focus to the field to read its contents but it appears this is the case in Access. Is this correct?

Parry,

Use the Value property instead. The Text property is only available when the control has focus. From the help file:-



<u>Remarks</u>

You can set the Text property to the text you want to display in the control.
You can also use the Text property to read the text currently in the control.



You can set or read this property only by using a macro or Visual Basic.



<font color="#FF0000">Note To set or return a control's Text property,
the control must have the focus, or an error occurs. To move the focus to a
control, you can use the SetFocus method or GoToControl action.</font>



While the control has the focus, the Text property contains the text data
currently in the control; the Value property contains the last saved data for
the control. When you move the focus to another control, the control's data is
updated, and the Value property is set to this new value. The Text property
setting is then unavailable until the control gets the focus again. If you use
the Save Record command on the Records menu to save the data in the control
without moving the focus, the Text property and Value property settings will be
the same.
 
Upvote 0
'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

This could just be done in the validation property of the control without needing any code

HTH

Peter
 
Upvote 0

Forum statistics

Threads
1,221,566
Messages
6,160,525
Members
451,655
Latest member
rugubara

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top