Default Values (Access)

You might also like

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 5

Default Values

Introduction When you create a Microsoft Access database, to assist the person doing data entry, you can provide a value for a certain field so the user would not need to change it if not necessary. This is referred to as the default value. To be efficient, the default value should be the most commonly used value for that particular field. or e!ample, when creating a table of employees that has a field called "ountry, since employees are most li#ely to reside in one country, you can provide the name of the country as the default value. To provide a default value, when creating the field in the Design View of the table $or form%, you can access the Default Value property of the field and type the desired value. &. If you want to follow this e!ample, create a database called Drug "ontrol '. (se the Table Wi)ard to create a table based on the *mployees table and select the following fields+ *mployeeID, irst,ame, -ast,ame, .ome/hone, Address, "ity, 0tate, /ostal"ode, and "ountry 1. rom the Mailing -ist 0ample Tables, add the following two fields+ *mailAddress and ,otes 2. "lic# ,e!t a few times and accept the name of the table as *mployees then clic# inish 3. 0witch the table to Design View 4. "hange the name of the /ostal"ode field to 5I/"ode

6. 0et the Default Value of "ountry to (0A 7. 0ave and close the table 8. (sing Auto orm, generate a form for the *mployees table and design it as you see fit+

&9. 0ave the form as *mployees and switch it to Design View "onditional Default Values In some cases, you may want to provide a default value but the value would depend on another value. or e!ample, if you are familiar with the system of telephone numbers in (0 and "anada, you may

#now that telephone numbers are defined using the following formula $999% 999:9999. They also follow certain rules+ &. The first three digits must be uni;ue in the country and they cover a certain area inside of a state or province. or e!ample, at the time of this writing, '9' is used for Washington D" while 19& covers part of $southern% Maryland '. The rest of the combination $the second three digits followed by the last four digits% must by uni;ue in the area covered by the first three digits. or e!ample, a telephone number should be provided to only one entity such as one household or family <f course, there are other rules we will not care about at this time. =ased on this, when the user enters a telephone number, you may want to provide a default value for the state or province. To do this+ &. While the form is in Design View, double:clic# the /hone,umber te!t bo! to access its /roperties window '. In the /roperties window, clic# the *vents tab. >ight:clic# After Update and clic# =uild... 1. In the "hoose =uilder dialog bo!, clic# "ode =uilder and clic# <? 2. Implement the AfterUpdate() event of the .ome/hone te!t bo! as follows+
Private Sub HomePhone_AfterUpdate() If Left(Me.HomePhone, 3) = !"! #hen Me.State = $% &'(eIf Left(Me.HomePhone, 3) = 3") #hen Me.State = M$ &'(eIf Left(Me.HomePhone, 3) = *"3 #hen Me.State = +A &nd If &nd Sub

3. >eturn to Microsoft Access 4. In the .ome /hone te!t bo!, type 19&4'&&&94+

6. When you press *nter or Tab, notice that the 0tate te!t bo! gets filled with MD, ac#nowledging

that a Maryland telephone number was entered 7. 0ave the form and switch it bac# to Design View Advanced Default Values The default value offered by Microsoft Access can be used only in the Default Value property of a field and can be specified in the Design View of the table or the form. If you want to use a conditional default value as we have done above, you would thin# of writing an e!pression either in the Default Value of the field in the table Design View or better, the >ecord 0ource of the field in the form@s Design View. (nfortunately, Microsoft Access doesn@t ac#nowledge this. This is why you have to use V=A. *ven if you create a Te!t =o! control and use it to hold the desired value. <nce you write an e!pression in its >ecord 0ource or directly in one of its events in the /roperties window, the user would not be allowed to enter any value in the field. This is one more reason V=A would be used. The main advantage of using V=A for a default value is that you can still let the user change the value of a field even if you had provided your suggestion through a default value. or e!ample, if you create a table for employees and decide to let the user assign a username or an *mail Address to employees, imagine you create an e!pression as a default value that uses the employees last name preceded by the first letter of his or her first name, there is still a chance that the company would have Aeannine Walter and Aimmy Walter in the database. While it is impossible to predict all possible scenarios, which is the reason most $if not all% companies employ a person in charge of assigning usernames andBor email address, you can provide preliminary code that can assist the user. <nce your e!pression has been applied, the user can still change your suggestion if it doesn@t fit or should not be accepted. &. To create a suggested email address for a new employee, on the form, clic# the -ast ,ame te!t bo! '. In the /roperties window, right:clic# After (pdate and clic# =uild... 1. In the "hoose =uilder dialog bo!, double:clic# "ode =uilder and implement it as follows+
Private Sub La(t,ame_AfterUpdate() Me.EmailAddress = LCase(IIf(Me.FirstName = "", "", Left(FirstName, 1)) _ & Me.LastName) & "@drug !"tr!l.md.us" &nd Sub

2. >eturn to Microsoft Access. "lic# irst ,ame, type Cermaine and press *nter. "lic# -ast ,ame, type Dufois 3. /ress *nter. ,otice that the *mail Address te!t bo! gets filled with gdufoisDdrugcontrol.md.us 4. To modify the suggested email address, change the string in the *mail Address te!t bo! to gdufois&'22Dstopdrugus.net and press *nter 6. ,otice that you are able to change the default value 7. To create more elaborate or other default values, return to Visual =asic and change the After (pdate event of the .ome/hone field as follows+
Private Sub HomePhone_AfterUpdate() If Left(Me.HomePhone, 3) = !"! #hen Me.State = $% &'(eIf Left(Me.HomePhone, 3) = 3") -r Left(Me.HomePhone, 3) =

-r Left(Me.HomePhone, 3) = .)" -r Left(Me.HomePhone, 3) #hen Me.State = M$ &'(eIf Left(Me.HomePhone, 3) = *"3 -r Left(Me.HomePhone, 3) = /." _ -r Left(Me.HomePhone, 3) = 0". -r Left(Me.HomePhone, 3) = .3. #hen Me.State = +A &nd If = ..3 If Left(Me.HomePhone, 3) = !"! #hen Me.%it1 = 2a(hin3ton &'(eIf Left(Me.HomePhone, 3) = !." #hen Me.%it1 = Si'ver Sprin3 &'(eIf Left(Me.HomePhone, 3) = .)" -r Left(Me.HomePhone, 3) = ..3 #hen Me.%it1 = 4a'timore &'(eIf Left(Me.HomePhone, 3) = .3. #hen Me.%it1 = %har'otte(vi''e &nd If &nd Sub

!."

8. "lose Visual =asic and test the form

You might also like