I am working on a code from a previous developer. This code has SystemTime set up.
Is there a way to get today date and minus 30 days in this format?
Code Below:
Public Function GetIsoTimestampTest() As String
Dim st As SYSTEMTIME
'Get the local date and time
GetSystemTime st
'Format the result
GetIsoTimestampTest = _
Format$(st.wYear, "0000") & "-" & _
Format$(st.wMonth, "00") & "-" & _
Format$(st.wDay, "00") & "T" & _
Format$(st.wHour, "00") & ":" & _
Format$(st.wMinute, "00") & ":" & _
Format$(st.wSecond, "00") & "Z"
End Function
Build a native date & time, add -30 days, format as a string:
utcInIsoFormat = Format$(DateAdd("d", -30, _
DateSerial(st.wYear, st.wMonth, st.wDay) _
+ TimeSerial(st.wHour, st.wMinute, st.wSecond)), "yyyy-mm-ddThh:nn:ssZ")
SYSTEMTIME appears to be a custom type defined elsewhere in your code. It's not a standard type available in Access VBA. So to use it effectively, you need to find the definition. Also GetSystemTime is also likely a custom function exclusive to your code. Here's a sample definition of a similar type, although it may not be exactly what's implemented in your system: http://custom-designed-databases.com/wordpress/2011/get-milliseconds-or-seconds-from-system-time-with-vba/
That said, System Time would refer to the Windows system time. You also have a native ability in VBA to get time using the Now() function. (https://msdn.microsoft.com/en-us/library/office/gg278671.aspx) This returns a variable with type Date, which is equivalent to a number where the integer represents days and the decimal represents time of day. An example to get 30 days prior to today would be:
Dim lastMonth as Date
Dim formattedDate as String
lastMonth = Now() - 30
formattedDate = Format(lastMonth, "yyyy-mm-ddThh:nn:ssZ")
DateSerial happily accepts a negative day count. Thus:
Public Function IsoDateMinus30() As Date
Dim st As SYSTEMTIME
Dim Result As Date
' Get the local date and time
GetSystemTime st
Result = DateSerial(st.wYear, st.wMonth, st.wDay - 30)
' To include time:
'
' Result = _
' DateSerial(st.wYear, st.wMonth, st.wDay - 30) + _
' TimeSerial(st.wHour, st.wMinute, st.wSecond)
IsoDateMinus30 = Result
End Function
Related
I have 3 record:
day_birth of type Number (1-31);
month_birth of type Short Text ("January");
year_birth of type Number.
How to get in calculated field, full birthday day name from this day?
I tried this one expression:
WeekdayName(Weekday(DateSerial(2003;1;1);2))
But it doesn't work. I get #Type! in my record field.
Well, you can use this:
? format(dateserial(2003,1,1),"ddd")
Wed
or even this:
? format(dateserial(2003,1,1),"dddd")
Wednesday
However, you suggest/state/hint/imply/note that your input month is a LONG TEXT month of January. (short would be Jan).
So, we need to take into account that fact.
(but, why oh why oh why oh why oh why was the DOB not just stored as a simple single datetime column? With such a column, then you are 100% free to break it apart into 3 columns for reporting etc., but always have one simple date column
However, you have what you have, probably not your fault.
So, then:
Dim strTestMonth As String
Dim intTestYear As Integer
Dim intTestDay As Integer
strTestMonth = "January"
intTestYear = 2003
intTestDay = 1
Dim strTestDate As String
Dim strDateFormat As String
strDateFormat = "mmmm/dd/yyyy"
strTestDate = strTestMonth & "/" & intTestDay & "/" & intTestYear
Dim dtDate As Date
dtDate = Format(strTestDate, strDateFormat)
' get day of week as number
Debug.Print Weekday(dtDate)
' get day of week as text
Debug.Print "Short day of week = " & Format(dtDate, "ddd")
Debug.Print "Long day of week " & Format(dtDate, "dddd")
Output:
4
Short day of week = Wed
Long day of week Wednesday
So, approach:
Convert the string into a internal date, and once done, then you are free to spit out the date in any format you want, including use of weekday function, or even format which can return a short day format (wed), or a longer date format (Wednesday).
It not clear if you need this "display" of the week day on the form, or in a report or whatever. So as always the VERY important issue becomes the when/where/how/what point in time you need this expression.
So, you could for example place this function in a standard code module: (not forms code module)
Public Function GetDayOfWeek(strMonth As String, _
intDay As Integer, _
intYear As Integer) As String
Dim strDateFormat As String
Dim dtDate As Date
strDateFormat = "mmmm/dd/yyyy"
dtDate = Format(strMonth & "/" & intDay & "/" & intYear, strDateFormat)
GetDayOfWeek = Format(dtDate, "dddd")
End Function
So, now when ever and "where" ever you need to display the weekday as "text", then you can do this for even a control on that form:
=(GetDayOfWeek([MonthField],[dayField], [YearField]))
So, place the above code in a standard code module (not forms code module), and then in code, or even as a expression on a control in a form, you can pass the 3 values, and it will return/display the day of week in long text format.
Use DateValue and a simple query:
Select
*,
DateValue(Str([year_birth]) & " " & [month_birth] & Str([day_birth])) As BirthDate,
WeekdayName(Weekday(DateValue(Str([year_birth]) & " " & [month_birth] & Str([day_birth])), 2), 2) As BirthWeekday
From
YourTable
I'm trying to get Access to create a certificate for me. I've got almost everything worked out except for the date. I want the date to display as text.
I'm able to accomplish this with a module in Excel, but when I input it into Access as a function, it gives me a #Name? error. Here's what I have in the function (again, this works in Excel):
Function DateToWords(ByVal xRgVal As Date) As String
Dim xYear As String
Dim Hundreds As String
Dim Decades As String
Dim xTensArr As Variant
Dim xOrdArr As Variant
Dim xCardArr As Variant
xOrdArr = Array("1st", "2nd", "3rd", "4th", "5th", "6th", _
"7th", "8th", "9th", "10th", "11th", "12th", _
"13th", "14th", "15th", "16th", "17th", "18th", _
"19th", "20th", "21st", "22nd", _
"23rd", "24th", "25th", "26th", _
"27th", "28th", "29th", "30th", "31st")
xCardArr = Array("", "one", "two", "three", "tour", _
"five", "six", "seven", "eight", "nine", _
"ten", "eleven", "twelve", "thirteen", _
"fourteen", "fifteen", "sixteen", _
"seventeen", "eighteen", "nineteen")
xTensArr = Array("twenty", "thirty", "forty", "fifty", _
"sixty", "seventy", "eighty", "ninety")
xYear = CStr(Year(xRgVal))
Decades = Mid$(xYear, 3)
If CInt(Decades) < 20 Then
Decades = xCardArr(CInt(Decades))
ElseIf CInt(Decades) Like "*0" Then
Decades = xTensArr(CInt(Left$(Decades, 1)) - 2)
Else
Decades = xTensArr(CInt(Left$(Decades, 1)) - 2) & "-" & _
xCardArr(CInt(Right$(Decades, 1)))
End If
Hundreds = Mid$(xYear, 2, 1)
If CInt(Hundreds) Then
Hundreds = xCardArr(CInt(Hundreds)) & " hundred "
Else
Hundreds = ""
End If
DateToWords = "This " & xOrdArr(Day(xRgVal) - 1) & " day of" & _
Format$(xRgVal, " mmmm, ") & _
xCardArr(CInt(Left$(xYear, 1))) & _
" thousand " & Hundreds & Decades
End Function
Code in Access that calls the function. This is in the text box on the report:
=DateToWords([Forms]![FrmMakeCert]![Date])
n Excel, this takes the date that I specify in a cell (e.g. 12/27/18) and turns it into:
"This 27th day of December, two-thousand eighteen"
That's what I'd like to accomplish in the Access report, pulling the date from a form.
Issue was caused by using the same name for both user-defined function and module namely DateToWords.
You should use different names.
Rename the module to, for instance, mdlDateToWords.
Steps to use.
First create a module in access and paste your codes.
Suppose in report you are displaying date to text in a textbox name txtDateText
In txtDateText data source enter following
=DateToWords([Forms]![Form1]![txtDate])
Remember: Form1 must be open and contain valid date otherwise it will show error. Form1 is form name, so it will be different in your case. Also text box name.
File download link
I got this JSON in VB6 (not .NET) inside a textbox:
[{"id":123,"key":"h73df", "birth_date":"20180101"}]
It returns no error when posting to an url..but the date is not inserted and I don't know why.
I already try different formats like:
"2018.01.01"
["20180101"]
2018.01.01
but won't work. I think I have to use something like cdate() but then I put all the JSON string into a textbox and all became a simple string..and doesn't work.
JSON itself doesn't care what format you use for the date. However, it is most commonly used in the JavaScript format which is ISO 8601 format.
That being said, you can create a string in this format using the following VB6 code (The below returns the time in UTC, if you want to use the local time you will need to call GetLocalTime API instead of GetSystemTime):
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Put inside module:
Private Type SYSTEMTIME '16 Bytes
wYear As Integer
wMonth As Integer
wDayOfWeek As Integer
wDay As Integer
wHour As Integer
wMinute As Integer
wSecond As Integer
wMilliseconds As Integer
End Type
Private Declare Sub GetSystemTime Lib "kernel32" (lpSystemTime As SYSTEMTIME)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Function GetJsonDateTime() As String
Dim NowTime As SYSTEMTIME
Dim sYear, sMonth, sDayOfWeek, sDay, sHour, sMinute, sSecond, sMilliseconds As String
Dim JsonDateTime As String
GetSystemTime NowTime
sYear = Format(NowTime.wYear, "0000")
sMonth = Format(NowTime.wMonth, "00")
sDay = Format(NowTime.wDay, "00")
sHour = Format(NowTime.wHour, "00") 'wHour - or + X depends local timezone
sMinute = Format(NowTime.wMinute, "00")
sSecond = Format(NowTime.wSecond, "00")
sMilliseconds = Format(NowTime.wMilliseconds, "000")
JsonDateTime = sYear & "-" & sMonth & "-" & sDay & "T" & sHour & ":" & sMinute & ":" & sSecond & "." & sMilliseconds & "Z"
GetJsonDateTime = JsonDateTime
End Function
The query it not handling the date ranges correctly.
I have a table with values stored like "01/10/2013 11:00:00 PM" for DateStamp. Ranging from 25/09/2013 1:00:00 AM to 02/10/2013.
The textboxs values are dtBegin = "28/09/2013" and dtEnd = 01/10/2013.
dSumUsuage = DSum("Average", "tblScadaHourly", "[DateStamp] >= #" & dtBegin & "#" & " AND " & "[DateStamp] < #" & dtEnd & "#" & " AND ([Station] ='" & sStationName & "')")
This query is returning an error of "Invalid use of Null". But their is data between these dates and for that station name.
This may be a classical Access date formatting error: When using the english date format with slashes, Access thinks that you use the format MM/DD/YYYY, unless this results in an invalid date (then it tries DD/MM/YYYY).
Therefore, I prefer using the international date format YYYY-MM-DD.
In your case, this might result in a query for ">=2013-09-28 And <2013-01-10", which obviously can't return any results. >> with the international format, it would ask for ">=2013-09-28 And <2013-10-01"
As the previous answer, your where string is malformed, as Your dSumUsuage is of type Date, it does not accept Null, as no records meet your bad WHERE constraints, so an error is raised.
We use this procedure:
Public Function FormatDateTimeVb(ByVal varDateTime)
FormatDateTimeVb = "#" & Format(varDateTime, "yyyy-mm-dd hh:mm:ss") & "#"
End Function
Using this function to form date time string, now we get the sum of tblScadaHourly.Average:
dSumUsuage = DSum("Average", "tblScadaHourly", _
"[DateStamp] >= " & FormatDateTimeVb(dtBegin) _
& " AND [DateStamp] < " & FormatDateTimeVb(dtEnd) _
& " AND ([Station] = '" & sStationName & "')")
I need to count the number of shifts worked by people over the last twelve months. I want to use DateSerial. My code works if I input an actual date, not DateSerial. I tested my code by changing DateSerial to return a date other than twelve months ago but my code always gives the figure for the sum of all data - going back more than twelve months. So it ignores MyDt. Where am I going wrong?
Public Function Shifts() As Integer
Dim MyDt As Date
MyDt = DateSerial(Year(Date), Month(Date) - 12, 1) - 1
Shifts = DSum("Shifts", "tblWorkNew", "GP = " & GPLookup & "And Month > #31/10/2012#") 'This works for any date.
Shifts = DSum("Shifts", "tblWorkNew", "GP = " & GPLookup & "And Month > " & MyDt) 'This only gives all data, i.e. ignores MyDt
End Function
MyDt is Date/Time, but you include it as a string in your DSum criteria argument. So transform the value to a string which the db engine recognizes as the date you intend.
Shifts = DSum("Shifts", "tblWorkNew", _
"GP = " & GPLookup & _
" And [Month] > " & Format(MyDt, "\#yyyy-mm-dd\#"))
I added a space before And because it looked like there was one missing. I assumed Month is the name of a field in your tblWorkNew table, so enclosed it in square brackets to inform the db engine you don't want the Month() function instead.