473,398 Members | 2,393 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Query SQL code

48
Is there a way (function) to extract dynamically the SQL-code out of a query ?

I have to write some very complex queries, what I would like to do in the query-designer, but run them afterwards embedded in VBA.

I thought it should be possible, at runtime with a function to
- get the SQL-code out of the query
- clean it up / format it
- runsql it

Thanks
Jan 29 '08 #1
11 1893
JustJim
407 Expert 256MB
Is there a way (function) to extract dynamically the SQL-code out of a query ?

I have to write some very complex queries, what I would like to do in the query-designer, but run them afterwards embedded in VBA.

I thought it should be possible, at runtime with a function to
- get the SQL-code out of the query
- clean it up / format it
- runsql it

Thanks
Hi,

Go ahead and design your query in query-designer, fiddle with it to get it exactly right, save it to get it optimized (if you wish), re-open it, go to SQL View, Copy the SQL and paste it into your VBA as a string and runqsl the string!

Jim
Jan 30 '08 #2
wquatan
48
Hi,

Go ahead and design your query in query-designer, fiddle with it to get it exactly right, save it to get it optimized (if you wish), re-open it, go to SQL View, Copy the SQL and paste it into your VBA as a string and runqsl the string!

Jim
Hi, I know that, but thought that it might be possible to get the SQL out of the query at runtime. Especially because I understood that the SQL-code has to be cleaned (removing Carriage-return a.s.o). Isn't that the case ?
Can it been run as-is ?

Thanks
Jan 30 '08 #3
JustJim
407 Expert 256MB
Hi, I know that, but thought that it might be possible to get the SQL out of the query at runtime. Especially because I understood that the SQL-code has to be cleaned (removing Carriage-return a.s.o). Isn't that the case ?
Can it been run as-is ?

Thanks
Very probably, since the query is an Object, but I'm sorry I don't know how.

Jim
Jan 31 '08 #4
Dan2kx
365 100+
When i create sql i use the builder and as you probly know you get it (in sql view) as

SELECT ...........
FROM .............
WHERE............
etc

and if you where to put that in a string you would do it like this i spose

mysql = "SELECT....." & _
"FROM............." & _
"WHERE..........."

i usually just put it all in one lineand dont even bother giving it a string...

docmd.runsql "SELECT......... FROM .......... WHERE............"

Does that answer you question!?
Jan 31 '08 #5
NeoPa
32,556 Expert Mod 16PB
Hi, I know that, but thought that it might be possible to get the SQL out of the query at runtime. Especially because I understood that the SQL-code has to be cleaned (removing Carriage-return a.s.o). Isn't that the case ?
Can it been run as-is ?

