Alpha Video Training
Results 1 to 8 of 8

Thread: Trouble Creating Function

  1. #1
    Member
    Real Name
    Chris DiBianco
    Join Date
    Feb 2006
    Location
    Albany, NY
    Posts
    76

    Default Trouble Creating Function

    I built my first function the other day and I cannot get it to work properly. I place it in a calcualted field on a form and I get an error stating "Invalid or incomplete expression"

    I am calling the function from within the calculated field using 5 fields from a table.

    What am I doing wrong? I'm sure it's something very simple.




    FUNCTION calc_warranty AS C (warr_brand AS C, warr_model AS C, warr_install AS D, warr_used AS C , warr_days AS C)

    select
    case warr_used="Y" .and. (warr_install + val(warr_days) > DATE())
    warr="Islander Warranty"

    case warr_used<>"Y" .and. (warr_brand="Hot Springs" .or. warr_brand="Tiger River" .or. warr_brand="Jacuzzi" .or warr_brand="Caldera") .and. (addyears(warr_install,5) > DATE())
    warr="5 Years Parts / 5 Years Labor"

    case warr_used<>"Y" .and. warr_brand="Hawkeye" .and. (addyears(warr_install,3) > DATE())
    warr="3 Years Parts / 1 Year Labor"

    case warr_used<>"Y" .and. warr_brand="Solana" .and. (addyears(warr_install,1) > DATE())
    warr="1 Year Parts / 1 Year Labor"

    case warr_used<>"Y" .and. warr_brand="Dream Maker" .and. (addyears(warr_install,1) > DATE())
    warr="1 Years Parts / 90 Days Labor"

    case warr_used<>"Y" .and. warr_brand="Lime Light" .and. (addyears(warr_install,3) > DATE())
    warr="3 Year Parts / 3 Years Labor"

    case warr_used<>"Y" .and. warr_brand="Vita" .and. (warr_model="Ibisa" .or. warr_model="Marbella" .or. warr_model="Madrid") .and. (addyears(warr_install,2) > DATE())
    warr="2 Years Parts / 1 Year Labor"

    case warr_used<>"Y" .and. warr_brand="Vita" .and. (warr_model<>"Ibisa" .and. warr_model<>"Marbella" .and. warr_model<>"Madrid") .and. (addyears(warr_install,3) > DATE())
    warr="3 Years Parts / 1 Year Labor"

    Case Else
    warr="None"

    end select

    calc_warranty=warr

    END FUNCTION

  2. #2
    "Certified" Alphaholic Tim Kiebert's Avatar
    Real Name
    Tim Kiebert
    Join Date
    Jul 2004
    Location
    Geelong, Victoria, Australia
    Posts
    2,785

    Default Re: Trouble Creating Function

    One way to test your function is to run it in the interactive window and place actual data in the arguments. I started with the first case statement and went right throught to the last but a quicker way is to enter data that will force the function to go through all the case statements. Such as
    Code:
    ?calc_warranty("","",{},"","")
    
    ERROR: command: case warr_used<>"Y" .and. (warr_brand="Hot Springs" .or. warr_brand="Tiger River
    Argument is incorrect data type
    This shows that there is a problem with the second case statement. On further inspection I found that there is a '.' missing on one of the .or.'s

    warr_brand="Jacuzzi" .or warr_brand="Caldera"
    After fixing that the function seems to work as intended.
    Tim Kiebert
    Eagle Creek Citrus
    A complex system that does not work is invariably found to have evolved from a simpler system that worked just fine.

  3. #3
    "Certified" Alphaholic
    Real Name
    Mike Christensen
    Join Date
    Nov 2005
    Location
    Michigan U.P.
    Posts
    5,937

    Default Re: Trouble Creating Function

    Chris,
    By substituting actual values into your function it seems to work for the few instances I tried. It sounds like you are placing your field names into the function instead of values?? Have you tried using variables set to your field's values and then placing them into the function.

    I'm just guessing as anyone else would be not knowing the exact context, what the calc field is, etc. You need to provide more info and /or a sample with instructions...least for me you would.

    EDIT:
    :) Tim, Of course that is the ONE line I had modified to make it easier to work with!! LOL
    Last edited by MikeC; 05-18-2008 at 02:00 AM. Reason: late
    Mike
    __________________________________________
    It is only when we forget all our learning that we begin to know.
    It's not what you look at that matters, it's what you see.
    Henry David Thoreau
    __________________________________________




  4. #4
    "Certified" Alphaholic G Gabriel's Avatar
    Real Name
    G. Gabriel
    Join Date
    Oct 2004
    Posts
    7,204

    Default Re: Trouble Creating Function

    Modified:
    Code:
    FUNCTION calc_warranty AS C (warr_brand AS C, warr_model AS C, warr_install AS D, warr_used AS C , warr_days AS N)
    
    select
    case warr_used="Y" .and. (warr_install + warr_days > DATE())
    warr="Islander Warranty"
    
    case warr_used<>"Y"
    select
    case  (warr_brand="Hot Springs" .or. warr_brand="Tiger River" .or. warr_brand="Jacuzzi" .or warr_brand="Caldera") .and. (addyears(warr_install,5) > DATE())
    warr="5 Years Parts / 5 Years Labor"
    
    case warr_brand="Hawkeye" .and. (addyears(warr_install,3) > DATE())
    warr="3 Years Parts / 1 Year Labor"
    
    case warr_used<>"Y" .and. warr_brand="Solana" .and. (addyears(warr_install,1) > DATE())
    warr="1 Year Parts / 1 Year Labor"
    
    case warr_brand="Dream Maker" .and. (addyears(warr_install,1) > DATE())
    warr="1 Years Parts / 90 Days Labor"
    
    case  warr_brand="Lime Light" .and. (addyears(warr_install,3) > DATE())
    warr="3 Year Parts / 3 Years Labor"
    
    case  warr_brand="Vita" .and. (warr_model="Ibisa" .or. warr_model="Marbella" .or. warr_model="Madrid") .and. (addyears(warr_install,2) > DATE())
    warr="2 Years Parts / 1 Year Labor"
    
    case  warr_brand="Vita" .and. (warr_model<>"Ibisa" .and. warr_model<>"Marbella" .and. warr_model<>"Madrid") .and. (addyears(warr_install,3) > DATE())
    warr="3 Years Parts / 1 Year Labor"
    end select
    case warr_brand<>""
    warr="None"
    end select
    calc_warranty=warr
    
    END FUNCTION

  5. #5
    Member
    Real Name
    Chris DiBianco
    Join Date
    Feb 2006
    Location
    Albany, NY
    Posts
    76

    Default Re: Trouble Creating Function

    Tim,

    Thanks! The missing period was the problem. I spent several hours trying to figure this out. I thought that I was putting the function into the calculated field wrong. I new it had to be a simple mistake. Sometimes it just takes a second pair of eyes.

    I also didn't know how to use the interactive screen. That is very handy.

    Thanks again!

  6. #6
    VAR
    Real Name
    Martin W. Cole
    Join Date
    Apr 2000
    Location
    Terrell, Texas (near Dallas)
    Posts
    5,957

    Default Re: Trouble Creating Function

    if you are using this on a form, then you need to modify it to something like this. This assumes you are referencing fields on a form made from a single table, or if a set, then the parent table.

    t=table.current()
    case t.warr_brand="Hawkeye" .and. (addyears(t.warr_install,3) > DATE())
    t.warr="3 Years Parts / 1 Year Labor"

    or - better

    case warr_brand.value="Hawkeye" .and. (addyears(warr_install.value,3) > DATE())
    warr.value="3 Years Parts / 1 Year Labor"

    if you say warr_brand="hawkeye" alpha will search for a variable named warr_brand - so you have to be more specific in telling Alpha what you are referencing.

    I say the second approach is better for two reasons" 1) working at a form level rather than a table level is easier, especially if you are getting started and 2) if other fields on the form have already been changed, you are still referencing the the "changed but not yet saved" record values.

    t=table.current()
    t.somefield="xxx" - table method

    somefield.value="xxx" - form level
    Cole Custom Programming - Terrell, Texas
    972 524 8714
    martin_w_cole@msn.com

    ____________________
    "A young man who is not liberal has no heart, but an old man who is not conservative has no mind." GB Shaw

  7. #7
    Member
    Real Name
    Chris DiBianco
    Join Date
    Feb 2006
    Location
    Albany, NY
    Posts
    76

    Default Re: Trouble Creating Function

    Martin,

    Thanks for that pointer.

    The name of the table that these fields are coming from is "spas"

    Could I also do it like this, specifically referencing those fields?

    case spas->warr_brand="Hawkeye" .and. (addyears(spas->warr_install,3) > DATE())
    warr.value="3 Years Parts / 1 Year Labor"

  8. #8
    VAR
    Real Name
    Martin W. Cole
    Join Date
    Apr 2000
    Location
    Terrell, Texas (near Dallas)
    Posts
    5,957

    Default Re: Trouble Creating Function

    Try it - but there is an issue I think you will run into
    if the record has already been changed, (some other field was changed,) I suspect you may get error messages.

    you could also try
    table.current().warr_brand="xxxxx"
    Cole Custom Programming - Terrell, Texas
    972 524 8714
    martin_w_cole@msn.com

    ____________________
    "A young man who is not liberal has no heart, but an old man who is not conservative has no mind." GB Shaw

Similar Threads

  1. is there a built command/function to replace this user Function?
    By Graham Wickens in forum Alpha Five Version 6
    Replies: 4
    Last Post: 12-07-2005, 11:54 AM
  2. I think I'm in trouble?
    By A4BEST in forum Alpha Five Version 6
    Replies: 3
    Last Post: 11-07-2005, 09:43 PM
  3. Serious trouble here
    By Curtis Nunn in forum Alpha Five Version 5
    Replies: 3
    Last Post: 06-03-2004, 08:07 AM
  4. Lookup function vs creating set
    By Lawrence Fox in forum Alpha Five Version 5
    Replies: 10
    Last Post: 07-17-2003, 01:38 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •