Date format issue in SSRS - vb.net

I have an issue with date format in my SSRS. I am saving date from DateTimePicker to database. From there I am taking display in my datagridview using following
dgv.items(0,2).value=Format(Cdate(dsSaver.tblInv.rows(0).items(0)),"dd-MMM-yyyy")
This displays it correctly (04-Nov-2011) but when I take date from the same database to my SSRS using
="Dated: " &Format(cdate(Fields!InvDate.Value),"dd-MMM-yyyy")
It displays it like 11-Apr-2011.
I have tested all winforms fare displaying it right but all SSRS are displaying it wrong.
Please advise.

A couple of things are going on here. The date is being saved appropriately but is being displayed incorrectly due to your formatting options. This line is quite problematic:
="Dated: " & Format(cdate(Fields!InvDate.Value), "dd-MMM-yyyy")
CDate takes a value, generally a string, and converts it to a date, which you are then taking and formatting back into a string. Now, by default reports are set to have their Language property set to English (United States) so the CDate function is taking the string representation of the date 04-Nov-2011 to be 04/11/2011 which it is then converting, using the US format of MM-dd-yyyy (not the Pakistani one) into being the date 11-Apr-2011 because it thinks the month comes first.
So, you should change your Language setting of your report to =User!Language so that it supports whatever the user's language is and will format things appropriately. This may be enough to make your expression work.
Regardless, if Fields!InvDate.Value is being supplied as a date field (as it should be) there is no need for the CDate function and this should work:
="Dated: " & Format(Fields!InvDate.Value, "dd-MMM-yyyy")
There is also the FormatDateTime function but unfortunately it doesn't support the format you want to use.

Have you looked at the RDLC options for Formatting a Report: Format the Date?

Related

Trim function causes date format change

Firstly, important to note that I'm in the UK so standard date format is dd/mm/yyyy
In a A1, I have a date: 02/05/2017 (dd/mm/yyyy)
I can confirm this in the immediate window:
?CLng(Range("A1").Value)
42857
Now, if I do the following:
Range("A1").Value = Range("A1").Value
you can probably guess, nothing happens - the date is still 02/05/2017 and the numeric value is still 42857
But if I use trim with it:
Range("A1").Value = Trim(Range("A1").Value)
The date is changed to 05/02/2017. This isn't just formatting - the numeric value has also changed to 42771.
What is it about the Trim() method that causes the date to be read in US format and then converted back to UK format with a new date value? Is this a bug?
From the discussion in comments:
The default or "token" format in VBA (not Excel itself, as Macro Man rightly pointed out) is US English - regardless of regional settings or cell formatting.
When you do VBA text functions on a date, the output of those functions are in text format. So the result of Trim(Range("A1").Value) is a string. This string happens to resemble a proper US date, so when you insert it into a cell, Excel recognizes it as a US date.
So two implicit conversions happen. The first happens when you read the cell contents and pass it to trim(): date->text conversion; the second happens when you write it back to an Excel cell: text->date conversion. The second conversion has no information about the format, so it assumes US English.
(You should be able to achieve the same result with any text function, not just trim().)
I found out that if you add "'" before the date, the date is not altered.
Range("A1").Value = "'"&Trim(Range("A1").Value)

VBA equivalent to VBScript's 'SetLocale' function?

To summarize my problem, I'm currently having the exact same issue as the person from this question here. I'm trying to parse a US date into an Excel sheet that has German as default and gives me a type mismatch on most of the dates because of it.
SetLocale sounded like the perfect solution to my issue, but after a minute of further research I discovered that GetLocale and SetLocale are apparently not supported in VBA.
It sort of worked when I assigned the parsed date to a String variable (I end up with a column using either MM/DD/YYYY or DD/MMM/YYYY format depending on whether or not I had a type mismatch, as long as I use On Error Resume), but I need them in a MM/DD/YYYY date type/format in order to compare all the parsed dates to a specific date in another cell (attempting to determine if the site has had any updates since the date entered in the specific cell).
I've also tried doing TimeStamp = Format(TimeStamp, "MM/DD/YYYY") (TimeStamp being a variable containing the parsed date), but it doesn't seem to be working- most likely due to the type mismatch error.
If anyone knows a VBA equivalent of the SetLocale function used in the linked question for me to try out, I would greatly appreciate it. If there isn't any, I'll be happy to amend my question and add my current code here to try and hammer out a solution together.
Thank you for your time and your help.
If you know they are all in US format, you can use a function like this:
Function ConvertUSDate(sDate) As Date
ConvertUSDate = Evaluate("DATEVALUE(""" & sDate & """)")
End Function

How to Format VBA TextBox To Long Date

I have a textbox for date display which shows the date in this format: 17/04/2012 but I would like to format it to shows up like 'April 17, 2012'. I tried this code
UserForm1.txtDate = Format(Long Date)
which I am getting syntax error from compiler.Can you please let me know how I can do it?
Thanks
there have been couple of posts on this. Culprit seems to be your system date format. Use of short, medium, long formats will change the result when the system settings change.
In US systems mainly it's done as, e.g. Format$(yourdate, “dd/mm/yyyy”)
Where as European systems, e.g. Format$(yourdate, “short date”)
If we look at explicitly defining your format, you are formulating your regional settings!
Good e.g. short date won’t always be mm/dd/yyyy but could be dd/mm/yyyy. That’s the key advantage over actually specifying the format. Simply you can change how you want your date to look like instead of depending on the system (with or without your knowledge).
When we first encountered this, we had some making the VBA apps and sending it over to another in some other place where they have own localized system date format. Your phone rings... ;)
Short/Long Date: Display a date according to your system’s long date format.
Medium Date: Display a date using the medium date format appropriate for the language version of the host application.
In your case you could use,
UserForm1.txtDate.Value = Format$(Date,"mmmm dd, yyyy")
Or to be super-safe,
Dim dtDate as Date
dtDate = DateSerial(Year(Date), Month(Date), Day(Date))
UserForm1.txtDate.Value = Format$(dtDate, "mmmm dd, yyyy")
Hope that helps.
References

Newbie - datepicker and to string VB

I know this is a really basic question but i'm just starting with VB Studio and this is straight out of the book.
MsgBox("Your birth date was " & DateTimePicker1.Text)
MsgBox("Day of the year: " & DateTimePicker1.Value.DayOfYear.ToString())
I expected the ToString to convert the integer value to a text value of the date but it still returns the nummerical value. If I watch it is actaully returns the numercial value as a string eg "20". I have tried to find some info online but i get the same peice of code back that i'm using.
Are there compatabitiy issues with date picker and Win7 (64bit) or am i missing some simple configuration.
I'd appreciate any support on this simple problem.
Thank you
Damian
The ToString function does not convert integers to literal strings. It is use to convert objects or fields to Strings which are suitable for display. Therefore it convert the integer 2 to the string "2".
Try using custom format to get your date in the proper format
Look at this link to get started

sql reporting services formats ( datetime, numbers

i put the format of a textbox using expression like Format(Fields!name.Value, "dd/MM/yyyy")
can i somehow set a global format for datetime and currency/floats without having the need to set it for each textbox in part
Perhaps.
You can specify a report language/culture to use which affects all controls.
However, I don't think you can specify a certain format "dd/MM/yyyy" which is not linked to language.
i didn't found how to do that but i do use know a macro recorder, so i created some macros for formatting stuff in my report like for datetime and cash, so i format a cell with a hotkey now, that's my workaround
well you could also format it in DB and send it as string, but it's kind off the same