Thanks
The QueryDef has a .SQL property which is the SQL string.
Is that what you're after?
Feb 2 '08 #6
NeoPa
32,556 Expert Mod 16PB
Sometimes (as in the explanation part of this procedure) Access (the older versions at least, pre-2003) Access can create incorrect SQL which even it can't process properly. The following little routine will get the SQL out of a QueryDef for you and fix any Access errors it finds. You can simply cut and paste the code into your project if you like.
Expand|Select|Wrap|Line Numbers
  1. 'GetSQL gets the SQL component from a named query OR a SQL string.
  2. 'When subqueries are specified in MS Access they are changed internally
  3. 'from   "FROM (SELECT blah blah blah) AS" to
  4. 'either "FROM [SELECT blah blah blah]. AS"
  5. 'or     "FROM [SELECT blah blah blah] AS" both of which are invalid SQL.
  6. 'This code assumes any effected subquery will start with " [SELECT ".
  7. 'This reverts SQL to correct format and loses ';' at end if requested.
  8. ' 11/11/2004    Updated to detect second problem and to use InStr
  9. Public Function GetSQL(strQuery As String, _
  10.                        Optional blnLoseSC As Boolean = True) As String
  11.     Dim intDepth As Integer
  12.     Dim lngLeft As Long, lngOpen As Long, lngRight As Long, lngAdjust As Long
  13.  
  14.     'If param passed is already SQL then leave as is - Otherwise get from query
  15.     If Left(strQuery, 1) = "(" Then
  16.         GetSQL = strQuery
  17.     Else
  18.         On Error Resume Next
  19.         GetSQL = CurrentDb.QueryDefs(strQuery).SQL
  20.     End If
  21.     lngLeft = -7
  22.     Do
  23.         'Check for corrupted subquery
  24.         lngLeft = InStr(lngLeft + 8, GetSQL, " [SELECT ", vbTextCompare)
  25.         If lngLeft = 0 Then Exit Do
  26.         'To find end correctly we must treat '[' & ']' as matched pairs
  27.         intDepth = 1
  28.         lngRight = lngLeft + 8
  29.         lngOpen = -lngRight
  30.         Do
  31.             'Find next ']'
  32.             lngRight = InStr(lngRight + 1, GetSQL, "]", vbBinaryCompare)
  33.             If lngRight = 0 Then
  34.                 GetSQL = ""
  35.                 Exit Function
  36.             End If
  37.             intDepth = intDepth - 1
  38.             Do
  39.                 'For lngOpen negative numbers mean that item has been counted
  40.                 'If already counted get next one - Otherwise drop through
  41.                 If lngOpen < 0 Then _
  42.                     lngOpen = InStr(-lngOpen + 1, GetSQL, "[", vbBinaryCompare)
  43.                 'we're only interested (now) if it found one BEFORE the ']'
  44.                 If lngOpen > lngRight Or lngOpen = 0 Then Exit Do
  45.                 intDepth = intDepth + 1
  46.                 lngOpen = -lngOpen
  47.             Loop
  48.         Loop While intDepth > 0
  49.         'If '].' found then be sure to drop the '.' too
  50.         lngAdjust = IIf(Mid(GetSQL, lngRight + 1, 1) = ".", 1, 0)
  51.         GetSQL = Left(GetSQL, lngLeft) & "(" & _
  52.                  Mid(GetSQL, lngLeft + 2, lngRight - lngLeft - 2) & ")" & _
  53.                  Right(GetSQL, Len(GetSQL) - (lngRight + lngAdjust))
  54.     Loop
  55.     'Lose ";" at end if requested and it exists
  56.     If blnLoseSC And Right(GetSQL, 3) = ";" & vbCrLf Then _
  57.         GetSQL = Left(GetSQL, Len(GetSQL) - 3)
  58. End Function
Feb 2 '08 #7
JustJim
407 Expert 256MB
Sometimes (as in the explanation part of this procedure) Access (the older versions at least, pre-2003) Access can create incorrect SQL which even it can't process properly. The following little routine will get the SQL out of a QueryDef for you and fix any Access errors it finds. You can simply cut and paste the code into your project if you like.
Expand|Select|Wrap|Line Numbers
  1. 'GetSQL gets the SQL component from a named query OR a SQL string.
  2. 'When subqueries are specified in MS Access they are changed internally
  3. 'from   "FROM (SELECT blah blah blah) AS" to
  4. 'either "FROM [SELECT blah blah blah]. AS"
  5. 'or     "FROM [SELECT blah blah blah] AS" both of which are invalid SQL.
  6. 'This code assumes any effected subquery will start with " [SELECT ".
  7. 'This reverts SQL to correct format and loses ';' at end if requested.
  8. ' 11/11/2004    Updated to detect second problem and to use InStr
  9. Public Function GetSQL(strQuery As String, _
  10.                        Optional blnLoseSC As Boolean = True) As String
  11.     Dim intDepth As Integer
  12.     Dim lngLeft As Long, lngOpen As Long, lngRight As Long, lngAdjust As Long
  13.  
  14.     'If param passed is already SQL then leave as is - Otherwise get from query
  15.     If Left(strQuery, 1) = "(" Then
  16.         GetSQL = strQuery
  17.     Else
  18.         On Error Resume Next
  19.         GetSQL = CurrentDb.QueryDefs(strQuery).SQL
  20.     End If
  21.     lngLeft = -7
  22.     Do
  23.         'Check for corrupted subquery
  24.         lngLeft = InStr(lngLeft + 8, GetSQL, " [SELECT ", vbTextCompare)
  25.         If lngLeft = 0 Then Exit Do
  26.         'To find end correctly we must treat '[' & ']' as matched pairs
  27.         intDepth = 1
  28.         lngRight = lngLeft + 8
  29.         lngOpen = -lngRight
  30.         Do
  31.             'Find next ']'
  32.             lngRight = InStr(lngRight + 1, GetSQL, "]", vbBinaryCompare)
  33.             If lngRight = 0 Then
  34.                 GetSQL = ""
  35.                 Exit Function
  36.             End If
  37.             intDepth = intDepth - 1
  38.             Do
  39.                 'For lngOpen negative numbers mean that item has been counted
  40.                 'If already counted get next one - Otherwise drop through
  41.                 If lngOpen < 0 Then _
  42.                     lngOpen = InStr(-lngOpen + 1, GetSQL, "[", vbBinaryCompare)
  43.                 'we're only interested (now) if it found one BEFORE the ']'
  44.                 If lngOpen > lngRight Or lngOpen = 0 Then Exit Do
  45.                 intDepth = intDepth + 1
  46.                 lngOpen = -lngOpen
  47.             Loop
  48.         Loop While intDepth > 0
  49.         'If '].' found then be sure to drop the '.' too
  50.         lngAdjust = IIf(Mid(GetSQL, lngRight + 1, 1) = ".", 1, 0)
  51.         GetSQL = Left(GetSQL, lngLeft) & "(" & _
  52.                  Mid(GetSQL, lngLeft + 2, lngRight - lngLeft - 2) & ")" & _
  53.                  Right(GetSQL, Len(GetSQL) - (lngRight + lngAdjust))
  54.     Loop
  55.     'Lose ";" at end if requested and it exists
  56.     If blnLoseSC And Right(GetSQL, 3) = ";" & vbCrLf Then _
  57.         GetSQL = Left(GetSQL, Len(GetSQL) - 3)
  58. End Function
