Announcement

Collapse

The Alpha Software Forum Participation Guidelines

The Alpha Software Forum is a free forum created for Alpha Software Developer Community to ask for help, exchange ideas, and share solutions. Alpha Software strives to create an environment where all members of the community can feel safe to participate. In order to ensure the Alpha Software Forum is a place where all feel welcome, forum participants are expected to behave as follows:
  • Be professional in your conduct
  • Be kind to others
  • Be constructive when giving feedback
  • Be open to new ideas and suggestions
  • Stay on topic


Be sure all comments and threads you post are respectful. Posts that contain any of the following content will be considered a violation of your agreement as a member of the Alpha Software Forum Community and will be moderated:
  • Spam.
  • Vulgar language.
  • Quotes from private conversations without permission, including pricing and other sales related discussions.
  • Personal attacks, insults, or subtle put-downs.
  • Harassment, bullying, threatening, mocking, shaming, or deriding anyone.
  • Sexist, racist, homophobic, transphobic, ableist, or otherwise discriminatory jokes and language.
  • Sexually explicit or violent material, links, or language.
  • Pirated, hacked, or copyright-infringing material.
  • Encouraging of others to engage in the above behaviors.


If a thread or post is found to contain any of the content outlined above, a moderator may choose to take one of the following actions:
  • Remove the Post or Thread - the content is removed from the forum.
  • Place the User in Moderation - all posts and new threads must be approved by a moderator before they are posted.
  • Temporarily Ban the User - user is banned from forum for a period of time.
  • Permanently Ban the User - user is permanently banned from the forum.


Moderators may also rename posts and threads if they are too generic or do not property reflect the content.

Moderators may move threads if they have been posted in the incorrect forum.

Threads/Posts questioning specific moderator decisions or actions (such as "why was a user banned?") are not allowed and will be removed.

The owners of Alpha Software Corporation (Forum Owner) reserve the right to remove, edit, move, or close any thread for any reason; or ban any forum member without notice, reason, or explanation.

Community members are encouraged to click the "Report Post" icon in the lower left of a given post if they feel the post is in violation of the rules. This will alert the Moderators to take a look.

Alpha Software Corporation may amend the guidelines from time to time and may also vary the procedures it sets out where appropriate in a particular case. Your agreement to comply with the guidelines will be deemed agreement to any changes to it.



Bonus TIPS for Successful Posting

Try a Search First
It is highly recommended that a Search be done on your topic before posting, as many questions have been answered in prior posts. As with any search engine, the shorter the search term, the more "hits" will be returned, but the more specific the search term is, the greater the relevance of those "hits". Searching for "table" might well return every message on the board while "tablesum" would greatly restrict the number of messages returned.

When you do post
First, make sure you are posting your question in the correct forum. For example, if you post an issue regarding Desktop applications on the Mobile & Browser Applications board , not only will your question not be seen by the appropriate audience, it may also be removed or relocated.

The more detail you provide about your problem or question, the more likely someone is to understand your request and be able to help. A sample database with a minimum of records (and its support files, zipped together) will make it much easier to diagnose issues with your application. Screen shots of error messages are especially helpful.

When explaining how to reproduce your problem, please be as detailed as possible. Describe every step, click-by-click and keypress-by-keypress. Otherwise when others try to duplicate your problem, they may do something slightly different and end up with different results.

A note about attachments
You may only attach one file to each message. Attachment file size is limited to 2MB. If you need to include several files, you may do so by zipping them into a single archive.

If you forgot to attach your files to your post, please do NOT create a new thread. Instead, reply to your original message and attach the file there.

When attaching screen shots, it is best to attach an image file (.BMP, .JPG, .GIF, .PNG, etc.) or a zip file of several images, as opposed to a Word document containing the screen shots. Because Word documents are prone to viruses, many message board users will not open your Word file, therefore limiting their ability to help you.

Similarly, if you are uploading a zipped archive, you should simply create a .ZIP file and not a self-extracting .EXE as many users will not run your EXE file.
See more
See less

Simple Loop Example

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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
    __________________________________________



    Comment


    • #3
      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.

      Comment


      • #4
        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.

        Comment


        • #5
          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
          __________________________________________



          Comment


          • #6
            Re: Simple Loop Example

            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
            Peter
            AlphaBase Solutions, LLC

            Peter@AlphaBaseSolutions.com
            https://www.alphabasesolutions.com


            Comment


            • #7
              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__________________

              Comment


              • #8
                Re: Simple Loop Example

                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?
                Peter
                AlphaBase Solutions, LLC

                Peter@AlphaBaseSolutions.com
                https://www.alphabasesolutions.com


                Comment


                • #9
                  Re: Simple Loop Example

                  ah ... thanks

                  Comment


                  • #10
                    Re: Simple Loop Example

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

                    Thanks again...John

                    Comment


                    • #11
                      Re: Simple Loop Example

                      Check to make sure rate_per_kwh is not defined as a calculated field.
                      There can be only one.

                      Comment


                      • #12
                        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

                        Comment


                        • #13
                          Re: Simple Loop Example

                          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

                          Comment


                          • #14
                            Re: Simple Loop Example

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

                            Comment


                            • #15
                              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

                              Comment

                              Working...
                              X