Computed Field in ACCESS

rod_ampon

New Member
Joined
Mar 14, 2005
Messages
11
Hi,

I'm trying to create a data entry form that will validate the data entered by user. The requirement is as follows:

columns:
Date of Birth, Age, Lastname

validation:
Age = can only accept >4 and <16

computed field:
Age = computed based on Date of Birth and current year.

error tracking:
User may only enter data with age >4 and <16, if not, message will be displayed and focus will go back to "Date of Birth" column.

code:
Option Compare Database
Dim AgeNumber As Integer

Private Sub DOB_AfterUpdate()

AgeNumber = Year(Now()) - Year(Me.DOB)
If AgeNumber > 4 And AgeNumber < 16 Then
Me.AGE = Year(Now()) - Year(Me.DOB)
Else
MsgBox "Can only accept age between 4 and 16 yrs old.", vbInformation, "Error..."
Me.DOB.SetFocus
End If

End Sub


Above script not working and focus will still go to the next column which is "Last Name"

Your help will be very much appreciated.

Thanks,
Rod
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
You should use the BeforeUpdate event for this. You can then put Cancel = True after your MsgBox statement and focus should remain in your DOB field.

Another way would be to set the Validation Rule and Validation Text properties for the field in the underlying table's design view.
 
Upvote 0

Forum statistics

Threads
1,221,875
Messages
6,162,563
Members
451,775
Latest member
Aiden Jenner

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