[AccessD] storing last item on the page

A.D.TEJPAL adtp at airtelbroadband.in
Sun Nov 11 23:57:58 CST 2007


    While using collection's Add method, second argument is optional and gets stored as the key for identifying the particular element. As a parallel, it is like the primary key value for a record.

    Let us presume that txtFooterLast displays "LV-1", "LV-2", "LV-3" and "LV-4" on page number 1 to 4 respectively. Your Add statement reads:

    col.Add Me.txtFooterLast.Value, CStr(Me.Page)

    This means that "1" to "4" get stored as the key string values for elements "LV-1" to "LV-4" respectively of the collection. In this state, the index position of the four elements is 1 to 4. The results fetched by the following two statements for page 3 will be identical:

    (a) Syntax: col(CStr(Me.Page)) - using key:  
          col("3") fetches "LV-3"

    (b) Syntax: col(Me.Page) - using index position:  
          col(3) also fetches "LV-3"

    If, for some reason (although not likely in this particular case), second element of collection were to be removed, and then re-added as "LLVV-2" with key "2" for page 2, the existing 3rd & 4th elements will move up to 2nd & 3rd positions respectively, while the newly added element "LLVV-2" will occupy the last i.e. fourth position (unless you take special care to specify its position in Add statement by making use of optional third & fourth arguments). 

    In this state, syntax as per (a) above, using key string will still fetch the correct value "LV-3" for page 3. However, with the other syntax as per (b) above, using index position, incorrect value of "LV-4" will get fetched for page 3. It is therefore considered a preferable practice to use key values (if available) for retrieving information from a collection. It is observed that you have already taken the trouble of embedding the key strings during Add process. That being so, better use it for retrieval (i.e. col(CStr(Me.Page))  in preference to  col(Me.Page)). Reverting to our parallel with a recordset, it is like using the primary key value rather than record number while locating a record.

    As mentioned earlier, your current exercise does not run the risk of collection once built, getting disturbed. As such, if you are particularly keen to keep everything absolutely simple, you can avoid using the second argument in Add method. That statement would become:

    col.Add Me.txtFooterLast.Value

    And then you can continue to use the following statement for retrieval:

    Me.txtHeaderLast = col(Me.Page)

A.D.Tejpal
------------

  ----- Original Message ----- 
  From: Susan Harkins 
  To: Access Developers discussion and problem solving 
  Sent: Saturday, November 10, 2007 20:03
  Subject: Re: [AccessD] storing last item on the page

  >    In your code, second statement in page footer's format event is also 
  > redundant. The real assignment for txtHeaderLast is taking place in page 
  > header's print event. First two lines of existing code block can be 
  > removed. The only portion required in page footer's format event is the 
  > block of three lines meant to build up the collection.

  ======Oh, you're right. I think that's just a residual from my original 
  attempt and I did a bad job of cleaning up. Thanks for pointing that out.
  >
  >    In page header's print event, while retrieving information from 
  > collection object, you have used number argument, which returns values as 
  > per index position in collection. This method does not take advantage of 
  > key strings that you have embedded (via CStr(Me.Page) as the second 
  > argument) while adding to the collection. Though in the present case, your 
  > results are not affected (being a simple situation), it is considered 
  > preferable in the interest of greater reliability to retrieve values using 
  > key (where available) instead of number index (specially if the collection 
  > is likely to get disturbed for some reason). Second statement in page 
  > header's print event would then become:
  >
  >    Me.txtHeaderLast = col(CStr(Me.Page))
  >
  >    On the other hand, if you wish to continue using number index, it is 
  > not necessary to provide the second argument (for key string) in 
  > collection's Add method. The existing statement in page footer's format 
  > event would then become:
  >
  >    col.Add Me.txtFooterLast.Value

  =======I don't understand what you're saying. I'm really sorry -- let me ask 
  a few questions, so you don't have to repeat yourself. I do see the 
  inconsistency now that you point it out -- I use CStr(Me.Page) to store that 
  value as a string, as required by the Collection object, but then I used 
  Me.Page to retrieve members -- so I think what you're saying is that the 
  code isn't even using the Me.Page value to retrieve values? That's where I 
  get lost. If not the Me.Page value, then what? Thanks for your help.

  Susan H. 


More information about the AccessD mailing list