I need a do while loop to count

To count the number of records in a .dat(txt) file, then display in a lblXXX.Caption.

How can I do it to where I don't know how many records there are but at the same time end when it has counted them all, displaying the number of records (lines) i.e. end of file.
LVL 1
GlobalFaxAsked:
Who is Participating?
 
jlbonanoConnect With a Mentor Commented:
Try this.  You don't need to do the input for the count but if you need the data then you can.

Dim i       As Integer
Dim iFile   As Integer

i = 0
iFile = FreeFile

Open strPingData For Input As iFile
Do While Not EOF(iFile)
    Input #iFile, strLine
    '-- Do whatever with strLine
    i = i + 1
Loop

Me.lblALabel.Caption = i

Close #iFile
0
 
SweatCommented:
GlobalFax,

You didn't mention what type of recordset you were using. How about this:

' Declare it...
Dim rs as RecordSet
dim vCount

' Populate the recordset using SQL, ADO, DAO it doesn't matter

Do
   vCount = vCount + 1
   lblXXX.Caption = vCount

   rs.MoveNext
Loop Until rs.EOF = True

But.  If you were using something structured like ADO or DAO you have the count of records available without needing to count.  rs.RecordCount is a property that holds the number of rows in the record set. (Be careful with DAO, you need to move to the end of the rs before that property is accurate).

If you are just reading a text file and counting lines, you would open the text file: Open "c:\me.txt" For Input As #1.

Then you'd read each line till the file is done:

Dim sLine
Dim vCount

Do
   vCount = vCount + 1
   lblXXX.Caption = vCount

   Line Input #1, sLine
Loop Until EOF(1) = True

Does this help?

Sweat

0
 
appariCommented:
try something like this

dim FNo as integer, LineCnt as integer
FNo = freefile

open datfilename.dat for input as #FNo
LineCnt=0
while not EOF(FNo)
  line input #FNo, strData
  LineCnt = LineCnt+1
wend

close #FNo

lblXXX.Caption=LineCnt
0
 
appariCommented:
jlbonano: almost same as yours:), i was late in posting as i had to attend a phone
0
 
GlobalFaxAuthor Commented:
Thanks, I did the data abd replaced the strPingData with the file path.

0
All Courses

From novice to tech pro — start learning today.