How do I make a checkbox write todays date into a table field?

B

Bob Quintal

I am trying to get a Checkbox on a form to write todays date
into a field on a table when checked.
It would also be nice if when the checkbox is unchecked the
date is removed

Thanks
if the form is bound to the table, put the following code in the
checkbox's afterUpdate event

if me.chkDate.value then
me.[field's name] = date ' or now() for date/time
else
me.[field's name] = null
end if

change chkDate to the name of your textbox and [field's name] to
the field's name.
 
G

Guest

I am trying to get a Checkbox on a form to write todays date into a field on
a table when checked.
It would also be nice if when the checkbox is unchecked the date is removed

Thanks
 
G

Guest

Use the check box's After Update event.
Bind a control on the form to the date field you want to update. It can be
invisible if you don't want the user to see it or Locked if you want it
visible but not updatable:

Private Sub chkAddDate_AfterUPdate()

If Me.chkAddDate Then
Me.txtSomeDate = Date()
Else
Me.txtSomeDate = Null
End If

End Sub
 
G

Guest

The Value property is not necessary. It is the default. Also, indenting
your code makes it much easier to read.

if me.chkDate then
me.[field's name] = date ' or now() for date/time
else
me.[field's name] = null
end if

--
Dave Hargis, Microsoft Access MVP


Bob Quintal said:
I am trying to get a Checkbox on a form to write todays date
into a field on a table when checked.
It would also be nice if when the checkbox is unchecked the
date is removed

Thanks
if the form is bound to the table, put the following code in the
checkbox's afterUpdate event

if me.chkDate.value then
me.[field's name] = date ' or now() for date/time
else
me.[field's name] = null
end if

change chkDate to the name of your textbox and [field's name] to
the field's name.
 
B

Bob Quintal

The Value property is not necessary. It is the default.
Also, indenting your code makes it much easier to read.

..value is the default property, and isn't strictly necessary.
There have been reports of a bug in some versions of access
where a database will not close because of a bug in the way MS-
Access parses me.chkBox and creates a custom property.

As to indentation, me bad. I always indent code in the VB
editor. Sometimes I forget in this newsreader.
if me.chkDate then
me.[field's name] = date ' or now() for date/time
else
me.[field's name] = null
end if
 
G

Guest

Interesting. I have not experienced nor heard of this bug.
--
Dave Hargis, Microsoft Access MVP


Bob Quintal said:
The Value property is not necessary. It is the default.
Also, indenting your code makes it much easier to read.

..value is the default property, and isn't strictly necessary.
There have been reports of a bug in some versions of access
where a database will not close because of a bug in the way MS-
Access parses me.chkBox and creates a custom property.

As to indentation, me bad. I always indent code in the VB
editor. Sometimes I forget in this newsreader.
if me.chkDate then
me.[field's name] = date ' or now() for date/time
else
me.[field's name] = null
end if
 
B

Bob Quintal

Interesting. I have not experienced nor heard of this bug.

Apparently, and especially in a subform doing If me.boolean then as
opposed to me.boolean.value can cause the database to not exit
properly, apparently because Access misinterprets the first as a
declaration of an object, instead of a default property.

I heard of this via comp.databases.ms-access last year, but cannot
find it because the search at Google Groups is all messed up.
Perhaps Allen Browne or some other regulars there can elucidate.
 
G

Guest

If it was using the actual text boolean, then I can understand it. Any time
you use a reserved word as a name you are asking for troulbe.

I ran accross one recently where there was a table field named Date. I
asked to be able to change the name, but was refused. I had to use the field
in VBA to do a comparison. When I did, I got really strange results, even
with the field name in Brackets. I resolved it by qualifying the Date
function:

If rst![Date] <> VBA.Date Then

To bad the Access compiler (sic) allows reserved words as names.
To bad there are lazy programmers.
 
B

Bob Quintal

If it was using the actual text boolean, then I can understand
it. Any time you use a reserved word as a name you are asking
for troulbe.
No it was any field that could be use in an expression without
an operator.

If me.complete then
If me.superceded then
where me.obsolete;

that sort of usage.
I ran accross one recently where there was a table field named
Date. I asked to be able to change the name, but was refused.
I had to use the field in VBA to do a comparison. When I
did, I got really strange results, even with the field name in
Brackets. I resolved it by qualifying the Date function:

If rst![Date] <> VBA.Date Then

To bad the Access compiler (sic) allows reserved words as
names. To bad there are lazy programmers.
 

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