Format Field while retaining Datatype (1 Viewer)

Spackle

Registered User.
Local time
Tomorrow, 00:59
Joined
Jul 12, 2018
Messages
12
I've been beating my head against this one for a while now, and I'm sure I'm not the first person on the planet with this issue.

I'm using a query to create a table. I then perform some VBA manipulation before outputting the modified table to an excel spreadsheet. The issue I have is that one field needs to be a date datatype so I can compare to other dates, but the spreadsheet needs this field in the format dd.mm.yyyy as it is then input into another program.

If I use 'format' in the query, the field gets changed into a text field, so my comparisons don't work, or I end up with the output dates in the wrong format.

Can I somehow do this all within the query, or can I change either the formatting or the datatype of the field after the fact, just before I output to excel?
 

Gasman

Enthusiastic Amateur
Local time
Today, 15:59
Joined
Sep 21, 2011
Messages
14,218
I would have thought you just export as date. Excel holds it as date. How it is formatted is up to the user, it just has to be a date.?
 

Ranman256

Well-known member
Local time
Today, 10:59
Joined
Apr 9, 2015
Messages
4,339
A query can only guess at the specific field type.
Instead of make table query, use a pre build table ,with correct field types,
Then empty table and append qry.
 

Spackle

Registered User.
Local time
Tomorrow, 00:59
Joined
Jul 12, 2018
Messages
12
So there's no way to do this dynamically? I can use cDate to set the datatype in the query. I can use format to get the numbers in the right order (using 'dd.mm.yyyy'), but I can't use them both at once?
 

isladogs

MVP / VIP
Local time
Today, 15:59
Joined
Jan 14, 2017
Messages
18,207
If you really need to do both, then use
Code:
CDate(Format(DateField, "dd.mm.yyyy"))

But if that's your default date format, that's how Access will show the dates without using date formatting
 

Spackle

Registered User.
Local time
Tomorrow, 00:59
Joined
Jul 12, 2018
Messages
12
If you really need to do both, then use
Code:
CDate(Format(DateField, "dd.mm.yyyy"))
But if that's your default date format, that's how Access will show the dates without using date formatting

Thanks Ridders. I tried that, but my date field comes out blank on the table.:banghead:

Code:
"SELECT CDate(Format([ActivityDate], ""dd.mm.yyyy"")) AS [DATE]" etc

I'm generating my query by creating it as a string, which is why I have double quotes, then using .CreateQueryDef to run it.

If you're suggesting that this should be possible in theory, is it that I've got the syntax wrong somewhere?
 

Mark_

Longboard on the internet
Local time
Today, 07:59
Joined
Sep 12, 2017
Messages
2,111
but the spreadsheet needs this field in the format dd.mm.yyyy

Is the spreadsheet doing something... odd... that it needs it in a specific format? Is the spreadsheet set up to treat it as a string for some ungodly reason?

Both access and excel store date/times the same way; it is a number. Neither stores "01.01.2018", both would store 43101. Unless there is something rather wonky going on you should be able to export without formatting.
 

Spackle

Registered User.
Local time
Tomorrow, 00:59
Joined
Jul 12, 2018
Messages
12
Oh, it's wonky alright. I need the spreadsheet to be in this very specific format because it is then uploaded to our SAP accounting system, and if everything isn't just so, SAP throws its toys out of the cot.

Currently the reason why I initially need the datatype as Date is because I've got a loop checking and modifying entries, so I've got several test recordsets in which I look for a date using .FindFirst, and then returns other data associated with that record. Given the amount of time it took me to get this check to work, I'm loathed to change it, but it might be easier to change both dates into strings at the outset and try that way.
 

Mark_

Longboard on the internet
Local time
Today, 07:59
Joined
Sep 12, 2017
Messages
2,111
Could you have the date in question in your query twice? Once as a string for export and again as an actual date to be used for your check?

As annoying as it sounds, you may need to have two queries, the first with your date as a date to be used, the second with every thing from the first formatted for export.
 

isladogs

MVP / VIP
Local time
Today, 15:59
Joined
Jan 14, 2017
Messages
18,207
OK - sorry
I've just checked and I can't return that peculiar format back to a date

For info, CDate(Format([ActivityDate], ""dd.mm.yyyy"")) is incorrect
Using double " quotes doesn't help at all

Format(DateField,"dd\.mm\.yyyy") returns a string - Access adds the \ automatically
CDate(Format(DateField,"dd\.mm\.yyyy")) returns #Error

Format(CLng(DateField),"dd\.mm\.yyyy") returns a string
CDate(Format(CLng(DateField),"dd\.mm\.yyyy")) returns #Error

BTW you shouldn't use Date as a field name as its a reserved word

You need to find another way such as that suggested by Mark
 
Last edited:

Users who are viewing this thread

Top Bottom