Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9042591
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T10:32:04+00:00 2026-06-16T10:32:04+00:00

I have a long sql query that I am attempting to put into VBA

  • 0

I have a long sql query that I am attempting to put into VBA for Excel. VBA for Excel has a limit to the amount of text that can go on a line and it seems to be about 1000 chars. What I want to do is copy the query to a text file and run it through a perl script and output to the text file with it formatted the way I need it for VBA.

I need Perl to count chars to 1000 then write (” & _) then a line break then (“) then repeat the process till the end of the file. Spaces or type of char do not matter. Any help is greatly appreciated. I will check back frequently to see if anyone needs more information. THANKS!!!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T10:32:05+00:00Added an answer on June 16, 2026 at 10:32 am

    If you have the text already in a string, one way to do this would be:

    $string =~ s/(.{1000})/$1" & _\n"/sg;
    print '"', $string, '"';
    

    or, perhaps better:

    my @chunks = unpack '(A1000)*', $string;
    s/\"/""/g, s/\n/" & vbCrLf & _\n"/g for @chunks;  # escape special characters
    print '"', join(qq(" & _\n"), @chunks), '"';
    

    If you’re reading the input from a file, it’s also possible to do this without reading all the input into memory, by setting the input record separator:

    {
        local $/ = \1000;  # read input in chunks of 1000 chars
        print '"';
        while ( <> ) {
            s/\"/""/g; s/\n/" & vbCrLf & _\n"/g;  # escape special characters
            print $_, qq(" & _\n");
        }
        print '"';
    }
    

    (Some of these methods can sometimes leave a pointless "" on the last line — in fact, the last method will always do that — but I assume that shouldn’t be a problem.)

    Finally, note that neither of these methods will do anything to escape any special characters (like double quotes) that might appear in the input. If your input might contain such characters, you’ll need to deal with them separately. I’ve marked the points where you could insert code to do that, if you need to.

    Edit: I did some Googling, and it looks like the main characters the need to be escaped in double quoted VBA strings are double quotes (which need to be doubled) and newlines (which have no simple encoding, and need to be handled with a kluge like " & vbCrLf & "). I’ve edited the code above to implement such escaping.
    I suppose, if one wanted to be extra sure, one could also escape all non-printable and non-ASCII characters with something like:

    s/([^\n -~])/sprintf '" & Chr(%d) & "', ord $1/eg
    

    (Ps. The backslash in s/\"/""/g is there only to avoid confusing SO’s syntax highlighter.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a long SQL text that I want to assign to a query
I have a long-running SQL Server 2005 query that I have been hoping to
Scenario I'm using SQL Server 2005 I have a T-Sql query that has multiple
I have two long type columns that I want to concat during sql query.
after a very long sql query a have a result that is in this
I have a pretty long SQL query that I use to, for all of
I have a SQL query that has me stumped. Basically, I have a Recipes
I have a long running SQL query inside a page that I've sped up
I have a long sql query that calculates a few things about payments and
I have a SQL query that takes a very long time to run on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.