Link to home
Start Free TrialLog in
Avatar of Jim Horn
Jim HornFlag for United States of America

asked on

Looking for a damn-handy SQL parser in VBA

Does anyone have any code from a prior project that edits SQL statements?  
------
I have a graph that's table-driven, where one of the fields is the SQL string.  An example is...

SELECT tbl_time.txt_time_month_year AS [Month], tbl_indicators_data.sng_idata_value_1 AS [ALCOA]
FROM tbl_time INNER JOIN tbl_indicators_data ON tbl_time.lng_id = tbl_indicators_data.lng_month_id
WHERE tbl_indicators_data.lng_indicator_id = 32
ORDER BY tbl_time.lng_month_ID ASC;

The above SQL displays a graph with all values.  

I have a business need to allow the user to edit the above to allow FROM and TO dates, like (note the WHERE-BETWEEN CLAUSE)...

SELECT tbl_time.txt_time_month_year AS [Month], tbl_indicators_data.sng_idata_value_1 AS [ALCOA]
FROM tbl_time INNER JOIN tbl_indicators_data ON tbl_time.lng_id = tbl_indicators_data.lng_month_id
WHERE tbl_indicators_data.lng_indicator_id = 32 AND tbl_time.lng_month_ID BETWEEN 200201 AND 200312
ORDER BY tbl_time.lng_month_ID ASC;

I can hack the big components of SQL okay, but getting the 'AND tbl_time.lng_month_ID BETWEEN 200201 AND 200312' clause is proving to be a pain.

Points given generously for anything I can build on.  

TIA -Jim
ASKER CERTIFIED SOLUTION
Avatar of shanesuebsahakarn
shanesuebsahakarn
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Jim Horn

ASKER

(1)  I have my dates formatted as longs in yyyymm format, since this is accounting summary stuff and everything is a month.
(2)  In SQL code I'm reading the data, populating FROM and TO combobox.rowsources, and displaying the first and last months as the values.

Pretty damn slick if I do say so myself.

My problem is, every time I alter the SQL I need to read the existing SQL, determine if there's an existing BETWEEN clause (somewhat tough), if so wipe it out, and write a replacement (fairly easy).  

Perhaps I'll include the BETWEEN clause in every SQL statement, even if it's redundant, just to make user changes like this easier.

Resolved it by re-writing the entire WHERE clause every time, instead of trying to hack multiple WHERE critera.  

Wow.  <sniff>  You were the only one that cared...
You should have asked an easier question, like "How do I make my form go beep"...:-)

Still not entirely sure why you're dynamically change the WHERE clause though - there must be an easier way?
Don't tempt me... if there's a mad push for points, I can ask all sorts of silly questions...