Sorting the Form collection

Results 1 to 3 of 3

Thread: Sorting the Form collection

  1. #1
    Mark Hammond Guest

    Default Sorting the Form collection

    I am gathering data from a from which is then relayed via email to our client. The form info must be in a certain order on the email.<BR><BR>The REQUEST.FORM collection seems to depend on what order the fields were filled out in, i believe. How can i force the fields into a specific order?<BR><BR>many thanks

  2. #2
    Join Date
    Dec 1969

    Default RE: Sorting the Form collection

    When you do <BR>&nbsp;&nbsp;&nbsp;&nbsp;Request.Form<BR>you are accessing a VBScript collection. And a "collection" is ACTUALLY essentially the same thing as a Scripting.Dictionary". That is, it is a hash table keyed by the names of the items in the Form.<BR><BR>So when you do <BR>&nbsp;&nbsp;&nbsp;&nbsp;For each item in Request.Form<BR>you are asking to traverse the KEYS of the collection, which means you<BR>will get the items in whatever order the hashing algorithm stored<BR>them in.<BR><BR>So the only way to predict the order is to know<BR>what hashing algorithm MS uses for the given<BR>collection. But even knowing that wouldn&#039t be<BR>of any real use.<BR><BR>*ANYWAY*, it turns out there *is* a solution. Even<BR>this solution breaks when you get too many <BR>fields in the form (I forget what "too many" is <BR>except that it was some power of two...I think<BR>256, but maybe as low as 64 or as high as 1024).<BR>But for most forms it works fine:<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;For ix = 1 to Request.Form.Count<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;&nbsp;&nbsp;fieldName = Request.Form.Key(i)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nb sp;&nbsp;&nbsp;&nbsp;fieldValue = Request.Form.Item(i)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;...<BR>&nbsp;&nbsp;&nbsp;&nb sp;Next<BR><BR>Haven&#039t tested it in all situations, but it seems to work <BR>just fine where I have used it.<BR><BR>***********<BR><BR>If you really need the fields of the form in a particular order, though, why not just *ask* for them in that order:<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;fldNames = Array("field7","field2","field1","field12")<BR>&nb sp;&nbsp;&nbsp;&nbsp;For fnum = 0 To UBound(fldNames)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;fieldName = fldNames(fnum)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;fieldValue = Request.Form(fieldName)<BR>&nbsp;&nbsp;&nbsp;&nbsp ;&nbsp;&nbsp;&nbsp;&nbsp;...<BR>&nbsp;&nbsp;&nbsp; &nbsp;Next<BR><BR>

  3. #3
    Mark hammond Guest

    Default RE: Sorting the Form collection

    Thanks Bill, an excellent answer.<BR><BR>After i had posted the message, I came up with the idea of a dictionary &#039sorting&#039 method, using rather a complicated algorithm, but your response is much shorter and slicker.<BR><BR>regards<BR>Mark

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts