Jump to content

How do you loop through a ExternalDataFileEx table?


cschendel

Recommended Posts

I would like to get a set of records back from an external table using the ExternalDataFileEx object. In all the examples I've seen, people are only pulling one record/row of data back. I would like to get all the rows that match my criteria.

 

Here I am able to get back the first match of the Zip Code passed by the field and return the StoreName and StoreCity -- but it repeats the same record the same amount as the myStores.recordCount. I image this is because their is no record.Next type command like in other recordset languages. So, I do I simulate that behavior?

 

Sample Code

 

var myStores = new ExternalDataFileEx("C:\\data\\Stores.txt", ",")

var strZip = Field("CustZip")

var strOutput = ""

 

for (var i = 1; i <= myStores.recordCount; i++)

{

strOutput += myStores.GetFieldValue(LocalShops.FindRecord("ZipID", strZip), "StoreName") + Chr(10)

strOutput += myStores.GetFieldValue(LocalShops.FindRecord("ZipID", strZip), "StoreCity") + Chr(10)

}

 

return strOutput

 

 

The Stores.txt file looks like;

 

ZipID, StoreName, StoreCity

55416, ABC Corporation, Edina

55416, EFG Inc., Saint Louis Park

55417, XYZ Companies, Minneapolis

55418, QT Corp., St. Paul

 

Any assistance would be great!

 

Thanks!

Chris

Link to comment
Share on other sites

CORRECTION: Sample Code

 

var myStores = new ExternalDataFileEx("C:\\data\\Stores.txt", ",")

var strZip = Field("CustZip")

var strOutput = ""

 

for (var i = 1; i <= myStores.recordCount; i++)

{

strOutput += myStores.GetFieldValue(myStores.FindRecord("ZipID", strZip), "StoreName") + Chr(10)

strOutput += myStores.GetFieldValue(myStores.FindRecord("ZipID", strZip), "StoreCity") + Chr(10)

}

 

return strOutput

Link to comment
Share on other sites

You're on the right track, but the ExternalDataFileEx.FindRecord function only finds the first record which matches the specified field value. If you want to find multiple records which match a certain criteria, you simply need to iterate through all the records, which you're already doing, and then test for the match in each. Try this:

for (var i = 1; i <= myStores.recordCount; i++)
{
 if (myStores.GetFieldValue(i, "ZipID") == strZip)
 {
   strOutput += myStores.GetFieldValue(i, "StoreName") + Chr(10) + 
   myStores.GetFieldValue(i, "StoreCity") + Chr(10);
 }
}

 

A search of the forum for ExternalDataFileEx turns up many similar examples, such as this:

http://forums.printable.com/showthread.php?t=388

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...