Now that's pretty darn clever!

Jim
Feb 3 '08 #8
NeoPa
32,556 Expert Mod 16PB
Thanks Jim.
I had to develop it at work as the Access 2000 SQL which was created for any QueryDef with sub-queries in always failed when I tried to execute it :(

It's probably less clever than it looks. The VBA libraries don't provide anything for finding matching brackets (or quotes or anything really) so I just had to code it in. That's where all the complicated looking code is.

Clever would be to code all that in a couple of lines. Unfortunately I don't think VBA lends itself well to that. It doesn't have the right building blocks (At least not that I found).
Feb 3 '08 #9
JustJim
407 Expert 256MB
Thanks Jim.
I had to develop it at work as the Access 2000 SQL which was created for any QueryDef with sub-queries in always failed when I tried to execute it :(

It's probably less clever than it looks. The VBA libraries don't provide anything for finding matching brackets (or quotes or anything really) so I just had to code it in. That's where all the complicated looking code is.

Clever would be to code all that in a couple of lines. Unfortunately I don't think VBA lends itself well to that. It doesn't have the right building blocks (At least not that I found).
I didn't think it was complicated, but it is clever, and also (my best compliment), elegant.

You will go go Heaven but see PM for details!
Feb 7 '08 #10
NeoPa
32,556 Expert Mod 16PB
High praise indeed. I've seen PM, but the compliment leaves me smiling.
Feb 8 '08 #11
wquatan
48
Sometimes (as in the explanation part of this procedure) Access (the older versions at least, pre-2003) Access can create incorrect SQL which even it can't process properly. The following little routine will get the SQL out of a QueryDef for you and fix any Access errors it finds. You can simply cut and paste the code into your project if you like.
Expand|Select|Wrap|Line Numbers
  1. 'GetSQL gets the SQL component from a named query OR a SQL string.
  2. 'When subqueries are specified in MS Access they are changed internally
  3. 'from "FROM (SELECT blah blah blah) AS" to
  4. 'either "FROM [SELECT blah blah blah]. AS"
  5. 'or "FROM [SELECT blah blah blah] AS" both of which are invalid SQL.
  6. 'This code assumes any effected subquery will start with " [SELECT ".
  7. 'This reverts SQL to correct format and loses ';' at end if requested.
  8. ' 11/11/2004 Updated to detect second problem and to use InStr
  9. Public Function GetSQL(strQuery As String, _
  10. Optional blnLoseSC As Boolean = True) As String
  11. Dim intDepth As Integer
  12. Dim lngLeft As Long, lngOpen As Long, lngRight As Long, lngAdjust As Long
  13.  
  14. 'If param passed is already SQL then leave as is - Otherwise get from query
  15. If Left(strQuery, 1) = "(" Then
  16. GetSQL = strQuery
  17. Else
  18. On Error Resume Next
  19. GetSQL = CurrentDb.QueryDefs(strQuery).SQL
  20. End If
  21. lngLeft = -7
  22. Do
  23. 'Check for corrupted subquery
  24. lngLeft = InStr(lngLeft + 8, GetSQL, " [SELECT ", vbTextCompare)
  25. If lngLeft = 0 Then Exit Do
  26. 'To find end correctly we must treat '[' & ']' as matched pairs
  27. intDepth = 1
  28. lngRight = lngLeft + 8
  29. lngOpen = -lngRight
  30. Do
  31. 'Find next ']'
  32. lngRight = InStr(lngRight + 1, GetSQL, "]", vbBinaryCompare)
  33. If lngRight = 0 Then
  34. GetSQL = ""
  35. Exit Function
  36. End If
  37. intDepth = intDepth - 1
  38. Do
  39. 'For lngOpen negative numbers mean that item has been counted
  40. 'If already counted get next one - Otherwise drop through
  41. If lngOpen < 0 Then _
  42. lngOpen = InStr(-lngOpen + 1, GetSQL, "[", vbBinaryCompare)
  43. 'we're only interested (now) if it found one BEFORE the ']'
  44. If lngOpen > lngRight Or lngOpen = 0 Then Exit Do
  45. intDepth = intDepth + 1
  46. lngOpen = -lngOpen
  47. Loop
  48. Loop While intDepth > 0
  49. 'If '].' found then be sure to drop the '.' too
  50. lngAdjust = IIf(Mid(GetSQL, lngRight + 1, 1) = ".", 1, 0)
  51. GetSQL = Left(GetSQL, lngLeft) & "(" & _
  52. Mid(GetSQL, lngLeft + 2, lngRight - lngLeft - 2) & ")" & _
  53. Right(GetSQL, Len(GetSQL) - (lngRight + lngAdjust))
  54. Loop
  55. 'Lose ";" at end if requested and it exists
  56. If blnLoseSC And Right(GetSQL, 3) = ";" & vbCrLf Then _
  57. GetSQL = Left(GetSQL, Len(GetSQL) - 3)
  58. End Function

