sql reporting services formats ( datetime, numbers - sql

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

Related

Can an unbound text box containing a string be set to equal a date(time) field programatically?

Alright. The goal is to programatically grab a string of numbers from an unbound control on a form. Why? I have a form that contains two controls for a start time and a finish time. The format for these controls are dd/mm/yyyy hh:dd. The end users are complaining that they hate having to take the time to enter the date and time in a single field, especially considering the old form (a horrible Excel spreadsheet where users could type whatever they felt like typing), allowed them to just type the 4-digit time. I am trying to replicate that experience, but as we know, date and time are wrapped into one field in Access.
The idea is to supply the date/time field with the values from the string using separate unbound control. For example, one control will be labelled 'Start Time' and will accept 4 numbers with an input mask of 99:99. Before the form is updated, I would like to pass the string from the control Unfortunately, I do not have the code, but I will attempt to build psuedo code here:
Class Level Module
Private Sub Form_BeforeUpdate(Cancel As Integer)
strStartTime as String
strFinishTime as String
strDate as String
rstDailyLog as recordset
Set rstDailylog = CurrentDb.OpenRecordset ("Daily Log" dbOpenDynaset)
'assign variables to unbound controls on form
strStartTime = Me![Start Time]
strFinishTime = Me![Finish Time]
'Here, I assume I begin parsing my string to the date/time field
'function to edit send the string to the actual date/time field
'Basically, copied from Microsoft Docs
Sub EditName(rstCovertString As Recordset, strStart As String, strFinish As String)
With rstConvertString
.Edit
![Start Time] = strStart
![Finish Time] = strFinish
.Update
.Bookmark = .LastModified
End With
What are the implications of doing so? For example, if the bound form displays a record with an unbound value, how will that record display the desired data?
As I was building this code, I just realized that the field that holds the date/time is a date/time data type?
Now I am really confused. If they are different data types, can I even send my start/finish variables to that control as a string?
It turns out that Access does a TRUCKLOAD of data type conversions for you.
When text box controls are bound to a underlying table, then that text box will return a datetime data type. Even if you stuff in a string - it gets converted to a datetime data type behind the scenes.
And same goes for text, or if the control is bound to a number. (again, data type forcing is actually occurring here. However, VBA is rather forgiving in this regards.
Thus, most of the time you don't notice this issue.
HOWEVER, WHEN the text box is NOT bound to a underlying table, then the data type of that text box is more loosely goosey.
So, set the format of the un-bound text box to date format. If you do this, then even assigning a string value to the text box will cause access to convert the string into a date time. In fact, your code should STILL take the data from that table, and if you are not directly assigining the date column to the text box?
Well, then format the string as USA format.
eg:
me.MyStartDate = format(dtValue, "MM/DD/YYYY")
or
me.MyStartDate = format(dtValue, "YYYY-MM-DD")
You will find that EVEN if your date format is different then above (say based on your computers regional settings), then Access will convert the above to an interal date format, and THEN display what your regional settings are REGARDLESS of the above.
So, your display might be:
DD/MM/YYYY (day first)
But, doing this:
me.MyStartDate = dtvalue
or
me.MyStartDate = format(dtValue,"YYYY-MM-DD")
will work.
So, do NOT convert the datetime value from the table into a string IF POSSIBLE to avoid doing so.
So, you simply assign that actual datetime value directly it to the text box. As long as Access sees and knows that text box has a date format, then it will assume and work with that text box as a date time data type.
The above ONLY works if you set the un-bound text box to have some kind of date formatting. Once you told access that the text box is a date type text box, then directly assign date values from a table (without conversion to a string) is your BEST solution.
As noted, for bound text boxes then the text box will take on the correct datatype - even if you don't format the text box.
And the same above trick also applies to number formats. And thus setting a number format for the text box will result in that text box NOT being a string/text type, but an actual number.
So, you can force/set un-bound text boxes to a given data type by use of the formatting option. Once you do this, then you should not need to format data from a table, but in fact just shove the actual date value into the text box. You can (and should) thus now be able to set any kind of date formatting you want for the text box - even setting that is the exact opposite of the actual date format your computer (and user) has chosen on a whim.
In other words, if you do this correct?
Then you don't care, or even have to know the date format settings on any given computer, and your code will always work.
Of all the suggestions here by me?
If possible, assign the actual date value to the text box, and do NOT format the string. This will allow you to set any kind of date/time format for the text box.
Once you defined/set the text box to be a date format, then it is a datetime thing and data type. As a result, you should not require any conversion from the table data to set/fill out the text box.
Edit
You have:
'assign variables to unbound controls on form
strStartTime = Me![Start Time]
strFinishTime = Me![Finish Time]
No! - declare the above two vars as date, not strings.
dim dtStartTime as date
dim dtFinishTime as date
now:
dtStartTime = Me![Start Time]
dtFinishTime = Me![Finish Time]
'Here, I assume I begin parsing my string to the date/time field
No, don't parse. You have the data as a internal datetime. How you display this data is up to you or the users regional settings. As a developer, it is a date type and a date "thing". Don't convert to string!!!
'function to edit send the string to the actual date/time field
'Basically, copied from Microsoft Docs
Sub EditName(rstCovertString As Dao.Recordset, dtStart As Date, _
dtFinish As date)
Access will often recognize a string with a date/time structure as a date/time value. Could use CDate() function to be sure or use # delimiters.
Do these tests in VBA editor immediate window.
?IsDate("1/1/2020 14:50")
?IsDate(CDate("1/1/2020 14:50"))
?IsDate(#1/1/2020 14:50#)
?IsDate("1/1/2020" & " " & "14:50")
All return True.
If you want to display the saved date/time then bind a textbox to the field. Lock the control if you don't want users to edit in it. Then use VBA to save any edits done in the unbound textboxes. I recommend AfterUpdate event for code to save. Use BeforeUpate event to validate user input.

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

Formatting date removes zeroes

I'm looking for a way to keep the zeroes in single digit months and days however the current way I'm doing it now isn't working. I"m looking for some insight as to what may have happened and if I"m not formatting correctly.
Currently I'm looking just to format the date into a specific format, here's my code:
dateFormat = Today.ToString("MM/dd/yyyy")
however the value of this isn't the 02/13/2013 value I was expecting it's #2/13/2013# removing all leading zeroes. What is going on here and why is it giving the # and removing all leading zeroes?
The relevant question is: Of what type is dateFormat? If it is a Date (or DateTime) then the formatted string will be converted back to a Date and the formatting will be lost. The formatting only works if you assign the result of the formatting to a String. The Date type does not store any formatting. Formatting in general only applies to strings.
Dim dateFormat As String
dateFormat = Today.ToString("MM/dd/yyyy")
Note: If you have Option Strict Off then VB automatically attempts to convert non-matching types. In your code the formatted string will automatically be converted to Date if dateFormat is of Date type. Therefore Option Strict Off is dangerous, since it hides potential programming errors. I strongly encourage you to use Option Strict On. You can do that either per source file or in the project properties under Compile > Option Strict.
When doing so, you will lose some automatic conversions and you will have to specify those conversions explicitly but you will increase the security of your code.
You can even set this option as default for new projects in menu Tools > Options, then navigate to Projects and Solutions > VB Defaults. (At least in VS 2008). I always have:
Option Explicit On
Option Strict On
Option Compare Binary
Option Infer On
Declare dateFormat as string :
Dim dateFormat as string = DateTime.Today.ToString("MM/dd/yyyy")
As for the "hashes". This answer explains it.
The DateTime itself doesn't really contain the hashes, and none of the
normal format strings will produce hashes either.
You can use the proper way of VB.NET date formatting
Dim DateFormat As DateTime = Now
Console.WriteLine(Format(DateFormat, "Short Date"))

Date format issue in SSRS

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?

Where is list of defined format styles for Excel using the OpenXML sdk

I'm using the OpenXML SDK 2.0, and I want to know where the list of defined format styles (and their values) is kept. Specifically, I want to know date/time formats. I want to open an xcel file, read a cell, and try to determine (based on format/style) if it is a date or date time value. Conversely, if I write data to a xcel file, and I want a specific cell to be of a 'Date', what styleindex value do i use?
This article
may be of help. It shows how to add style to a cell.
Unfortunately is not possible to reference just the cell format, but you need to reference a cell style item.
I just did a fast look and cannot find the table of standard styles (it's in the xlsx spec somewhere). However, be aware that some of the standard datetime formats are incorrect. Excel does not match what the spec says.