You are on page 1of 9

lalu kita akan membuat form dengan design seperti dibawah ini :

dimana ada tombol cari yang diisi coding Visual basic.


Coding Visual basic pada tombol cari dengan syntax sebagai berikut :
Set db = CurrentDb()
Set rs1 = db.OpenRecordset(Daftar Karyawan, dbOpenTabe)
!"##.Set$ocus
rs1."nde% = !"##
rs1.See& =, !"##.Te%t
"f !ot rs1.!o'atc( T(en
!a)a.Set$ocus
!a)a.Te%t = rs1*!a)a
+abatan.te%t=rs1*+abatan
,se
's-bo%Data t.da& d.te)u&an
!"##.setfocus
!"##.te%t=/
,nd "f
semoga membantu
A Pop-up Calendar for your Access
Forms
Why Use a Calendar?
Getting dates entered accurately always manages to be a problem. Everyone seems to
have their own way of writing a date. In the !" the standard format is month#day#year
whereas most of Europe uses day#month#year. $hat about date separators... should you
use a slash% a dash or a dot% or is it &' to use nothing at all( &r can you type something
like )*+!ep+,)( In fact% most of these options are acceptable date formats and can be
catered for with $indows settings% and even field property settings in "ccess tables and
forms.
-ifficulties arise when you create an unbound text box to accept a date on a form that is
designed to% for example% pass criteria to a .uery. /ecause there is no underlying form
field to re0ect incorrect dates% you need some other way to make sure that the user has
entered good data. !ometimes you 0ust don1t know what date to enter... 2the 3rd 4hursday
in "pril next year2.
$hat you really need is a calendar% and thankfully 5icrosoft has provided us with one
that can be placed on a form. 4his tutorial shows you how to find the calendar tool and
use it to include pop+up calendars on your "ccess forms% and explains how to add the
simple V/" programming instructions necessary to make the calendar work.
How to Add a Calendar to an Access Form
4here are three steps to creating a pop+up calendar. 6irst you must prepare the form% then
draw and customi7e the calendar to your re.uirements% and finally add the V/" code
which will drive the process.
4he calendar will be hidden until the user clicks the form1s date field% when the calendar
will appear. $hen a date is chosen the calendar will pass the it to the date field before
being hidden again. If the user uses their 84"/9 key to enter the date field% the calendar
will not appear% and they will be able to type a date directly into the field if they wish.
Step 1 Prepare the Form
-ate fields on forms are usually represented by simple 4ext /ox controls. 4he first step is
to convert your date field text box into a Combo /ox. 4he reason for this is to prompt the
user to click it: $hen a user sees the arrow button on a combo box they know that
clicking it will reveal a list. In this case% instead of a list% they will see a calendar.
!elect the date field text box on your form and go to Format ! Chan"e #o !
Combo $o%...
4his will convert your existing text box to a combo box. "lternatively you could draw a
new combo box on your form with the Combo /ox tool in the form design toolbox.
;;;

Step & 'raw the Calendar
<ou won1t find the a calendar tool in the form design toolbox but one is provided as an
2optional extra2. Click the (ore Controls button on the toolbox% and wait a moment
while "ccess builds its list of available controls. 4hen look for Calendar Control 1)*)
=for "ccess ),,)% or +*) in "ccess ),,,% or ,*) in "ccess >*? and select it...
;;;
4he mouse pointer will turn into the Calendar Control tool and you can click anywhere
on your form to insert a calendar...
<ou will probably want to resi7e and position the calendar to suit the design of your
form. -ouble+click the calendar to open its own properties window =alternatively% right+
click the calendar and choose Properties in the usual way% then go to the -ther tab and
find Custom. Click the build button .***/ to open the custom properties window?. 4hese
properties are additional to the regular set of properties that you normally see for form
controls. <ou can change font% colour scheme% and layout to suit your own re.uirements.
In this example% I want to make the pop+up calendar .uite small% so I have opted not to
see the 'ont(01ear T.te and also set the font si7e to ,. <ou can resi7e the calendar in the
normal way by dragging one of the handles around its edge. I have also given the
calendar a si0e 1 border using the 1ine2$order Width tool on the form design toolbar.
@ere1s how my calendar will look in $or) 2.ew...
Aemember that the calendar is going to be hidden until the user asks for it by clicking on
the date field combo box =you don1t have to do it this way but I prefer to:?. 5ake sure that
your form is big enough to display the calendar when it is un+hidden. If you wish% you
can place the calendar o3er existing controls. 4his does make building the form a bit
difficult but it can help to economise on space.
B&4E: 4he Caendar Contro is an "ctiveC control =actually a file called mscal*oc%?
supplied with 5icrosoft &ffice. It is normally installed with a standard installation of
5icrosoft &ffice% Excel or "ccess% but if you can1t find it on the list you will need to get
hold of a copy. If you are distributing your file% or planning to use it on more than one
computer% you will also need to make sure that the host computer has the mscal.ocx file
installed. <ou will find it on the C- that your copy of Excel came on% or you can
download a copy from my web site at: http:##www.fontstuff.com#mailbag#.vba,D.htm.
6inally% right+click on the calendar control and choose Properties to open its properties
sheet and on the Format tab set the 3isible property to 4o.
Step 5 Write the Code to Power the Calendar
Code to 'isplay the Calendar
4wo code procedures are re.uired: one to un+hide the calendar when the user clicks the
date field combo boxE and another to re+hide the calendar when the user chooses a date.
4he first procedure will be refined to match the calendar1s date with any date that is
already shown in the date field =otherwise to display the current date?. 4he second
procedure must include an instruction to transfer the chosen date to the date field combo
box.
&pen the properties sheet for the date field combo box and on the 67ent tab click in the
space next to -n (ouse 'own. 4hen click the build button .***/ to open the Choose
$uilder dialog% choose Code $uilder and click -8...
;;;
4he code editor window opens with the first and last lines of the date field combo1s
mouse down event procedure already written% and your cursor is in the space between.
Enter the following two lines% the first to unhide the calendar and the second to transfer
the focus to it:
ocxCalendar.Visible = True
ocxCalendar.SetFocus
B&4E: I have named the date field combo box on my form cboStartDate and the
calendar oc%Caendar. !ubstitute the names of your combo and calendar for these when
you write the code.
Bow enter the following lines% in which an "f State)ent checks the date field combo box
to see if it already contains a date. If it does% then the calendar is instructed to display this
date. If not% the procedure uses the V/" 'ate function to instruct the calendar to display
the current date:
If Not IsNull(cboStartDate) Then
ocxCalendar.Value = cboStartDate.Value
Else
ocxCalendar.Value = Date
End If
4he finished procedure should look like this:
Aeturn to your form and test the code =now is a good time to save the form in case
anything goes wrong + 0ust click the save button on the toolbar?. !witch the form to $or)
2.ew and click the form field combo box. 4he calendar should appear displaying either
today1s date =if the combo box was empty? or the same date as shown in the combo box.
Code to Hide the Calendar and #ransfer the 'ate
Aeturn to the code editor window and open the left+side combo box at the top of the code
window. 4his displays a list of ob0ects on the form. Choose the name of your calendar =in
this example oc%Calendar?% then open the right+side combo box to display a list of
procedures and choose Clic9. 4his prompts the editor to create the first and last lines of
the calendar1s click event procedure...
;;;
=B&4E: if the lines of any other procedure are created when you make the first choice
you can 0ust delete them.?
Enter the following lines of code:
cboStartDate.Value = ocxCalendar.Value
cboStartDate.SetFocus
ocxCalendar.Visible = False
4he first line transfers the date chosen in the calendar to the date field combo box. 4he
next line sets the focus back to the date field combo box. 4he last line hides the calendar
again. 4he finished code should look like this:
Aeturn to the form design window and save the form. !witch to $or) 2.ew and test your
calendar. If anything fails to work properly% go back to the code window and check your
typing:
<our pop+up calendar is finished: nless of course...
What About (ultiple Calendars?
It1s .uite likely that you will have more than one date field on a form. In which case% do
you need a separate calendar for each(... Bo:
Each date field combo box can call the same calendar control% but the calendar needs to
know which combo called it so that it can return a date to the correct one. 4his needs only
a minor change to the code we have already used.
'eclare the 3ariable
6irst of all% declare a variable to hold the name of the combo box that called the calendar.
I1ll name my variable cboOr.-.nator and declare it with the line...
Dim cbori!inator "s Combo#ox
...at the very top of the form1s code window after the Opt.on Co)pare Database and
Opt.on ,%p.c.t statements but before the first procedure% like this:
-eclaring the variable like this makes it global so that it will hold a value after the
procedure that gave it the value has finished% and can pass a value from one procedure to
another.
Pass the -ri"inator:s 4ame to the 3ariable
4here will be a series of almost identical 'ouseDown procedures% one for each date field
combo box. "dd a line to each one to pass the name of the originating combo box to the
variable:
Set cbori!inator = cboStartDate
B&4E: the use of the 2!et2 keyword when assigning a value to the variable. 4he set
keyword is re.uired when assigning a value to an ob4ect variable.
&ther references to the combo box in the procedure can now be written as cboOr.-.nator:
Pass the 'ate $ac9 to the -ri"inatin" Combo $o%
4he calendar1s C.c& procedure is similarly modified to pass the date back to the combo
box named in the cboOr.-.nator variable.
" final line:
Set cbori!inator = Nothin!
...empties the variable% ready for the next call.

You might also like