Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I set the secondary index of a Table Programmatically??

Status
Not open for further replies.

SercoSteve

Programmer
Sep 25, 2002
44
GB
Firstly is it possible to set the secondary index of a Table prgrammatically when the primary index is linked to another table in the data model?

I am using the code shown in an attempt to set the secondary index of my all_part DB to "ALT" which is defined. Then update my PARTS Table Frame to reflect the changes to all_part.

method pushButton(var eventInfo Event)

var
custTbl Table
tc TCursor
endVar

custTbl.attach(":ITEMSLIST:all_part.db")
if isTable(custTbl) then
if custTbl.setIndex("ALT") then
if tc.open(custTbl) then
PARTS.resync(tc)
tc.close()
endIf
endIf
endIf

endMethod

I only learnt about Secondary indexes this morning so I am kind of shooting in the dark, any suggestions would be greatly appreciated.

Steve
 
SercoSteve,

It is, though you have to unlink the detail table, set the index, and then relink the detail. this means that the alternate secondary must have the linking field(s) as the first one(s) in the secondary index.

Following is a somewhat extensive example based on the sample files provided with Paradox. Be sure to review the notes following the code:

Code:
method pushButton(var eventInfo Event)
; -----------------------------------------------
; Lets the user change the sorting of the orders
; by choosing the appropriate index from a menu.
; -----------------------------------------------
var
   tcOrders  tCursor
   strIndex  String
   strInUse  String
   dstrMenu  DynArray[] String
   astrKeys  Array[] String
   pmSortBy  PopupMenu
endVar

   ; Step 1: Set up
   tcOrders.attach( Ords )
   tcOrders.getIndexName( strInUse )

   astrKeys.addLast( "Customer No" )  ; Define master fields

   dstrMenu[ &quot;<Default>&quot; ]         = &quot;&quot;
   dstrMenu[ &quot;Date (Ascending)&quot;]   = &quot;CustDateAsc&quot;
   dstrMenu[ &quot;Date (Descending)&quot; ] = &quot;CustDateDesc&quot;
   dstrMenu[ &quot;Largest Orders&quot; ]    = &quot;CustTotalInv&quot;

   pmSortBy.AddStaticText( &quot;Available Sorts&quot; )
   pmSortBy.AddSeparator()
   forEach strIndex in dstrMenu
      if strInUse = dstrMenu[ strIndex ] then
         pmSortBy.AddText( strIndex, menuChecked )
      else
         pmSortBy.AddText( strIndex )
      endIf
   endForEach

   try
      strIndex = dstrMenu[ pmSortBy.show() ]
   onFail
      beep()
      Message( &quot;Sort change cancelled..&quot; )
      tcOrders.close()
   endTry


   ; Step 2: Do-it!
   if strIndex = strInUse then
      beep()
      Message( &quot;Sort change cancelled...&quot; )
   else

      tcOrders.switchIndex( strIndex )
      dmUnlink( &quot;cust&quot;, &quot;ords&quot; )
      Ords.resync( tcOrders )
      dmLinktoIndex( &quot;cust&quot;, astrKeys, &quot;ords&quot;, strIndex )

   endif

   ; Step 3: Clean-up
   if tcOrders.isAssigned() then
      tcOrders.close()
   endIf

endMethod

Notes:

1. This example assumes that Table Aliases have been used to rename the tables in the data model. This is always a good idea. For more information, please search Paradox's Help file for Table Aliases. In this example, cust refers to the Customer.db and Ords is the alias for Orders.db.

2. When the form is opened, Ords is linked to Cust using the Customer No field, e.g the primary key. Note that the dmUnlink will fail if the link is accidentally lost.

3. You'll note that I've been rather paranoid about making sure the tCursor is closed. This is always a good idea, for certain versions of Paradox allow to you leave tCursors in memory, even though they are out of scope.

4. Only minimal error checking has been provided; also, the example hasn't been heavily tested, so some tweaks may be needed.

Hope this helps...

-- Lance
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top