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

Create one to many set only using the last 4 records in the many Table

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

    Create one to many set only using the last 4 records in the many Table

    Is there a way to limit the number of records when building a one-to-many relationship in a set? I want to only include the Last for records of the child table using the native dbf.

    So if the parent is STUDENTS and the child table is NOTES, I want to only see the last x amount of records. I mainly want to do this for reporting purposes.

    #2
    Re: Create one to many set only using the last 4 records in the many Table

    Do you mean you would want to only see the last 4 notes for each parent record?

    I would approach this by writing a script which

    unmarked all records in notes
    ordered the notes table ascending by studentid, descending by recno()
    looped through the whole table, and for each group of studentid
    mark records 1 through 4 (if there are 4)

    The report filter (or the set filter for a reporting only set) would include marked().
    There can be only one.

    Comment


      #3
      Re: Create one to many set only using the last 4 records in the many Table

      stan
      i see there is an option to set filter for the child table and the alphapedia says that
      Optionally, enter an expression for the Child Table Filter. This expression gives you control over the manner in which records are linked. You can filter out child records based on a condition you set. One example is exclude child records with duplicate linking key values. Another example (if you built a set consisting of Customer as the parent and Transaction as the child) is to create a filter that selects only Transaction records that were entered within the last year.
      so it seems you can set a filter to filter out certain number of records.
      i am wondering if this can be done for the report source.
      (it is easier to ask you and get a result in a second than struggle with xbasic for a long time)
      thanks for reading

      gandhi

      version 11 3381 - 4096
      mysql backend
      http://www.alphawebprogramming.blogspot.com
      [email protected]
      Skype:[email protected]
      1 914 924 5171

      Comment


        #4
        Re: Create one to many set only using the last 4 records in the many Table

        Yes, you can set a filter for the child table records in the set definition as I indicated in my last sentence of my previous post.

        The usual expressions employing tablemax() or dbmax() don't seem feasible as they are meant to reference field values. Perhaps something could be constructed with BOTTOM_RECORDS() but that would not work in the report filter, only in the set definition.

        Will investigate.
        There can be only one.

        Comment


          #5
          Re: Create one to many set only using the last 4 records in the many Table

          but that would not work in the report filter, only in the set definition.
          i meant set definition not the report definition.
          https://www.screencast.com/t/mQpAH21aU
          thanks for reading

          gandhi

          version 11 3381 - 4096
          mysql backend
          http://www.alphawebprogramming.blogspot.com
          [email protected]
          Skype:[email protected]
          1 914 924 5171

          Comment


            #6
            Re: Create one to many set only using the last 4 records in the many Table

            Yes you are both correct, I wanted to build the set to filter the last x amount of records for the child. I saw the filter section that Gandhi had mentioned but was not sure how to construct such a filter. I figured building a set with the criteria I needed would be easier to build reports off of, rather than setting it in each report I built.

            Comment


              #7
              Re: Create one to many set only using the last 4 records in the many Table

              I am not aware of any filter expression that would limit the record selection to the last four child records per parent record either in the set definition or the report record selection.

              It is straightforward to base either expression on some date, numeric, or logical field value.

              Here's a sample script that opens a table named notes, orders descending on the id field and marks the last 4 records per id.

              Code:
              tbl = table.open("notes")
              tbl.unmark_range(".T.")
              tbl.order("id",".T.","D")
              cid = tbl.id
              while .not. tbl.fetch_eof()
              	while tbl.id = cid .and. (.not. tbl.fetch_eof())
              		for qx = 1 to 4
              			tbl.change_begin()
              			tbl.mark()
              			tbl.change_end()
              			tbl.fetch_next()
              			if tbl.id <> cid
              				exit for
              			end if
              		next qx
              		while tbl.id = cid .and. (.not. tbl.fetch_eof())
              			tbl.fetch_next()
              		end while
              	end while
              	cid = tbl.id
              end while
              My testing indicated that forcing the order by id and recno() descending was not necessary. If desired one could use the order expression

              tbl.order("id+padl(alltrim(str(recno())),19,"0")))",".T.","D")
              Last edited by Stan Mathews; 05-16-2017, 02:01 PM.
              There can be only one.

              Comment


                #8
                Re: Create one to many set only using the last 4 records in the many Table

                Originally posted by Stan Mathews View Post
                I am not aware of any filter expression that would limit the record selection to the last four child records per parent record either in the set definition or the report record selection.

                It is straightforward to base either expression on some date, numeric, or logical field value.

                Here's a sample script that opens a table named notes, orders descending on the id field and marks the last 4 records per id.

                Code:
                tbl = table.open("notes")
                tbl.unmark_range(".T.")
                tbl.order("id",".T.","D")
                cid = tbl.id
                while .not. tbl.fetch_eof()
                	while tbl.id = cid .and. (.not. tbl.fetch_eof())
                		for qx = 1 to 4
                			tbl.change_begin()
                			tbl.mark()
                			tbl.change_end()
                			tbl.fetch_next()
                			if tbl.id <> cid
                				exit for
                			end if
                		next qx
                		while tbl.id = cid .and. (.not. tbl.fetch_eof())
                			tbl.fetch_next()
                		end while
                	end while
                	cid = tbl.id
                end while
                My testing indicated that forcing the order by id and recno() descending was not necessary. If desired one could use the order expression

                tbl.order("id+padl(alltrim(str(recno())),19,"0")))",".T.","D")
                Thank you for the post Stan. How can I incorporate this into printing my report? What we do now is, from a form, I have a button that will provide a student overview along with the notes for the student. Some students have 10+ note records which is why I wanted to limit the notes to the most recent 4 or less records.

                Comment


                  #9
                  Re: Create one to many set only using the last 4 records in the many Table

                  You would run the code either adding it to the current button code as an inline xbasic element (first action) or possibly a separate button entirely followed by the report button.

                  The report filter could be as simple as (try this first)

                  marked("notes")

                  but you might have to use flattenquery() to get the desired results.

                  Rereading your post it may be that you are printing one student record at a time. If this is the case you would add to the current report filter

                  .and. marked("notes")
                  Last edited by Stan Mathews; 05-17-2017, 12:44 PM.
                  There can be only one.

                  Comment


                    #10
                    Re: Create one to many set only using the last 4 records in the many Table

                    I just did a bit of experimenting and I believe you will get the best results by placing a subreport in the detail section of the report. The subreport would only contain the desired fields from the notes table. The filter for the subreport would be

                    marked()

                    The subreport can be sized the same height as the normal detail section and it will grow to the necessary height to show the notes records automatically.
                    There can be only one.

                    Comment


                      #11
                      Re: Create one to many set only using the last 4 records in the many Table

                      Let me give this a go, the only thing I am wondering is, how am I going to make this work if I have multiple users using the same system? I guess, what are the odds of two or more users pressing the print button at the exact same time, right?

                      Comment


                        #12
                        Re: Create one to many set only using the last 4 records in the many Table

                        This is the code I ran, it took such a long time to run too... But did not produce the result I was looking for. I added in the report a sub-report that was filtered based on marked records. I got an error message when the code completed, but I failed to write it down and am afraid to re-run it as my system was experiencing slowing. I will attempt this again when there are no users. SideNote: My notes table has an excess of 200k records, I should add some indexes to help with the speed issues.

                        Code:
                        'Get 'Value' property of 'Ssn17' in Form 'DO_NOT_OPEN' .
                        DIM SHARED vSSN AS c
                        vSSN = parentform:Ssn17.value
                        
                        'Mark Last four Notes but first unmark all Notes
                        tbl = table.open("notes")
                        tbl.unmark_range(".T.")
                        tbl.order("pk_notes_id",".T.","D")
                        cid = vSSN
                        while .not. tbl.fetch_eof()
                        	while tbl.ssn = cid .and. (.not. tbl.fetch_eof())
                        		for qx = 1 to 4
                        			tbl.change_begin()
                        			tbl.mark()
                        			tbl.change_end()
                        			tbl.fetch_next()
                        			if tbl.id <> cid
                        				exit for
                        			end if
                        		next qx
                        		while tbl.id = cid .and. (.not. tbl.fetch_eof())
                        			tbl.fetch_next()
                        		end while
                        	end while
                        	cid = tbl.id
                        end while
                        
                        
                        'Print a report, label, letter, form or browse layout. You can specify which records to print.
                        
                        'Check to see if you are running the script from within a Form
                        dim flag_error as l
                        flag_error = .f.
                        if is_object(topparent.this) then 
                        	p = topparent.this
                        	
                        	if eval_valid("P:Ssn17.value") then 
                        		DELETE Parameter1 
                        		'Set the Parameter variable to the object's .value property
                        		Parameter1 = eval("P:Ssn17.value")
                        	else
                        		flag_error = .t.
                        	end if 
                        else
                        	flag_error = .t.
                        end if 
                        
                        if flag_error then 
                        	ui_msg_box("Error","Ssn17 does not exist. This script cannot be run in this context.",UI_STOP_SYMBOL)
                        	end 
                        end if 
                        
                        filter = "ssn = [varC->parameter1]"
                        query.filter = replace_parameters(filter,local_variables())
                        query.order = ""
                        
                        :Report.Print("1Oview-TEST",query.filter,query.order)
                        Last edited by Gustavo1478; 05-17-2017, 05:16 PM.

                        Comment


                          #13
                          Re: Create one to many set only using the last 4 records in the many Table

                          My table had many fewer records of course.

                          The script can be speeded up with

                          Code:
                          'Mark Last four Notes but first unmark all Notes
                          tbl = table.open("notes")
                          tbl.unmark_range(".T.")
                          tbl.order("pk_notes_id",".T.","D")
                          cid = vSSN
                          [COLOR="#FF0000"]tbl.batch_begin()[/COLOR]
                          while .not. tbl.fetch_eof()
                          	while tbl.ssn = cid .and. (.not. tbl.fetch_eof())
                          		for qx = 1 to 4
                          			tbl.change_begin()
                          			tbl.mark()
                          			tbl.change_end()
                          			tbl.fetch_next()
                          			if tbl.id <> cid
                          				exit for
                          			end if
                          		next qx
                          		while tbl.id = cid .and. (.not. tbl.fetch_eof())
                          			tbl.fetch_next()
                          		end while
                          	end while
                          	cid = tbl.id
                          end while
                          [COLOR="#FF0000"]tbl.batch_end()[/COLOR]
                          tbl.close() '[COLOR="#0000FF"]forgot to include this[/COLOR]
                          There can be only one.

                          Comment


                            #14
                            Re: Create one to many set only using the last 4 records in the many Table

                            i thought bottom_records(4) at the set definition will work.
                            a short experiment seems to work i will test for real, later.
                            thanks for reading

                            gandhi

                            version 11 3381 - 4096
                            mysql backend
                            http://www.alphawebprogramming.blogspot.com
                            [email protected]
                            Skype:[email protected]
                            1 914 924 5171

                            Comment


                              #15
                              Re: Create one to many set only using the last 4 records in the many Table

                              My original "marking" approach is not working this morning as I thought it was yesterday. I may not have had enough records or constructed something correctly.

                              I am able to get what I think is the correct result with a modified version.

                              I duplicated my notes table with, of course, a new name - no records. Instead of marking records in the notes table I changed the script to copy the last four records for each id from notes to the new table (zapping it each time first). I created a new set substituting the new table for notes and created a new report based on the new set. As only the desired last four records for each student are in the new table no filtering is needed other than that of student selection.

                              Code:
                              tbl = table.open("anotes")'my original notes table
                              tbl.unmark_range(".T.")
                              tbl.order("id",".T.","D")
                              cid = tbl.id
                              b_tbl = table.open("abnotes")'my operating table for the new set
                              b_tbl.zap(.T.)
                              b_tbl.close()
                              tbl.batch_begin()
                              while .not. tbl.fetch_eof()
                              	while tbl.id = cid .and. (.not. tbl.fetch_eof())
                              		for qx = 1 to 4
                              			tbl.Copy_Record_To("abnotes")
                              			tbl.fetch_next()
                              			if tbl.id <> cid
                              				exit for
                              			end if
                              		next qx
                              		while tbl.id = cid .and. (.not. tbl.fetch_eof())
                              			tbl.fetch_next()
                              		end while
                              	end while
                              	cid = tbl.id
                              end while
                              tbl.batch_end()
                              tbl.close()
                              There can be only one.

                              Comment

                              Working...
                              X