Writing to a text file

I'm using the following code below, which works fine, but is there someway I can automatically write to another file when the size of the initial one reaches 400KB and so on, so I could finish up with 10 400KB files rather than one 4MB file?

    If CommonDialog1.FileName <> "" Then
            Open Left(CommonDialog1.FileName, Len(CommonDialog1.FileName) - (Len(CommonDialog1.DefaultExt) + 1)) & Mid(CommonDialog1.DefaultExt, 2) For Append      As #1
          For ID = 1 To ProgressBar1.Max

            Print #1, Query
         Close #1
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

You could count the number of bytes in Query, and when it exceeds your limit, close the file, and write the rest
Guy Hengel [angelIII / a3]Billing EngineerCommented:
dim lngOffset as long
dim lngLen as long
dim lngPart as long
dim TrunkSize as long
dim intFile as integer

Trunksize = 400 * 1024
lngOffset = 1
lngPart = 1
lngLen = len(Query)

while lngOffset < lngLen
  intFile = freefile
  Open Left(CommonDialog1.FileName, Len(CommonDialog1.FileName) - (Len(CommonDialog1.DefaultExt) + 1)) & "." & lngPart & "." & Mid(CommonDialog1.DefaultExt, 2) For Append      As #intFile
  print #intFile, mid(Query, lngOffset, TrunkSize)
  close #intFile

  lngPart = lngPart + 1
  lngOffset = lngOffset + Trunksize  


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.