I need a do while loop to count

Posted on 2003-03-12
Medium Priority
Last Modified: 2010-05-01
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.
Question by:GlobalFax
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Accepted Solution

jlbonano earned 800 total points
ID: 8125106
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

Me.lblALabel.Caption = i

Close #iFile

Expert Comment

ID: 8125139

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

   vCount = vCount + 1
   lblXXX.Caption = vCount

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

   vCount = vCount + 1
   lblXXX.Caption = vCount

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

Does this help?


LVL 39

Expert Comment

ID: 8125148
try something like this

dim FNo as integer, LineCnt as integer
FNo = freefile

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

close #FNo

LVL 39

Expert Comment

ID: 8125159
jlbonano: almost same as yours:), i was late in posting as i had to attend a phone

Author Comment

ID: 8125165
Thanks, I did the data abd replaced the strPingData with the file path.


Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question