Alpha Software Mobile Development Tools:   Alpha Anywhere    |   Alpha TransForm subscribe to our YouTube Channel  Follow Us on LinkedIn  Follow Us on Twitter  Follow Us on Facebook

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

pass table name to a function

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

    pass table name to a function

    Is there a way to pass a table name to a function, which then opens a table based on that name? Here's what I'm trying to use, but I keep getting an error when I execute the script on the line where i have underlined:

    FUNCTION frequency AS F (itbl AS C, ifld AS C )

    dim tbl as P
    dim outlist as U
    dim keys as U
    dim k as C
    dim num as N
    dim qry as P


    tbl = table.open(itbl,FILE_RO_SHARED)
    num = tbl.records_get()

    'query.filter = unique_key_value()
    query.order = "VAR->ifld"
    query.options = "U"
    debug(1)

    qry = tbl.query_create()

    num = qry.records_get()
    qry.close()
    tbl.close()
    dim temparr[num] as P

    temparr = initialize_from_table(itbl)
    j = temparr.first_empty()

    for i = 1 to j
    k = temparr[i].ifld
    num = tablecount(s_quote(itbl),s_quote(ifld) +" = " + s_quote(k))
    next i

    query.filter = ""
    query.order = ""
    query.options = ""



    END FUNCTION
    David A. Volgas, MD

    #2
    Re: pass table name to a function

    It would help if we knew the actual error message but I believe your problem is here:

    query.order = "VAR->ifld"

    This is telling the query to sort the results by a field called specifically Var->ifld

    If you want it to sort by a field name that is stored in the "ifld" variable, I think you can just remove the quotes.

    query.order = VAR->ifld

    or, depending on your preference, simply:

    query.order = ifld

    Comment


      #3
      Re: pass table name to a function

      Cal,

      it fails at the line I highlighted above with a message "No such field"

      I used the debugger to examine the num variable which correctly shows 147 records. I tried commenting out the query parameters and that did not solve the problem, either.

      Thank you!
      David A. Volgas, MD

      Comment


        #4
        Re: pass table name to a function

        Originally posted by davidv43 View Post
        it fails at the line I highlighted above with a message "No such field"
        David,

        It fails at the qry = tbl.query_create() because query.order = "VAR->ifld" is just a harmless variable assignment (query.order is the variable). But when you try to pass the nonsensical value "VAR->ifld" to the query command [tbl.query_create()], she breaks at that point. As Cal said, Alpha thinks you want to sort a field called VAR->ifld, which, of course, doesn't exist.
        Peter
        AlphaBase Solutions, LLC

        [email protected]
        https://www.alphabasesolutions.com


        Comment


          #5
          Re: pass table name to a function

          Did you try just removing the quotes as in my initial suggestion?

          Also, it may be that query.filter has been set somewhere else. Try setting it to
          query.filter = ""
          rather than just commenting it out. If you comment it out, it will use whatever value it was last set to.

          This works fine on my system: (I used fetch_last() in my test because there are blank last names in the table. My initial tests did return <blank> but I prefer seeing an actual value.)
          Code:
          ifld = "Last_Namef"
           
          tp = table.open("customer")
          query.filter = ""
          query.order = VAR->ifld
          query.options = "U"
          ndx = tp.query_create()
          tp.fetch_last()
          ui_msg_box( "RESULT", tp.Last_namef )
          tp.close()
          This doesn't give me an error (which seems rather odd) but it doesn't set the order properly either. It simply uses the record number index:
          Code:
          ifld = "Last_Namef"
           
          tp = table.open("customer")
          query.filter = ""
          query.order = [B][COLOR=red]"[/COLOR][/B]VAR->ifld[B][COLOR=red]"[/COLOR][/B]
          query.options = "U"
          ndx = tp.query_create()
          tp.fetch_last()
          ui_msg_box( "RESULT", tp.Last_namef )
          tp.close()
          FWIW: Setting the options to "U" or adding unique_key_value() to the filter should both yield the same results.

          Comment


            #6
            Re: pass table name to a function

            thank you guys... when I added the query.description variable, it now works, or should I say, doesn't fail.

            My goal with this function is to call a function:
            FUNCTION frequency AS F (itbl AS C, ifld AS C )

            This function should take the table (itbl), look up each unique value in ifld (a user-defined field) and generate a CR-LF list of each occurence of the value of ifld and the number of times it occurs in the database.

            For example:

            Smith
            Able
            Wilson
            Smith
            Thomas
            Jones
            Smith
            Thomas


            should generate:
            Able 1
            Jones 1
            Smith 3
            Thomas 2
            Wilson 1



            Is there a simpler way to do this? It seems like this would be a common requirement.

            Thanks!

            Dave
            David A. Volgas, MD

            Comment


              #7
              Re: pass table name to a function

              as I work more of this function, i hit another snag...

              can arrays be accessed in a for ... next loop?

              for 1 = 1 to 10
              David A. Volgas, MD

              Comment


                #8
                Re: pass table name to a function

                as I work more of this function, i hit another snag...

                can arrays be accessed in a for ... next loop?

                for i = 1 to 10
                k = myarray[i]
                next i


                dave
                David A. Volgas, MD

                Comment


                  #9
                  Re: pass table name to a function

                  Originally posted by davidv43 View Post
                  can arrays be accessed in a for ... next loop?
                  Yes.

                  Maybe you want something like this?
                  Code:
                  tbl = table.open(itbl,FILE_RO_SHARED)
                  num = tbl.records_get()
                  
                  'other code here...
                      
                  Dim temparr[num] as P
                  for i = 1 to j
                      k = temparr[i].ifld
                      'do something with k (otherwise k keeps changing w. each iteration)
                  next i
                  Peter
                  AlphaBase Solutions, LLC

                  [email protected]
                  https://www.alphabasesolutions.com


                  Comment


                    #10
                    Re: pass table name to a function

                    that would seem to be the way to go, but I get an error "property not found" temparr[] not found.

                    Since the actual property name isn't known (it comes from the variable 'ifld'), I tried the syntax

                    k = temparr[i] as well as k = eval(temparr[i].ifld)

                    I don't find any reference in the help manual that arrays can be referenced by a variable (i), nor that properties can be referenced by a var.
                    David A. Volgas, MD

                    Comment


                      #11
                      Re: pass table name to a function

                      Peter,

                      do you have any other method to accomplish this?

                      My goal with this function is to call a function:
                      FUNCTION frequency AS F (itbl AS C, ifld AS C )

                      This function should take the table (itbl), look up each unique value in ifld (a user-defined field) and generate a CR-LF list of each occurence of the value of ifld and the number of times it occurs in the database.

                      For example:

                      Smith
                      Able
                      Wilson
                      Smith
                      Thomas
                      Jones
                      Smith
                      Thomas


                      should generate:
                      Able 1
                      Jones 1
                      Smith 3
                      Thomas 2
                      Wilson 1



                      Is there a simpler way to do this? It seems like this would be a common requirement.

                      Thanks!

                      Dave
                      David A. Volgas, MD

                      Comment


                        #12
                        Re: pass table name to a function

                        Make sure that you have dimmed the array as per my example.

                        k = temparr[i] doesn't do anything.

                        k = temparr[i].ifld does - assuming that you have prev. assigned a value to that variable.
                        Peter
                        AlphaBase Solutions, LLC

                        [email protected]
                        https://www.alphabasesolutions.com


                        Comment


                          #13
                          Re: pass table name to a function

                          here's the current working version... i think i did it your way

                          tbl = table.open(itbl,FILE_RO_SHARED)
                          num = tbl.records_get()
                          query.description = "temp"
                          query.filter = ".t."
                          query.order = ifld
                          query.options = "U"
                          qry = tbl.query_create()
                          num = qry.records_get()
                          dim temparr[num] as P
                          temparr.initialize_from_table(tbl,".t.",ifld)
                          j = temparr.first_empty()

                          for i = 1 to j-1
                          k = temparr[i].ifld
                          num = tablecount((itbl),ifld +" = " + s_quote(k))
                          next i

                          query.filter = ""
                          query.order = ""
                          query.options = ""
                          qry.close()
                          tbl.close()
                          David A. Volgas, MD

                          Comment


                            #14
                            Re: pass table name to a function

                            I forget the syntax, but i think you are initializing an array w. ALL the table records, but you are dimming it with a query value. You're also reusing "num" in seemingly diff. contexts - i would use diff. variable names. Again, both k & num will change value upon each iteration. Maybe you want to put in a crlf() list as you said above? - then:

                            k = k + crlf() + temparr[i].ifld
                            num = num + crlf() + tablecount((itbl),ifld +" = " + s_quote(k))

                            Make sure you dim k & num first...
                            Peter
                            AlphaBase Solutions, LLC

                            [email protected]
                            https://www.alphabasesolutions.com


                            Comment


                              #15
                              Re: pass table name to a function

                              Originally posted by davidv43 View Post
                              Peter,

                              do you have any other method to accomplish this?

                              My goal with this function is to call a function:
                              FUNCTION frequency AS F (itbl AS C, ifld AS C )

                              This function should take the table (itbl), look up each unique value in ifld (a user-defined field) and generate a CR-LF list of each occurence of the value of ifld and the number of times it occurs in the database.

                              For example:

                              Smith
                              Able
                              Wilson
                              Smith
                              Thomas
                              Jones
                              Smith
                              Thomas


                              should generate:
                              Able 1
                              Jones 1
                              Smith 3
                              Thomas 2
                              Wilson 1



                              Is there a simpler way to do this? It seems like this would be a common requirement.

                              Thanks!

                              Dave
                              Try this
                              Code:
                              Function GetCountsOfValues as C(iTbl as C,iFld as C)
                                  dim valuelist as C
                                  dim countlist as C
                                  valuelist = table.external_record_content_get(iTbl,iFld,iFld,"unique_key_value()")
                                  countlist = *for_each(valu,valu +" - " + tablecount(iTbl,iFld + "=" +quote(valu))  ,valuelist)
                                  GetCountsOfValues = countlist
                              End Function
                              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.

                              Comment

                              Working...
                              X