Runtime Error 2448 - Can't assign a value to this object

C

charles.kendricks

I am trying to develop a timesheet which employee would use to sign in
and out for work and for lunch. I want the date field to always
display today's date (short date format). I inserted the following
code if the forms open event:

Private Sub Form_Open(Cancel As Integer)
'Check that the form is opened for today's date
If (Me.Date.Value <> Format(Now(), "Short Date")) Then
'If the date field does not hold todays date then reset all
controls
Me.Date.Value = Format(Now(), "Short Date")

The date control on the form is bound to a field in the table used to
store employees times, and the default value for the control is Now()
(short date format), although removing the default value didn't seem
to make any difference.
 
R

Rick Brandt

I am trying to develop a timesheet which employee would use to sign in
and out for work and for lunch. I want the date field to always
display today's date (short date format). I inserted the following
code if the forms open event:

Private Sub Form_Open(Cancel As Integer)
'Check that the form is opened for today's date
If (Me.Date.Value <> Format(Now(), "Short Date")) Then
'If the date field does not hold todays date then reset all
controls
Me.Date.Value = Format(Now(), "Short Date")

The date control on the form is bound to a field in the table used to
store employees times, and the default value for the control is Now()
(short date format), although removing the default value didn't seem
to make any difference.

A LOT of this is confusing. You say this is a bound form. Are you therefore
opening it in DataEntry mode (showing only a blank new record)? If you are then
all you should need to do is make Now() the default value and there should be no
need to Test the value of any field.

If you are NOT opening in DataEntry mode then you will be changing an existing
record rather than creating a new one and that almost has to be incorrect, no?

Also Date is a reserved word in Access (there is a function named "Date") so
there is a good chance that Me.Date.Value will be interpreted incorrectly unless
you put square brackets around it like Me.[Date].Value. Better yet would be to
change the name to RecordDate or similar.
 
C

charles.kendricks

Rick,

Thanks, you've given me plenty of food for thought (I guess you can
tell that I very new to VBA). What I want is for the employee to be
able to update the same record (signing in and out), several times
during the same day. So I guess I'd have to have the employees open
up a new form in DataEntry Mode every work day, and then open that
same record for updates during the day, right? Would I then open the
form in Edit Mode for updates (adding times in and out) during the
day?



I am trying to develop a timesheet which employee would use to sign in
and out for work and for lunch. I want the date field to always
display today's date (short date format). I inserted the following
code if the forms open event:
Private Sub Form_Open(Cancel As Integer)
'Check that the form is opened for today's date
If (Me.Date.Value <> Format(Now(), "Short Date")) Then
'If the date field does not hold todays date then reset all
controls
Me.Date.Value = Format(Now(), "Short Date")
The date control on the form is bound to a field in the table used to
store employees times, and the default value for the control is Now()
(short date format), although removing the default value didn't seem
to make any difference.A LOT of this is confusing. You say this is a bound form. Are you therefore
opening it in DataEntry mode (showing only a blank new record)? If you are then
all you should need to do is make Now() the default value and there should be no
need to Test the value of any field.

If you are NOT opening in DataEntry mode then you will be changing an existing
record rather than creating a new one and that almost has to be incorrect, no?

Also Date is a reserved word in Access (there is a function named "Date") so
there is a good chance that Me.Date.Value will be interpreted incorrectly unless
you put square brackets around it like Me.[Date].Value. Better yet would be to
change the name to RecordDate or similar.
 
R

Rick Brandt

Rick,

Thanks, you've given me plenty of food for thought (I guess you can
tell that I very new to VBA). What I want is for the employee to be
able to update the same record (signing in and out), several times
during the same day. So I guess I'd have to have the employees open
up a new form in DataEntry Mode every work day, and then open that
same record for updates during the day, right? Would I then open the
form in Edit Mode for updates (adding times in and out) during the
day?

One filter should handle this. If you open the form with a filter that
specifies "show me the record for this employee for today's date", that will
work in both cases. When there is no such record (first thing in the morning)
then the form will open to a new record. When a record does exist (later in the
day) the form will open to that record.

I still would see no reason to "clear the form" or set any values other than
relying on default values though.
 
R

Rick Brandt

Can you please provide an example of how to accomplish this?

Here is an Example that assumes there is a numeric field EmployeeID and a date
field RecordDate that stores only the date (time = midnight). It also assumes
you would be starting on a form where the desired employee would be chosen from
a ComboBox.

DoCmd.OpenForm "FormName",,,"EmployeeID = " & Me.ComboBoxName & " AND RecordDate
= Date()"
 
C

charles.kendricks

That works perfectly...Thanks a million


field RecordDate that stores only the date (time = midnight). It also assumes
you would be starting on a form where the desired employee would be chosen from
a ComboBox.

DoCmd.OpenForm "FormName",,,"EmployeeID = " & Me.ComboBoxName & " AND RecordDate
= Date()"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top