First .... I wanna say sorry ! I didn't get notifications about new replies in this post .... and to be honest forgot a liitle bit :-). I didn't want to be rude !

Secondly .... Thanks, this seems exactly what I'm looking after. I'll give it a try asap

Thanks a lot for the great work over here
Feb 20 '08 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

29
by: shank | last post by:
1) I'm getting this error: Syntax error (missing operator) in query expression on the below statement. Can I get some advice. 2) I searched ASPFAQ and came up blank. Where can find the "rules"...
3
by: Harvey | last post by:
Hi, I try to write an asp query form that lets client search any text-string and display all pages in my web server that contain the text. I have IIS 6.0 on a server 2003. The MSDN site says...
0
by: starace | last post by:
I have designed a form that has 5 different list boxes where the selections within each are used as criteria in building a dynamic query. Some boxes are set for multiple selections but these list...
5
by: ????? | last post by:
I have an access query which gets data from a number of different tables. Although the tables have primary key fields, the order in which the records are returned means that none of these are in...
6
by: jjturon | last post by:
Can anyone help me?? I am trying to pass a Select Query variable to a table using Dlookup and return the value to same select query but to another field. Ex. SalesManID ...
3
by: cover | last post by:
I have a table with 50 fields that receive input depending on whether that input came in from a 'shaker' form or a 'conveyor' form. Input from the 'conveyor' form might populate 25 fields while...
6
by: tizmagik | last post by:
I am having a lot of difficulty generating a CrossTab Query based report. I have looked online for several tutorials and whatnot but I have not been able to really find what I'm looking for, nor...
3
by: Kevin M | last post by:
I have one table and have created a form from that table. Also, I have created a delete query. I want to but a button on the form which will delete all records from the table; however, I cannot get...
14
by: Rich P | last post by:
Hi Sara, If you try it this way - you may have better luck Dim strSql As String strSql = "INSERT INTO tblPOCancels ( PONum, PODate, DateEntered, MerchantKey,VendorKey, POApproved, " _ &...
11
beacon
by: beacon | last post by:
Hi everybody, I created a database that links one table from an ODBC data source. I saved my password and UID to the data source so neither myself nor anyone else would have to login each time...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.