4 Replies Latest reply on May 15, 2006 4:14 PM by DJ_Jamba


    JohnGree Level 1
      Hi i have this code in my query.
      WHERE MatchYear = <CFIF isdefined (FORM.Years)>#FORM.Years#<CFELSE>current year</CFIF>

      this wont work, what i am tring to do is if the form is defined then the where value will be the form value, if not defined then i would like to have the year now not sure what the current year code is

      can anyone help
        • 1. where
          The ScareCrow Level 1
          <CFIF isdefined ("FORM.Years")>#FORM.Years#<CFELSE>#Year(Now())#</CFIF>

          • 2. Re: where
            jdeline Level 1
            Be careful. If FORM.Years is a text box, it would always be defined. If there is nothing in the text box, its value would be "". If FORM.Years is a radio button or checkbox, it would only be defined if it is selected. You may want to do something like the code below.
            • 3. Re: where
              Except a user can easily enter something that's not a year into a text box... so something like this should be a minimum:
              IsDefined("Form.Years") AND Len(Form.Years) AND IsNumeric(Form.Years)

              And it might be wise to also do a check to ensure it's actually a valid year - something along the lines of this should work...
              • 4. where
                DJ_Jamba Level 1
                You are all correct!
                Not to mention all of this should be done outside of the query:

                <CFPARAM NAME = "theYear" DEFAULT="#Year(Now())#">
                <CFIF IsDefined("Form.Years") AND Len(Form.Years) AND IsNumeric(Trim(Form.Years)) AND REFind("^(19|20)?[0-9][0-9]$",Trim(Form.Years))>
                <CFSET theYear = "#Trim(Form.Years)#">
                <CFQUERY NAME="getSomething" DATASOURCE="#myDS#">
                SELECT something FROM somewhere WHERE MatchYear = #theYear#

                EDIT: and just for good measure: