473,386 Members | 1,738 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Formatting number field

266 256MB
hi, i was wondering if its possible to format a number field i have populated mmddyyyy. is it possible to turn that into a date field with the seperator so it looks like this mm/dd/yyyy? thanks for your help
Feb 22 '11 #1
4 2335
beacon
579 512MB
Hi Didacticone,

Are you working solely with tables/queries or are you trying to accomplish this on the form? Basically, are you trying to store this number/date anywhere or just convert what the user sees?

If you've already established the field as a number field on the table, then you won't be able to turn the existing field into a date field, if by "turn" you mean "convert".

What you can do is create a new field in your table with a date/time data type then use a query to update the value using an expression and move it into the new date/time field.

Let's pretend this is your table structure:
Expand|Select|Wrap|Line Numbers
  1. Table Name: tblDates
  2. Field: ID (Autonumber)
  3. Field: NumericDate (Number)
  4.  

First, you would go into Design View for tblDates and add a new field, called ConvertedDate for the sake of explanation, and give it a Date/Time data type. Close the table and save it.

Next, you would create a new query. In Design View, click on the Query menu (I'm using 2003, so forgive me if you're using a later version and the menu isn't right) and select Update Query. Add the ID, NumericDate, and the newly created ConvertedDate fields to the query.

In the Update To section in the query grid, enter the following expression:
Expand|Select|Wrap|Line Numbers
  1. CDate(IIf(Len([NumericDate])=7,"0" & Left([NumericDate],1) & "/" & Mid([NumericDate],2,2) & "/" & Right([NumericDate],4),Left([NumericDate],2) & "/" & Mid([NumericDate],3,2) & "/" & Right([NumericDate],4)))
  2.  

The expression assumes that you've entered an 8 digit number for the date, but Access has trimmed the leading zero for months 1-9. Then, the expression adds a "/" after the month and day. Finally, since the expression is created as a string, the CDate function is encompasses the expression to convert it back to a date.

The last step would be to go to the Query menu and click Run. You should get a message telling you how many rows are updated. After clicking Ok, you can double-check the table to make sure that the rows were updated properly.

I should also mention that the expression doesn't handle null values, so you'll either want to add that to the expression or ensure that you have no null values in the table before you start.

If you are trying to accomplish this on a form, just add the expression I provided above to an unbound textbox and change the field in the brackets to match the name of the date field on the form.

Hope this helps and good luck,
beacon
Feb 23 '11 #2
didacticone
266 256MB
i have done what you said, but when i paste the code in the update to section of the query grid it immediately give me a popup saying:

"there was an error compiling this function. the visual basic module contains a syntax error. check the code and then recompile it."

any ideas? thanks for your help.
Feb 23 '11 #3
beacon
579 512MB
Did you change [NumericDate] to the name of the literal field in your table? When you copied and pasted the code into the Update To section, did you check out the code to make sure you didn't copy an extra character or something?

I did a search online for the error message and it looks like it's possible that your database could be corrupt. You may want to compact and repair to see if that resolves the issue. Before you compact and repair, you may want to try this out on a brand new test database. If the error message doesn't appear in the new version, then that would probably support the possibility that your database is corrupt.
Feb 23 '11 #4
didacticone
266 256MB
you are correct, it seems as though the database was corrupt, upon trying it on a new database it worked... thanks a lot for your help!
Feb 23 '11 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

16
by: Douglas | last post by:
Gday, How would I format a number so that: TheValue = 32500 Displays in the TextBox as: $32,500.00
2
by: Mike | last post by:
How do I select a number field from oracle 9i? The field is a number(integer). I have this line in my datareader: double dbid; dbid = dr.GetDecimal(0); I have tried getstring, convert and...
2
by: Irwinsp | last post by:
Hi All, I have a form with an auto number field displayed. The field looks great except when the user is entering a new record. The field then has the text "auto number" in it. Is there a...
4
by: James | last post by:
Hello there, Does anyone know how to create a sequential record number field in a query?? Thanks, James
8
by: shumaker | last post by:
I see other posts where some say fields that will hold a number with leading zeros should be stored as text instead of numbers. This is very inefficient though, as a string of digit characters...
0
by: icezcube | last post by:
I am not a programmer and a newbie with MS Access. I hope you guys can help me with my problem. I'm creating an IT hardware inventory database for my company. We have a few branches and each...
1
by: dympna | last post by:
HI I am trying to create a number field that will create a unique incremented number each time a record is added to the database via a form. but I want that number to be in sequence and start from...
2
by: Apple001 | last post by:
Hi all! I am having trouble with number field in the table. When I enter 632674648064, it shows 6.32675E+11. The field type has to be number and I want the number to appear as 632674648064 as...
4
by: gitimaya | last post by:
hi can someone help me. I have two talbed. In one table (A)a field is having numbers like 111,222,333(field name (data"). In another table I have a field which has abc111222333xxx(B)(field name...
12
by: Coni | last post by:
Hi All, I am working in Access to print Invoices. On each invoice there is a line number column which is a field to number each line for a product purchase. I would like to know if there is a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.