Alpha Video Training
Results 1 to 16 of 16

Thread: Simple Loop Example

  1. #1
    Member
    Real Name
    john fringer
    Join Date
    Dec 2004
    Posts
    104

    Default Simple Loop Example

    I am writing my first xbasic loop. I've done a search and found some good samples, but more complex than what I am trying to achieve. I've written some vba loops, but nothing to complicated.

    I'm trying to loop through a table, looking for account numbers that match the account number in the current table, then compare a couple of dates, eventually finding a "match".

    If anyone could point in the right direction, as always, it would be greatly appreciated.

    Thanks...John

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

    Default Re: Simple Loop Example

    Hi John,
    There are so many ways to do what you are suggesting that many times, I think, it just comes down to personal preference--unless something is needed to be sped up in a script.

    Here is one way that I scraped off the board sometime or another.

    Code:
    'WHILE ... END WHILE is a control structure that repeats the statements it contains 
    'while the Logical Expression evaluates to TRUE. Execution resumes at the line following the 
    'end while statement when the Logical Expression is FALSE or when the EXIT while 'statement is encountered.
    
    'Example
    
    'This script searches for a customer whose last name is 'Smith'.
    
    tbl = table.current()
    search_name = "Smith"
    tbl.fetch_first()
    
    while .not. tbl.fetch_eof()
    
        if (trim(tbl.last_name) = search_name) then
            goto NAME_FOUND
        end if
        tbl.fetch_next()
    end while
       ui_msg_box("Search", "Not Found.")
       end
    
      NAME_FOUND:
       ui_msg_box("Search", search_name + " has been found.")
    end
    It sure sounds like it may even be possible to just use a query to do as you want--more specifics and maybe more specific answers or possibilities will surface....
    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
    __________________________________________




  3. #3
    Member
    Real Name
    Doug Page
    Join Date
    Jan 2002
    Location
    Vancouver, BC Canada
    Posts
    963

    Default Re: Simple Loop Example

    John,

    From what you have said, I would seriously think about Mike's last point. I think that you are going down the wrong road in your thinking by not understanding the advantages of queries over creating loops. Loops take forever to go through if you have any quantity of records. Issue the query and then you are working with only the records that meet your criteria. This makes the rest of your code much easier.

  4. #4
    Member
    Real Name
    john fringer
    Join Date
    Dec 2004
    Posts
    104

    Default Re: Simple Loop Example

    Thanks! I think you are both correct that a query would be the best option. What I'm confused about is .... after the query is run, how would I get the value I need from the query to a different table? Maybe this doesn't make sense.

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

    Default Re: Simple Loop Example

    John,
    Are you after just one value? If so then, once found, you could set its value to a variable, open the table, and do what is needed....which is also unclear as to what you want to do once you have this value--a new record, update a value, use the value to query a different table...............

    Sorry to be so vague but not much to go on really.

    BTW--seems like I can connect about once every half hour or so and then only for a few minutes so until this messageboard problem is fixed it may be a while between responses!!
    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
    __________________________________________




  6. #6
    Volunteer Moderator Peter.Greulich's Avatar
    Real Name
    Peter Greulich
    Join Date
    Apr 2000
    Location
    Boston, MA
    Posts
    11,644

    Default Re: Simple Loop Example

    Quote Originally Posted by John Fringer View Post
    .... after the query is run, how would I get the value I need from the query to a different table?
    Making a bunch of assumptions:
    Code:
    tblc = table.current()
    vAccount_No = tblc.Account_No
    ''========================QUERY
    tbl = table.open("Accounts")
    query.description = ""
    query.order = ""        
    query.filter = "Account_No=vAccount_No .and. between(cdate(Date),'" + cdate(vDate1) + "','" + cdate(vDate2) + "')"
    query.options = ""    
    Q = tbl.query_create()
    ''************************No of records
    nrecs = Q.records_get()
    IF nrecs = 0
        Q.drop()
        tbl.close()
        ui_msg_box("Null Query","No records were found matching the criteria.",UI_INFORMATION_SYMBOL)
        END
    END IF
    ''========================FETCH 
    tbl.fetch_first()
    WHILE .not. tbl.fetch_eof()
        IF tbl.your_value = vYourValue
            tbl2.open("other_table")
            tbl2.enter_begin()
            tbl2.somevalue = tbl.value_you_want_to_transfer
            tbl2.enter_end(.t.)
            tbl2.close()
        END IF
        
        statusbar.percent(i,nrecs)
        i = i + 1
        tbl.fetch_next()
    END WHILE
    tbl.close()
    END

  7. #7
    Member
    Real Name
    john fringer
    Join Date
    Dec 2004
    Posts
    104

    Default Re: Simple Loop Example

    Thank you so much Peter. You are a bit of a mind reader!
    I'm getting an error "Mission or Invalid Key Word" on the following line: tbl2.rate_per_kwh=tbl.c_rate


    tblc = table.current()
    vAccount_No = tblc.ACCT_NUMBER
    ''========================QUERY
    tbl = table.open("contracts")
    query.description = ""
    query.order = ""
    query.filter = "Account_Number=vAccount_No .and. between(cdate(date),'" + cdate(start_date) + "','" + cdate(end_date) + "')"
    query.options = ""
    Q = tbl.query_create()
    ''************************No of records
    nrecs = Q.records_get()
    IF nrecs = 0
    Q.drop()
    tbl.close()
    ui_msg_box("Null Query","No records were found matching the criteria.",UI_INFORMATION_SYMBOL)
    END
    END IF
    ''========================FETCH
    tbl.fetch_first()
    WHILE .not. tbl.fetch_eof()
    IF tbl.Acct_number = vaccount_no
    tbl2.open("invoice_alpha")
    tbl2.enter_begin()
    tbl2.rate_per_kwh=tbl.c_rate
    tbl2.enter_end(.t.)
    tbl2.close()
    END IF

    statusbar.percent(i,nrecs)
    i = i + 1
    tbl.fetch_next()
    END WHILE
    tbl.close()
    END__________________

  8. #8
    Volunteer Moderator Peter.Greulich's Avatar
    Real Name
    Peter Greulich
    Join Date
    Apr 2000
    Location
    Boston, MA
    Posts
    11,644

    Default Re: Simple Loop Example

    Quote Originally Posted by John Fringer View Post
    I'm getting an error "Mission or Invalid Key Word" on the following line: tbl2.rate_per_kwh=tbl.c_rate
    Are both field types the same - e.g. character or numeric? - and same size?

  9. #9
    Member
    Real Name
    john fringer
    Join Date
    Dec 2004
    Posts
    104

    Default Re: Simple Loop Example

    ah ... thanks

  10. #10
    Member
    Real Name
    john fringer
    Join Date
    Dec 2004
    Posts
    104

    Default Re: Simple Loop Example

    The code runs, but it's not putting the value in tbl2. I'll keep investigating.

    Thanks again...John

  11. #11
    "Certified" Alphaholic Stan Mathews's Avatar
    Real Name
    Stan Mathews
    Join Date
    Apr 2000
    Location
    Bowling Green, KY
    Posts
    25,119

    Default Re: Simple Loop Example

    Check to make sure rate_per_kwh is not defined as a calculated field.

  12. #12
    "Certified" Alphaholic Tom Henkel's Avatar
    Real Name
    Tom Henkel
    Join Date
    May 2002
    Location
    New Jersey, USA
    Posts
    1,875

    Default Re: Simple Loop Example

    Thank you so much Peter. You are a bit of a mind reader!
    I'm getting an error "Mission or Invalid Key Word" on the following line:

    tbl2.rate_per_kwh=tbl.c_rate


    tblc = table.current()
    Shouldn't it be tblc.rate or tblc.c_rate

    Tom

  13. #13
    Member
    Real Name
    Geoff Meredith
    Join Date
    Aug 2006
    Posts
    637

    Default Re: Simple Loop Example

    Quote Originally Posted by Tom Henkel View Post
    Shouldn't it be tblc.rate or tblc.c_rate

    Tom
    I think the tbl2.rate_per_kwh=tbl.c_rate is correct but difficult to be certain without further information. I am assuming that the contracts table has a field that stores each contract kw/h rate, and that field is c_rate.
    tbl is the pointer for the contracts table so it should be okay.

    However, I don't really understand what the loop is trying to achieve.

    tbl.fetch_first()
    WHILE .not. tbl.fetch_eof()
    IF tbl.Acct_number = vaccount_no
    tbl2.open("invoice_alpha")
    tbl2.enter_begin()
    tbl2.rate_per_kwh=tbl.c_rate
    tbl2.enter_end(.t.)
    tbl2.close()
    END IF
    It looks for a record in the contracts table where the acct_number=vaccount_no.
    It then opens invoice_alpha table, creates a new record with just the rate_per_kwh field = tbl.c_rate.

    I am not following the logic of what is being achieved and as I'm only just getting my head round using xbasic would like to know. Is the current table also "invoice_alpha" or are there three different tables. I have used similar scripts but its normally been to change a record rather than create a new one, especially when only one field is being entered without what appears to be a linking field. I know this is probably down to my lack of understanding but would appreciate more information, I am keen to learn from these threads and I haven't got Peter's mind reading skills:).

    Thanks

    Geoff

  14. #14
    "Certified" Alphaholic Tom Henkel's Avatar
    Real Name
    Tom Henkel
    Join Date
    May 2002
    Location
    New Jersey, USA
    Posts
    1,875

    Default Re: Simple Loop Example

    My bad. I was looking at tblc, and didn't see tbl.:o

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

    Default Re: Simple Loop Example

    also, unless something has changed, I think you need to change the filter

    query.filter = "Account_Number=var->vAccount_No .and. between(cdate(date),'" + cdate(start_date) + "','" + cdate(end_date) + "')"

    or

    query.filter = "Account_Number=var->vAccount_No .and. between(date,var->start_date, var->end_date)"

    or

    query.filter = "Account_Number=var->vAccount_No .and. between(cdate(date),cdate(var->start_date), cdate(var->end_date))"
    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

  16. #16
    Volunteer Moderator Peter.Greulich's Avatar
    Real Name
    Peter Greulich
    Join Date
    Apr 2000
    Location
    Boston, MA
    Posts
    11,644

    Default Re: Simple Loop Example

    Quote Originally Posted by martinwcole View Post
    also, unless something has changed, I think you need to change the filter

    var->
    FYI:
    You don't need to use the var->
    I never do.

Similar Threads

  1. Loop construct
    By sgbotsford in forum Alpha Five Version 7
    Replies: 4
    Last Post: 11-21-2005, 10:26 PM
  2. Continue in For loop
    By elidaho in forum Alpha Five Version 6
    Replies: 5
    Last Post: 06-30-2005, 02:03 PM
  3. Help with endless while loop
    By Jerry Gray in forum Alpha Five Version 6
    Replies: 3
    Last Post: 06-02-2005, 11:31 AM
  4. Creating A Loop ?
    By Russell Sanderlin Sr in forum Alpha Five Version 5
    Replies: 4
    Last Post: 12-22-2004, 07:03 AM
  5. Infinite Loop
    By Melvin Davidson in forum Alpha Five Version 5
    Replies: 9
    Last Post: 04-03-2003, 07:28 AM

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
  •