MSACCESS Form VBA Open Where Clause - US v AU Date Formatting - ms-access-2010

Morning all,
This should be a simple fix and I have searched around and found things that appear they should work, but they don't seem to though...
So - it's a simple "Open this form and show only records from This date" function and, you guessed it, good ol' US of A date formatting is the problem and no matter which way I try it nothing is solving the problem...
So - here's the form code...
DoCmd.OpenForm "frm_Prod_Runs_Edit_List", , , "# " & NEWDATE & " #"
to find NEWDATE...
I strip the date components from a couple of combo boxes (CBOYear & CBOMonth) and a field that represents the day of the month (data will be in the format "1st" or "2nd" etc...)
I concatenate the individual components...
NEWDATE = DateSerial(TempYear, TempMonth, TempDate)
A msgbox NEWDATE popup results in the correct data (ie 1/3/18)
But when I run the code it either selects everything or nothing... the only time the function works is when the day field is 13 or higher (ie 13/3/18) - thus it can determine the correct format to work with.
I'm sure the correct answer is already here somewhere - the correct question however may be something I have not thought to search for.
I have tried to use DATEVALUE() before and within the form open code. I have tried to hard code the US format to see if that works...
DoCmd.OpenForm "frm_Prod_Runs_Edit_List", , , "#3/1/18#"
No good... shows all records - same if I hard code for the AU format #1/3/18#
My PC is set up for AU format dd/mm/yy
So... Which function do I need to use to convert the AU Date string into US Date string prior to calling the Openform, so the correct records open? OR is there a better way to write the WHERE clause in the Docmd line to achieve this result? - Or do I need to reset all my date fields to US format and convert them back to AU on the forms only?
Thanks for the feedback...

In VBA and VB6, date-literals using the # syntax are always in M/dd/yyyy format, regardless of the user's date format settings. This is a legacy of VBA/VB6's development in the USA before localization was a concern (and the USA is the only country to use the illogical MM/dd/yyyy format).
Note it's M/dd/yyyy and not MM/dd/yyyy - so omit any leading zeroes.
This is documented here: https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/data-types/date-data-type
The same rule also applies to date-literals in Access SQL (not the same thing as VBA/VB6). Fortunately in SQL Server you always use the ISO 8601 yyyy-MM-dd format in quotes... shame about Access though.

You simply need a properly formatted string expression for your date value - and the correct syntax:
Dim NEWDATE As Date
Dim WhereCondition As String
NEWDATE = DateSerial(TempYear, TempMonth, TempDate)
WhereCondition = "[NameOfYourDateField] = #" & Format(NEWDATE, "yyyy\/mm\/dd") & "#"
DoCmd.OpenForm "frm_Prod_Runs_Edit_List", , , WhereCondition

Your first problem is that you're not comparing NewDate to something.
Your second problem is that you're using string concatenation to filter by a date, and that results in the wrong date.
You can view this answer for the different ways to use parameters in Access.
Your final code should probably look something like this:
NEWDATE = DateSerial(TempYear, TempMonth, TempDate)
DoCmd.SetParameter("NewDateParam", NEWDATE)
DoCmd.OpenForm "frm_Prod_Runs_Edit_List", , , "SomeDateField = NewDateParam"
Or, if there might be time stored with the date:
NEWDATE = DateSerial(TempYear, TempMonth, TempDate)
DoCmd.SetParameter("NewDateParam", NEWDATE)
DoCmd.OpenForm "frm_Prod_Runs_Edit_List", , , "SomeDateField - NewDateParam < 1"

Related

Tabulator Cell datetime object - Need to Format

