English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I use length of file divided by record length to find the number of records, but although the record length shows correctly, the number of records is one more than it should be. Is that because LOF is incorrect? How can you tell? If you put a break point on it and let the cursor hover over that line nothing is shown as a value of LOF. The .dat file seems to be ok.

Open App.Path & "\Customers.dat" For Random As #1 Len = Len(Customer(Index))
NumberOfRecords = LOF(1) / Len(Customer(Index))
For Count = 1 to NumberOfRecords
Get #1, Count, Customer(Index)
Index = Index + 1
Next Count
Close #1
For i = 0 to Index
lstCustomerList.AddItem (Customer(i).CustomerID)
Next i

2006-11-15 22:41:29 · 1 answers · asked by Older&Wiser 5 in Computers & Internet Programming & Design

Thanks Rawlyn, but I had tried that whilst waiting for an answer. Instead of defining a MaxRecords variable I just used "For i = 0 to Index - 1" - but it doesn't help. Index was initialsed to zero before the open statement. You're right Index should finish being equal to the number of records, but it doesn't because NumberOfRecords is one too many and the loop goes through "1 to NumberOfRecords" times. So it goes through too many times, making index one higher than the number of records. And then index gets incremented, which now makes it 2 higher than the number of records instead of the expected 1 higher.

Any further help MUCH appreciated.

2006-11-16 00:41:31 · update #1

Problem solved - Rawlyn emailed me some ideas which lead to the solution. Thanks.

2006-11-16 02:22:45 · update #2

1 answers

Firstly, Index is not initialized, so its value is uncertain at the beginning of the "For Count = 1 to NumberOfRecord" loop. Assuming it to be zero at the start of the loop, it would equal NumberOfRecords by the end of the loop (its gets incremented by one for each NumberOfRecords).
You then start a loop "For i = 0 to Index" - this loop is one element longer than the previous loop because it is zero-indexed, unlike all your previous loops. This may be where the problem lies.
In order to avoid confusion, I would define an extra variable, MaxRecord, and make it equal NumberOfRecords-1, then use "For i = 0 to MaxRecord" instead.

Rawlyn.

2006-11-15 23:05:46 · answer #1 · answered by Anonymous · 0 0

fedest.com, questions and answers