openbd cfloop over a date - date

I'm trying to convert my sites from CF8 to openBD. I have a cfloop in a site that loops over a date range.
In essence, I want to insert a new record into the db for every 2 weeks (step) of a date range (from and to)
my loop looks like this...
<cfloop
from = "#form.startDate#"
to = "#form.endDate#"
index = "i"
step = "#theStep#"
>
This works perfectly in CF8, in openBD, I get this error...
Data not supported: value [11/05/09] is not a number
Any ideas of a work around?
Thx

Your problem lies in not checking for ambiguous locale dependent date strings from your FORM.
A more robust version would be this:
<cfset SetLocale("English (US)")> <!--- set expected input locale here --->
<cfif LSIsDate(form.startDate) and LSIsDate(form.endDate)>
<cfset theStep = 14>
<cfloop
from = "#LSParseDate(form.startDate)#"
to = "#LSParseDate(form.endDate)#"
index = "i"
step = "#theStep#"
>
<!--- do stuff --->
</cfloop>
<cfelse>
<!--- output some error message --->
</cfif>
It would be helpful to restrict people to entering unambiguous date formats into the FORM, like "yyyy-mm-dd".
The "value is not a number" error comes from the fact that the loop still goes over numbers, even if you feed it dates. It uses a numerical representation of these dates then, but they must be valid and intelligible for that to work.

I can't see your code, but here's my first suggestion:
<cfset current = [your begin date]>
<cfloop condition = "datecompare(enddate, current)">
[do stuff]
<cfset current = dateadd('d', 14, current)>
</cfloop>
HTH.

As Ben says, your code isn't there - you need to use the 101 010 icon to create a code block for it.
Here's another solution which should work:
<cfloop index="Current" from="#parseDateTime(StartDate)#" to="#parseDateTime(EndDate)#" step="14">
[do stuff]
</cfloop>

Related

Convert string to utf-8 unicode in ColdFusion

I need to convert a string to a UTF8 encoded format and I'm not sure how to proceed.
Is there any function within ColdFusion to convert a string into UTF-8, such as on this website?
For example, typing in "stackoverflow.com/questions/ask" into the above website gives the result:
\x73\x74\x61\x63\x6B\x6F\x76\x65\x72\x66\x6C\x6F\x77\x2E\x63\x6F\x6D\x2F\x71\x75\x65\x73\x74\x69\x6F\x6E\x73\x2F\x61\x73\x6B
I am not very familiar with encoding, however my instructions were to encode a string to UTF-8. The example I was given gave an encoded result of the below for example.
/re/r/434/t//4r3/t434/4t/t3/3/4t/43tt/53/
I am not sure if this is a real representation of an encoded string or if it was just typed out to give a visual example. Is there a format that looks like that? And is it different than the format from the first example?
Thank you!
I think you can use a combination of CharsetDecode() and CharsetEncode() to accomplish this.
<cfset my_string = "test">
<cfset binary_my_string = CharsetDecode(my_string, "ASCII")>
<cfset utf8_my_string = CharsetEncode(binary_my_string, "utf-8")>
You'd just need to substitute the correct initial encoding for "ASCII" in my example.
<cfset str = "stackoverflow.com/questions/ask">
<cfset hexStr = "">
<cfloop index="i" from="0" to="#len(str)-1#">
<!--- Pick out each character in the string. Remember that charAt() starts at index 0. --->
<cfset ch = str.charAt(i)>
<!--- The decimal value of the Unicode character. ColdFusion uses the Java UCS-2 representation of Unicode characters, up to a value of 65536. --->
<cfset charDecVal = asc(ch)>
<!--- The decimal value of the character, upper-casing the letters.--->
<cfset charHexVal = uCase(formatBaseN(charDecVal,"16"))>
<!--- Append the characters together into a Hex string, using delimiter '\x' --->
<cfset hexStr = hexStr & "\x" & charHexVal>
</cfloop>
<cfscript>
writeoutput(hexStr);
</cfscript>

MS Access Date range filtering displaying no records only for a certain range

I Do have a form with a sub-form (continuous form) on which I would like to apply a date range filter. The thing is that it works partially.
I am using a simple piece of VBA that seem to be THE method. I am using two controls (start & End) and a button to apply the filter.
He is my code sample
With Me.Sub_Desi_Schedule.Form
.Filter = "[Task_End] " & " BETWEEN " & "#" & DateStart & "#" & " AND " & "#" & DateEnd & "#"
.FilterOn = True
And it works fine except when I pick a date range with a value in the last days of the month (as starting period) AND any values within the first week of the next month (as ending period).
Results are blanks even if there is values AND no error message of any kind to help me trouble shooting.
I thought that it could have been something with the date format. I tried to force it to DDMMYYY. No effect and It had the same strange behavior.
And for any other ranges picked later on the month, it works fine...
Does anyone here had this problem before? Is there something obvious I am missing?
I suspect you're right that the problem is due to date format.
Examine the completed filter string the code creates. Do that by using a variable to hold it. Then you can use Debug.Print to see it, and later assign the variable to the form's .Filter property.
Dim strFilter As String
With Me.Sub_Desi_Schedule.Form
strFilter = "[Task_End] BETWEEN #" & DateStart & "# AND #" & DateEnd & "#"
Debug.Print strFilter ' <- view this in Immediate window; Ctrl+g will take you there
.Filter = strFilter
.FilterOn = True
End With
You can avoid problems due to date format by using the unambiguous yyyy-m-d format for those Date/Time values.
strFilter = "[Task_End] BETWEEN " & Format(DateStart, "\#yyyy-m-d\#") & " AND " & Format(DateEnd, "\#yyyy-m-d\#")
I added the # delimiters within the Format expressions. But you don't need to do it that way; I think this should work as well ...
strFilter = "[Task_End] BETWEEN #" & Format(DateStart, "yyyy-m-d") & "# AND #" & Format(DateEnd, "yyyy-m-d") & "#"

