If you're willing, let's change your table's design. Delete the "Clock-out"
field from the table. That will give us a single field ("Clock-in") to store
the type of "clock" procedure.
(Note: I also suggest that you change the field name to ClockIn -- eliminate
the hyphen from the name.)
I assume that you're using a query for the form's RecordSource. If yes, open
that query in design view and change it so that it contains the "ClockIn"
field and does not contain the previous fields (the ones that you deleted /
changed name).
Now, let's open your form in design view. In the Toolbox toolbar, click "on"
the Wizard button to turn the wizard on (the wizard to do option groups is
pretty good), then click on the Option group icon (a rectangular box with
XYZ at the top), then click on your form. The wizard will take you through
the setup for the option group -- select toggle buttons, set the value of
the Clock In button to -1, and the value of the Clock Out button to 0.
After the option group has been created on the form, set the ControlSource
of the option group itself to ClockIn field.
Now, we'll use the BeforeUpdate event of the option group control to see if
the most recently added record (prior to the current one) is a clock-in or a
clock-out record, and determine if that would be a "duplicate" of what the
user has selected. I assume that you have the timestamp field in the form's
RecordSource so that we can use its value in our test.
I am using "generic" names for fields and controls in the following code
example -- be sure to replace them with the real names.
We'll put an event procedure on the BeforeUpdate event of the option group.
While the form is in design view, click on the option group (not the toggle
button) and then open the Properties window. Select the Event tab. In the
box next to On Before Update, select [Event Procedure] from the dropdown
list. Then click on the three-dot button at far right of box -- this will
take you to the Visual Basic Editor (VBE).
In the VBE, you'll see a Private Sub line, an empty line, and an Exit Sub
line. In the empty line, type the following code steps (remember to replace
my generic names with the real names):
Dim blnValue As Boolean
Dim datTimeDate As Date
Dim strMsg As String
Dim varValue As Variant
Dim varTimeDate As Variant
varTimeDate = DMax("TimeStampField", "TableName", "EmpID=" & _
Me.EmpID.Value & " And TimeStampField<#" & _
Format(Me.TimeStampField.Value, "m\/d\/yyyy h\:nn\:ss") & "#")
If IsNull(varTimeDate) = False Then
datTimeDate = CDate(varTimeDate)
varValue = DLookup("ClockIn", "TableName", "TimeStampField=#" & _
Format(datTimeDate, "m\/d\/yyyy h\:nn\:ss") & "# And EmpID=" & _
Me.EmpID.Value
If IsNull(varValue) = False Then
blnValue = CBool(varValue)
If blnValue = Me.OptionGroupName.Value Then
Select Case Me.OptionGroupName.Value
Case -1 ' clock in entry
strMsg = "Your last entry was a ""clock in"" entry!"
Case 0 ' clock out entry
strMsg = "Your last entry was a ""clock out"" entry!"
End Select
strMsg = strMsg & " Change the entry that you're making."
Msgbox strMsg, vbExclamation + vbOK, "Change The Entry!"
Cancel = True
End If
End If
End If
--
Ken Snell
<MS ACCESS MVP>
Chou said:
Thanks for the reply Ken,
The toggle buttons are link to the table. After an employee enters their
Id
number, they either click on clock in or clock out. Each button is linked
to
their respective flieds and they are not in an option group.
Which way would be easier, having the 2 flieds or just one like you said.
How do I put them into an option group.
Thanks.
:
When you indicated that you're using toggle buttons for the "clock in"
and
"clock out" status, I assumed that you would have a single field in the
table for recording that status. You indicate that you have two separate
fields to record what essentially are mutually exclusive values, meaning
that both the Clock-in and Clock-out fields cannot have the same value
for a
single record. Normally, this would be designed as a single field with
the
Yes/No (boolean) data type.
Do you have each toggle button bound to one of the fields in the table?
If
you were to use one field, then you would be able to use an option group,
with the two toggle buttons as "members" of that option group -- and the
option group would be bound to the single field, and one toggle button
would
be given the data value of 0 and the other button would be given the data
value of -1 (0 means No and -1 means Yes in ACCESS table for the Yes/No
fields).
So, before I suggest any code to do the validation that you seek, please
provide more details about the form's setup and how you're using the
toggle
buttons (with or without an option group, to which field is each bound,
etc.).
--
Ken Snell
<MS ACCESS MVP>
The table has EmpID, Clock-in, Clock-out, timestamp. The Clock-in and
Clock-out are yes/no data type.
:
What are the fields in the table, especially the one where you record
"clock
in" or "clock out" status? What you seek to do can be done, but we
need a
bit more information.
--
Ken Snell
<MS ACCESS MVP>
I am new to access so any help would be greatly appreciated.
I created a database that keeps track of employeeID, date, time, and
if
they
clock-in or clock-out. I create a form that allows the employee to
enter
their Id and click on a clock-in or clock-out button. The Clock-in
and
Clock-out buttons were check boxs but I changed them to toggle
buttons.
Can anyone help me create a macro, expression, or code that will NOT
alllow
an employee to clock in twice in a roll and clock-out twice in a
roll.
Any help will be greatly appreciated.