Returning today's date 
Author Message
 Returning today's date

This is a pretty basic issue but I cannot get it to work properly.

I want to set today's date as the default value for a text box (txtDate) on
a form.  I declared the variable 'myDate' at the module level so that it
would be accessible from other procedures while the form is open.  When the
form loads, the following code executes:

txtDate = Now()
myDate = txtDate

When a new record is added, I set the value of 'txtDate' to 'myDate' so the
user doesn't have to bother with entering the date each time.

This arrangement works fine with one exception:  the value assigned by the
Now()  function is the current date AND time and I want only the date.

I tried experiementing with the Date function but get a Run-Time error of
94: 'Invalid use of Null.'  I also played with the DatePart function with no
luck.

How can I extract just the date portion from the Now() function?

Thanks,
Dave



Thu, 14 Jun 2001 03:00:00 GMT  
 Returning today's date
You should be able to set txtDate=Date().  This is the date only function.

This code can be replaced by setting the default value of txtDate to =Date()
in the control's property.



Thu, 14 Jun 2001 03:00:00 GMT  
 Returning today's date
As Holly pointed out, you should be able to use =Date() to get "just
the date". But be aware that this really is giving you a time of
midnight (12:00 AM). The time is always there as the decimal portion
of the double number that is stored as the date: .0=midnight, .5=noon,
.75=6:00PM, etc.  

 -- Andy

Quote:

>This is a pretty basic issue but I cannot get it to work properly.

>I want to set today's date as the default value for a text box (txtDate) on
>a form.  I declared the variable 'myDate' at the module level so that it
>would be accessible from other procedures while the form is open.  When the
>form loads, the following code executes:

>txtDate = Now()
>myDate = txtDate

>When a new record is added, I set the value of 'txtDate' to 'myDate' so the
>user doesn't have to bother with entering the date each time.

>This arrangement works fine with one exception:  the value assigned by the
>Now()  function is the current date AND time and I want only the date.

>I tried experiementing with the Date function but get a Run-Time error of
>94: 'Invalid use of Null.'  I also played with the DatePart function with no
>luck.

>How can I extract just the date portion from the Now() function?

>Thanks,
>Dave



Thu, 14 Jun 2001 03:00:00 GMT  
 Returning today's date
Thanks Andy and Holly.

I tried the txDate=Date again and it still returns the Run-Time error of
94: 'Invalid use of Null.'   So I started over with a fresh form and it
worked fine.

I went back to the original form and played with it until I discovered that
the problem arises when I bind a record source (table or query) to the form.
It does not seem to matter whether the txtDate *control*" is bound or not;
only when the *form* has a record source defined will the error occur.

I've distilled this problem down to its essence  by creating a form with
only a single textbox control and the following code:

Option Compare Database
Option Explicit
Dim x

Private Sub Form_Load()
    txtDate = Date
    x = txtDate
    MsgBox x  'test value of x
End Sub

This works works fine, right up until I bind a recordsource to the form.  As
I mentioned, I can have txtDate bound or unbound and the error persists.

Why would binding a recordsource to a form cause an 'invalid use of Null'
error with the Date function?  To make matters more confusing, if I
substitute the Now function for the Date function, it WILL work.

One final point of confusion, when I type the Date function in my form
module I include the parens (e.g., 'Date()'). However, the intellisense
feature automatically removes them from the Date function.  It does not
remove them from the Now function.

Sorry to sound like such a novice.  But I have checked the online help,
Getz's A97 HB, and Balter's MA95 and I cannot find anything on point.
Usually when the problem is this frustrating, it's something really stupid.
If anyone could enlighten me I would be very grateful.

Thanks,
Dave



Thu, 14 Jun 2001 03:00:00 GMT  
 Returning today's date
Problem has been resolved.

The root of the problem, as Holly Irick called to my attention off-line, was
the fact that I had a 'date' field in my recordsource.

It seems so obvious once you know.

Thank you Holly and to all who responded.

Dave



Thu, 14 Jun 2001 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Selecting Today's Date With Sheridan Date Combo

2. Adding a variable number of weeks to today's date

3. Getting today's date.....

4. Accessing today's date in a filter

5. Inserting today's date

6. Today's Date

7. Viewing today's date

8. getting today's date

9. Automatically selecting *today's* date - MonthView control

10. Date time picker has today's value

11. How to subtract a value from today's date

12. Filter on Today's Date

 

 
Powered by phpBB® Forum Software