Writing to a text file

Posted on 2006-04-09
Last Modified: 2011-09-20
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
Question by:PeterErhard
    LVL 14

    Expert Comment

    You could count the number of bytes in Query, and when it exceeds your limit, close the file, and write the rest
    LVL 142

    Accepted Solution

    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  


    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
    Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
    As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
    Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

    729 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

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now