MS-Access between filter on form

Hi there I've created a form on access which looks like this:
I can make it so, that all the filters work, except for the Year and Length filters.
The Year boxes are unbound, and the left is called Year1 and the right one is Year2. I've tried to use Me.Filter code, but it doesn't work. It doesn't come up with an error, it just won't filter my data. So what I need is to make it so that the user can enter a year in Year1 and in Year2, and it filters the data between those two years. So for example they put 2000 in Year1 and 2010 in Year2, and then when they run the query it only shows data from 2000 to 2010.
This is the code I'm currently using:
Private Sub Year2_AfterUpdate()
Me.Filter = "[Year] BETWEEN #" & Me.Year1 & "# AND #" & Me.Year2 & "#"
Me.Filteron = true
Any help would be much appreciated! :)
(If you could explain what code does what that would be much appreciated too, so that I can learn to write it myself, and so that I can understand better! Thanks!)
You only use the "#" characters when you are using a date literal in your filter. If it is just a numeric you would use:
Me.Filter = "[Year] BETWEEN " & Me.Year1 & " AND " & Me.Year2
If [Year] actually IS a date then you should use:
Me.Filter = "[Year] BETWEEN #01/01/" & Me.Year1 & "# AND #12/31/" & Me.Year2 & "#"
Which will evaluate to [Year] BETWEEN #01/01/2010# AND #12/31/2011# if you enter 2010 and 2011 in the year textboxes.
You must use the name of the query (frmAufträgeQuery) and NOT the Form (frmAufträge):
On Error Resume Next
FromDate = "01.01.2021"
ToDate = "01.04.2021"
Me.Filter = "([frmAufträgeQuery].[Administrativ abgeschlossen] Between #" & Format(FromDate, "yyyy-mm-dd") & "# AND #" & Format(ToDate, "yyyy-mm-dd") & "#)"
Me.FilterOn = True

Does Len function only evaluate numerical results?

Why does the following code not output "Error" if the form is submitted with a blank field? Does Len only evaluate numerical values?
<cfif NOT Len(Trim("Form.myField"))>
<cfoutput>Error</cfoutput>
</cfif>
The following also does not evaluate as expected:
<cfif Len(Trim("Form.myField")) IS 0>
<cfoutput>Error</cfoutput>
</cfif>
HTML:
<input type="text" name="myField" value="">
Because it's evaluating the literal string "Form.myField", which is not length 0.
Try: <cfif len(trim(form.myField)) EQ 0>
are you sure you're supposed to pass in the parameter in quotes within the trim function? it may be literally trimming the string "Form.myField"

How to print date in the format of mm/dd/yyyy in VB

I need to print the date in the format of mm/dd/yyyy.
if the date is 4/24/2009 it should print the date as 04/24/2009.
that is zero padding is also needed..
I used date function to get the current date...but the date is getting in the format of m/dd/yyyy...
Tested in the immediate window and is working for me (output as a comment)
Format(Now, "MM/dd/yyyy") '04/29/2009
Format(Date, "MM/dd/yyyy") '04/29/2009
Format(CStr(Now), "MM/dd/yyyy") '04/29/2009
Format(Date$, "MM/dd/yyyy") '04/29/2009
Format(CDate(Date), "MM/dd/yyyy")'04/29/2009
So whether it is string or datetime should not matter.
Edit: Saw your comment to Fredrik. It doesn't matter how it looks like when you save it to the db table (column date format would be a property of the db and not your program's (or vb's) responsibility). Just format the value as and when you retrieve it from the db.
Note that the "/" character in date formatting functions has a special meaning, as "date separator". This means that i may be replaced with the date separator for the current locale that the program is executed in (here in Sweden it would be replaced with "-" for instance). In order to ensure that you do indeed get the "/" character in the output, I think this would work (I don't have a VB installation to verify with):
Format(date, "MM'/'dd'/'yyyy")
just for the record, escaping the slash will work
Format(dt,"MM\/dd\/yyyy")
Try the next code:
Format(dt,"MM/dd/yyyy")
When you enter date in whatever format it will convert default value so do one thing that in access change your data type date/time to text then it can't affect to your work sure.
I also use VB6 and need to format date in my txt report
this works for me
Format$(Now, "yyyy-mm-dd-00.00.00")
but only if I declare date as string
You can try like this also depending upon your requirement
Dim strDate As String
Dim strDate1() As String
strDate = FormatDateTime(Now, vbGeneralDate)
If InStr(strDate, " ") > 0 Then
strDate1 = Split(strDate, " ")
Dim datDate1 As Date
If Month(strDate1(0)) < 10 Then
txtDate.Text = "0" + strDate1(0)
Else
txtDate.Text = strDate1(0)
End If
Else
End If
Formatting DateTime as a string is straightforward. Often we use format patterns like "HH." But methods like ToShortDateString are also useful.
Example. First we get the current time through DateTime.Now. When you execute these code examples, the current DateTime will be different on your computer.
Here: A format string beginning with MMM (for the month) is used. Look at how the pattern matches up to the output of the program.
VB.NET program that uses format string with DateTime
Module Module1
Sub Main()
' Use current time.
' ... Use a format.
' ... Write to console.
Dim time As DateTime = DateTime.Now
Dim format As String = "MMM ddd d HH:mm yyyy"
Console.WriteLine(time.ToString(format))
End Sub
End Module
Output
Feb Tue 21 13:26 2017
strDate = Format(strDate, "yyyy-mm-dd")
BillTime = Format(BillTime, "hh:mm:ss")