+ Reply to Thread
Results 1 to 2 of 2

How to break long sql vba statement?

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    02-26-2013
    Location
    Philippines
    MS-Off Ver
    Excel 2007
    Posts
    367

    How to break long sql vba statement?

    I have the below code and I need to break it into multiple lines

    
    SqlQuery = "SELECT COUNT(*) AS Report_ID,q1.Report_Name,q1.Report_Owner,q1.Function,q1.Region,q1.Comment,q1.Cut_Off,q1.Reminder_Time,q1.Report_Day FROM (SELECT Report_ID,Report_Name,Report_Owner,Function,Region,Comment,Cut_Off,Reminder_Time,Report_Day FROM tbl_Monday UNION ALL SELECT Report_ID,Report_Name,Report_Owner,Function,Region,Comment,Cut_Off,Reminder_Time,Report_Day FROM tbl_Tuesday UNION ALL SELECT Report_ID,Report_Name,Report_Owner,Function,Region,Comment,Cut_Off,Reminder_Time,Report_Day FROM tbl_Wednesday) q1 INNER JOIN(SELECT Report_ID,Report_Name,Report_Owner,Function,Region,Comment,Cut_Off,Reminder_Time,Report_Day FROM tbl_Monday UNION ALL SELECT" & _
                "Report_ID,Report_Name,Report_Owner,Function,Region,Comment,Cut_Off,Reminder_Time,Report_Day FROM tbl_Tuesday UNION ALL SELECT Report_ID,Report_Name,Report_Owner,Function,Region,Comment,Cut_Off,Reminder_Time,Report_Day FROM tbl_Wednesday)" & _
                "q2 on CStr(q1.Report_ID) + q1.Report_Name + q1.Report_Owner + q1.Function + q1.Region + q1.Comment + q1.Cut_Off + q1.Reminder_Time + q1.Reminder_Day" & _
                ">= CStr(q2.Report_ID) + q2.Report_Name + q2.Report_Owner GROUP BY q1.Report_ID,q1.Report_Name,q1.Report_Owner"

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: How to break long sql vba statement?

    Perhaps something like this.
        SqlQuery = "SELECT COUNT(*) AS Report_ID,q1.Report_Name,q1.Report_Owner,q1.Function,q1.Region,q1.Comment,q1.Cut_Off,q1.Reminder_Time,q1.Report_Day "
        SqlQuery = SqlQuery & " FROM (SELECT Report_ID,Report_Name,Report_Owner,Function,Region,Comment,Cut_Off,Reminder_Time,Report_Day "
        SqlQuery = SqlQuery & " FROM tbl_Monday "
        SqlQuery = SqlQuery & " UNION ALL SELECT Report_ID,Report_Name,Report_Owner,Function,Region,Comment,Cut_Off,Reminder_Time,Report_Day "
        SqlQuery = SqlQuery & " FROM tbl_Tuesday "
        SqlQuery = SqlQuery & " UNION ALL SELECT Report_ID,Report_Name,Report_Owner,Function,Region,Comment,Cut_Off,Reminder_Time,Report_Day "
        SqlQuery = SqlQuery & " FROM tbl_Wednesday) q1 "
        SqlQuery = SqlQuery & " INNER JOIN(SELECT Report_ID,Report_Name,Report_Owner,Function,Region,Comment,Cut_Off,Reminder_Time,Report_Day "
        SqlQuery = SqlQuery & " FROM tbl_Monday "
        SqlQuery = SqlQuery & " UNION ALL SELECT Report_ID,Report_Name,Report_Owner,Function,Region,Comment,Cut_Off,Reminder_Time,Report_Day "
        SqlQuery = SqlQuery & " FROM tbl_Tuesday "
        SqlQuery = SqlQuery & " UNION ALL SELECT Report_ID,Report_Name,Report_Owner,Function,Region,Comment,Cut_Off,Reminder_Time,Report_Day "
        SqlQuery = SqlQuery & " FROM tbl_Wednesday) q2 "
        SqlQuery = SqlQuery & " ON CStr(q1.Report_ID) + q1.Report_Name + q1.Report_Owner + q1.Function + q1.Region + q1.Comment + q1.Cut_Off + q1.Reminder_Time + q1.Reminder_Day"
        SqlQuery = SqlQuery & ">= CStr(q2.Report_ID) + q2.Report_Name + q2.Report_Owner GROUP BY q1.Report_ID,q1.Report_Name,q1.Report_Owner"
    If posting code please use code tags, see here.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Break long range into 2 code lines
    By ker9 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 01-13-2011, 01:50 PM
  2. Break apart in long excel spreed sheet
    By Lillian in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-30-2005, 12:40 PM
  3. [SOLVED] How to split too long text in one row to break in next row.
    By KrunoCro in forum Excel General
    Replies: 2
    Last Post: 09-14-2005, 09:05 AM
  4. Break a long row of code
    By Otto Moehrbach in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-10-2005, 09:05 PM
  5. [SOLVED] How do i break really long bars to show short ones in Excel?
    By nkchu83 in forum Excel Charting & Pivots
    Replies: 2
    Last Post: 05-26-2005, 05:15 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1