Date format in VBA

imfarhan

Board Regular
Joined
Jan 29, 2010
Messages
125
Office Version
  1. 2016
Platform
  1. Windows
Dim dtMdate As Date
dtMdate = Format(rs.Fields("Mdate"), "yyyymmdd")


Debug.print dtMdate



But when I print the above variable in my Intermeditate window I got the
values like
debug.Print dtmdate
00:00:00 instead of date value like dd/mm/yyyy

Am I using wrong way to use format mask?
Many thanks
Farhan
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Are you sure that there is a value in the record that you are on there? That looks like it is returning nothing so it gives you that answer.
 
Upvote 0
what data type is rs.Fields("Mdate") ?
a date ? a text ?

Code:
Dim dtMdate As Date

dtMdate = rs.Fields("Mdate")
 
Debug.print dtMdate
shouldn't this work ?
why do you need the format at all ?
 
Upvote 0
Farhan

The format function returns a string not a date.

Some times VBA will recognise the string as a date and convert it, some times it won't.

You could try using something like DateValue or DateSerial.

Or as James has suggested - don't do anything, which might work if the field is date or text (see above about converting strings to date).
 
Upvote 0
Farhan

The format function returns a string not a date.

Some times VBA will recognise the string as a date and convert it, some times it won't.
And in this case using yyyymmdd it does not recognize that as a date value. So, you are right about that.

If they want to use that format then they need to declare their variable as a string. If they want it as a date then they would have to use a valid date format. I didn't even think about that earlier. Good catch.
 
Upvote 0
Hi All,
Thanks for your swift response.

When I don't use the format its work fine but it bring the time values too which I don't want to display becuase further in my VBA code I want to use the DateDiff function which mentioned below
I want to calcuate the variable date value with current date only.

Set rs= CurrentDb.OpenRecordset("select * from maxdate)

Note the maxdate is the query not table
and orginal field is date/time type mdate is the

The MaxDate query is like:-
Select Max(CeStartDateTime) from <table>

Further I would like to use ...thats why I dont want time stamp
IF DateDiff("d",date,mDate) <2 Then
X
Else
Y
End IF

Many thanks
Farhan
 
Upvote 0
I think this might work to get rid of the time from the datetime and I think its also somewhat compatible across different systems
I didn't really look in detail at your last post.
Code:
SELECT 
    DATEADD( 'd', 
             DATEDIFF( 'd', 0, Mdate), 
             0 
           ) as my_date
FROM 
  example_table;
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,853
Members
452,361
Latest member
d3ad3y3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top