Solved

access form date format error

Posted on 2011-09-08
10
306 Views
Last Modified: 2012-05-12
I have a text box in a form.  On GotFocus this code is executed:
Private Sub txtThk_Res_Smpl_Lot_Measure_DateTime_In_GotFocus()
    If IsNull(txtThk_Res_Smpl_Lot_Measure_DateTime_In) Then
        Me.txtThk_Res_Smpl_Lot_Measure_DateTime_In = Format(Now(),"mm/dd/yy"", ""hh:nn ampm")    
    End If
End Sub
but get the error "The value you entered isn't valid for this field".
Runtime error -2147352567 (800200009)

The field type is Date/Time.

Thanks,
Brooks
0
Comment
Question by:gbnorton
  • 3
  • 3
  • 3
  • +1
10 Comments
 
LVL 75
ID: 36506155
Try this:

Me.txtThk_Res_Smpl_Lot_Measure_DateTime_In = Format(Now(),"mm/dd/yy hh:nn ampm")    
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 36507417
<No points wanted>

Or:
"mm/dd/yy hh:nn AM/PM"
0
 
LVL 49

Accepted Solution

by:
Gustav Brock earned 500 total points
ID: 36509508
Format returns a string to your date field. Thus:

Private Sub txtThk_Res_Smpl_Lot_Measure_DateTime_In_GotFocus()
    If IsNull(Me!txtThk_Res_Smpl_Lot_Measure_DateTime_In) Then
        Me!txtThk_Res_Smpl_Lot_Measure_DateTime_In = Now
    End If
End Sub

Apply the format you wish to the Format property of Me!txtThk_Res_Smpl_Lot_Measure_DateTime_In

/gustav
0
 

Author Comment

by:gbnorton
ID: 36510438
cactus data,
Can you please explain the difference between using "Me." and "Me!" ?


DatabaseMX, boag2000,
The error went away after using your code. Still did not get the results i wanted. I would like the date to appear 9/8/11 7:20 AM.  I am getting the full date/time 9/8/2011 7:20:12 AM
Thanks,
Brooks
0
 
LVL 49

Expert Comment

by:Gustav Brock
ID: 36510621
> Can you please explain the difference between using "Me." and "Me!" ?

There may not be one in real life, and this is a lengthy discussion, but . is for properties while ! is specifically for fields.

> DatabaseMX, boag2000,
> The error went away after using your code.

But that is not how to do it. You first have Now which is of data type DateTime, then convert it to a string expression for a date, then Access will have to cast it back to data type DateTime before assigning it as Value for the field.

> I would like the date to appear 9/8/11 7:20 AM.  

Then, as I wrote, apply the format to the Format property of the textbox:

m/d/yy h:nn AM/PM

Note please the it should be AM/PM not ampm.

/gustav
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 36510749
Then try something like this:
 "m/d/yy h:nn AM/PM"

You can see the help files on "date format" for more info on date formats than you could ever dream of.

...But remember here the issue that surrounding the year 2000...
If you do this:  2/15/27
...is this 1927 or 2027
What about this: 2/15/20?
What is the cut off date for two digit years in your environments?
In MS it is 1929-1930, in some other applications it is 1950.

So why not move up to the more modern 4 digit year and avoid running the chance of an embarrassing miscalculation, just because you used an outdated date format.

In other words, the two digit year syntax is being phased out in most programs in favor of the less ambiguous 4 digit year.

Up to you as the developer...



On Me. vs Me!...
There are many technical explanations for this.
But the one I like is from GRayL
".", Microsoft made it
"!", You made it.

Most developers will use "." because it will autolist the members for you.


;-)

Jeff
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 36510809
as a final, unrelated note...

You might want to reconsider your naming convention:
    txtThk_Res_Smpl_Lot_Measure_DateTime_In

...in addition to having to worry about spelling, Long names may cause issue because in certain situations there is a limit on how long an expression can be...

0
 

Author Closing Comment

by:gbnorton
ID: 36510978
Thanks for your help!
0
 
LVL 49

Expert Comment

by:Gustav Brock
ID: 36511094
You are welcome!

/gustav
0
 

Author Comment

by:gbnorton
ID: 36511134
Jeff,
I agree on about the 4 digit year, but... in this particular case we have a very busy form that is causing us to compact in every way we can...
Thanks for your suggestions.
Brooks
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

In the previous article, Using a Critera Form to Filter Records (http://www.experts-exchange.com/A_6069.html), the form was basically a data container storing user input, which queries and other database objects could read. The form had to remain op…
In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now