I am attempting to reformat a datetime value in a cell that contains TZ UTC data. An example value is: 2019-12-09T14:50:47.000Z-0500
I need it to display as:
MM/DD/YYYY HH:mm:ssXM - ex: 12/09/2019 02:50:47PM
Local time, of course.
I have tried reading the moment.js doc without success. Here is a snippet I have attempted. The table shows up with "blank rows." If I remove the formatting, the data shows correctly but not with the date and time format I would like.
{title:"Last Submitted", field:"createdOn", sorter:"date", formatter:"datetime", formatterParams:{inputFormat:"YYYY-MM-DD hh:mm:ss", outputFormat:"MM/DD/YYYY", invalidPlaceholder:"(Invalid date)"}},
Any assistance would be greatly appreciated!
Ben
UPDATE BASED ON ANSWER 12/26/2019
Thank you again for responding. However, this is perhaps an issue for the author of Tabulator since I copied the inputFormat and outputFormat verbatim into the column definition of a Tabulator component and it displays blank rows. If I remove the column cell formatter (which is a wrapper around moment.js code), the list displays with the full timestamp (including UTC / zulu time).
ex:
2019-12-09T12:50:47.000Z-0500
Expected result (either 24h or 12h format doesn't matter at this point - and I did try to remove the "A" for the AM/PM indicator)
Unfortunately, I cannot upload the code for this project since it makes internal WS calls for JSON results (which is another issue - Remote Pagination does not appear to be working.)
Here is the source code for the column:
{title:"Last Submitted", field:"createdOn", sorter:"date", formatter:"datetime", formatterParams:{inputFormat:"YYYY-MM-DD[T]HH:mm:ss.SSS[Z]Z", outputFormat:"MM/DD/YYYY HH:mm:ssA",invalidPlaceholder:"(Invalid date)"}},
As stated above, if I add the formatter, blank table appear and nothing else. If I remove the formatter all data is displayed including the unformatted date (well it's formatted in a way in which I nor my users will want).
Any ideas would be greatly appreciated!
Image of Result with datetime formatter
With momentjs you can parse date if you know the format of an input string:
moment(inDate, inFormat);
For example:
moment('12-25-1995', 'MM-DD-YYYY');
In your case format of an input string is YYYY-MM-DD[T]HH:mm:ss.SSS[Z]Z - square brackets work as escape characters.
You can get formatted string from moment object with .format method:
moment().format(outFormat);
For example:
moment().format('MM-DD-YYYY');
In your case format of an output string is MM/DD/YYYY HH:mm:ssA - you can read more in docs
You can see how both parsing and formatting work together in the snippet below:
let inDate = '2019-12-09T14:50:47.000Z-0500',
inFormat = 'YYYY-MM-DD[T]HH:mm:ss.SSS[Z]Z',
outFormat = 'MM/DD/YYYY HH:mm:ssA',
outDate = moment(inDate, inFormat).format(outFormat);
console.log(`In Date: ${inDate}`);
console.log(`Out Date: ${outDate}`);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>

Date conversion handling YYYY-MM-DD HH:MM:SS.SSS

My source is a file and loading into SQL Server table. I'm working on a scenario where i have to convert a string '2019-04-02T21:24:00.065' to informatica datetime format.
I tried below expression but some times its failing due to we are not receiving milliseconds from our source file in few occasions.
IIF(NOT ISNULL(DATEFIELD),TO_DATE(SUBSTR (DATEFIELD, 0, 10) || ' ' || SUBSTR(DATEFIELD, 12, 12), 'YYYY-MM-DD HH24.MI.SS.US'),NULL)
I'm looking for a permanent fix to handle all types of datetime formats regardless of what we receive in the file.
Well... I'm sorry to say, but there is no magic component that will recognize all possible date and time formats (including e.g. verbal in swahili).
You will need to detect the format for yourself. You can use a DECODE function, like e.g.:
DECODE(True,
IS_DATE(your_input_port, 'DD/MM/YYYY'), TO_DATE(your_input_port, 'DD/MM/YYYY'),
...)
If you are completely sure that only seconds/milliseconds are the missing part, you can check for length, if less than 12, use RPAD to the second part of your SUBSTR with missing format, or you can use decode as suggested by #maciejg and write code for all possible date formats.
Thanks for your inputs guys. Since we are not sure which date format we are receiving , we decided to go with a simple fix for this. I have changed the target field to varchar and simply replacing the T with ' ' value since this a staging mapping.
Again thanks for your time and inputs.

Convert JavaScript DateTime to UTC with no milliseconds

Right now I'm trying to parse a date that's written in a human-readable format into a DateTime String that a SharePoint list will accept. In order to do this I've determined that I need a String in a format similar to ISO that looks like: 2007-08-20T00:00:00Z. It seems that SharePoint only accepts DateTimes that are in UTC with no milliseconds included (for whatever reason, SharePoint gives errors and won't accept the DateTime when you include the milliseconds), so I need to convert my local time into a UTC time before converting it to the ISO string.
Here's the process that the code below is using.
First I use DateJS to parse my human-date into a JavaScript Date.
(Works fine, but apparently DateJS has been abandoned, so maybe I
should change this to use MomentJS.)
Next I tried to create a new
moment in UTC. (This line is very, very wrong, and crashes my
program.)
Then I have SPServices convert it into an ISO. SPServices drops the milliseconds off the DateTime so that SharePoint will accept it. (Works
fine).
I'm sure there has to be a more elegant/working way to achieve this, instead of stitching together 3 different libraries. I'm just not sure what it is.
var jScriptStartDate = Date.parse("6/29/2014 8:30am"); //JS Date
var jScriptStartDateUTC = moment(jScriptStartDate).utc(); //local date to UTC.
var startDate = $().SPServices.SPConvertDateToISO({ //Sharepoint ISO 8601 format
dateToConvert: jScriptStartDateUTC,
dateOffset: "" //Sharepoint dates only accept UTC times, aka no dateOffset.
});
newItem.set_item('EventDate', startDate); //EventDate is internal for StartTime
You can just use moment.js, and this is all in the documentation.
moment('6/29/2014 8:30am','M/D/YYYY h:mma').toISOString()
This assumes all of the following:
The source value is in the user's time zone (that is - the time zone of the machine where the JavaScript code is running)
The input will always be in the specified format
It's also worth mentioning that if you put a space before the "am", that most modern browsers can do this natively without any library:
new Date('6/29/2014 8:30 am').toISOString()
If you take that approach, realize that the date parts are ordered according to the users locale, which might be m/d/y, or d/m/y or y/m/d.
Also, you said in the title "... with no milliseconds", but didn't elaborate on that in your question. I'm fairly certain you can pass the milliseconds without issue. There's no good reason to go out of your way to remove them. But if you must, then that would be like this with moment:
moment('6/29/2014 8:30am','M/D/YYYY h:mma').utc().format('YYYY-MM-DD[T]HH:mm:ss[Z]')
I ended up adjusting this code to use the updated DateJS (https://github.com/abritinthebay/datejs/) and a custom function I made called .toShortISOString().
The process is down to 3 lines:
var jScriptStartDate = Date.parse("6/28/2014 8:00am"); //convert to Javascript Date
var startDate = jScriptStartDate.toShortISOString(); //convert to Sharepoint ISO format (UTC with no milliseconds)
newItem.set_item('EventDate', startDate); //Strangely the internal name for Start Time is EventDate
.toShortISOString()'s main change is the removal of milliseconds, since SharePoint doesn't like milliseconds. It's code looks like this inside of the date.js file:
if ( !$P.toShortISOString ) {
$P.toShortISOString = function() {
return this.getUTCFullYear() +
"-" + p(this.getUTCMonth() + 1) +
"-" + p(this.getUTCDate()) +
"T" + p(this.getUTCHours()) +
":" + p(this.getUTCMinutes()) +
":" + p(this.getUTCSeconds()) +
//Remove the milliseconds.
//"." + String( (this.getUTCMilliseconds()/1000).toFixed(3)).slice(2, 5) +
"Z";
};
}
If you're trying to add this to date.js, make sure you have the latest version from the link above, then search the file for toISOString and place the code right below the handler for toISOString.

Classic ASP format date

I have a date being extracted from the database which is formatted as following
11/04/2013
Which is
mm/dd/yyyy
I want to convert it in classic asp to the following
04/11/2013
which is
dd/mm/yyyy
Could anyone help me with this?
I think this might a duplicate of an earlier question. I found a response here: ASP formatting date.
In the example, instead of Now() you would use a DateTime representation of your date. In order to get that from a string use CDate(string) (Click to see the documentation).
In the interests of providing a full example, to get your date you could do something like this:
Dim vbDate
vbDate = CDate("11/04/2013")
Response.Write(addLeadingZero(Day(vbDate)) & "/" & addLeadingZero(Month(vbDate)) & "/" & Year(vbDate))
function addLeadingZero(value)
addLeadingZero = value
if value < 10 then
addLeadingZero = "0" & value
end if
end function

Applescript: Convert date into something useful, then perform calculation

I'm trying to create an applescript script that will take a date in the form:
02/20/99
Then, subtract 5 days from the date, and return a date like this:
02/15/99
Here's the basic applescript I have, but, obviously, I'm missing the logic around converting the date into something that I can manipulate (and returning it to something readable by humans).
set itemPath to quoted form of POSIX path of theFile
set dateString to do shell script "awk 'NR == 10 {print $3}' " & itemPath & " -"
set myDueDate to dateString - (5 * days)
set theTask to "Pay AMEX bill"
tell application "OmniFocus"
tell front document
set theContext to first flattened context where its name = "OFFICE"
set theProject to first flattened context where its name = "Bookkeeping - PMS"
tell theProject to make new task with properties {name:theTask, context:theContext, due date:myDueDate}
end tell
end tell
Thanks in advance!
I think these lines should be sufficient for what you need:
set dateString to "02/20/99"
set thisDate to date dateString
set fiveDaysEarlier to thisDate - 5 * days
log date string of fiveDaysEarlier
log short date string of fiveDaysEarlier
This page has some more examples of the coercions supported when converting strings to objects of date class:
AppleScript Class Reference: date
Also, it probably goes without saying, but the string "02/20/99" is not Y2k compliant, so guesses will have to be made somewhere as to which century this date lies in.
If you get error "FileMaker Pro got an error: Object not found." number -1728, add my before date. e.g,
set thisDate to my date dateString.
When working inside a tell, I think it needs to access methods outside the tell's scope.

Resources