473,397 Members | 2,033 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,397 software developers and data experts.

Parsing out SQL statement

Does anyone know of a good function that will parse out parts of an SQL
statement that is passed to it in seperate variables?

It should be able to parse statements that contain ORDERBY, WHERE,
GROUP, etc.

Thank you,

Brian

Nov 13 '05 #1
6 7626
Ummm... no, but that won't stop you from writing your own...

use InStr to find the chunks you want, and then use left, right, mid to
strip out the chunks you want.

What's the point of this, anyway?

Nov 13 '05 #2
I am trying to accomplish this:

Read in an "orginals" query SELECT using a querydef and setting a
variable called strSQL.
Parse out strSQL to different variables, strSQLWhere, strGroupBy,
strOnOrder, etc.

Then add any additional criteria that a users selects based on Yes/No
fields and/or check boxes.

Then resave the query as their own query with their intitals attached
in the queryname.

Any ideas?

Thanks
Brian

Nov 13 '05 #3
BerkshireGuy wrote:
I am trying to accomplish this:

Read in an "orginals" query SELECT using a querydef and setting a
variable called strSQL.
Parse out strSQL to different variables, strSQLWhere, strGroupBy,
strOnOrder, etc.

Then add any additional criteria that a users selects based on Yes/No
fields and/or check boxes.

Then resave the query as their own query with their intitals attached
in the queryname.

Any ideas?


Would be 1000% easier to just have a base query and put a SQL string
together to query on that query.

--
[OO=00=OO]
Nov 13 '05 #4
BerkshireGuy wrote:
I am trying to accomplish this:

Read in an "orginals" query SELECT using a querydef and setting a
variable called strSQL.
Parse out strSQL to different variables, strSQLWhere, strGroupBy,
strOnOrder, etc.

Then add any additional criteria that a users selects based on Yes/No
fields and/or check boxes.

Then resave the query as their own query with their intitals attached
in the queryname.

Any ideas?

Thanks
Brian


What you want is a bit of trivial drudgery. Why not do it yourself? It
certainly is no challenge to anyone who will take a bit of time to read
the help files and do a bit of work. And someone who does this might
actually learn something.
Make your best try. If your effort has problems post it here then, and
ask for help.

Nov 13 '05 #5
"BerkshireGuy" <bd*****@yahoo.com> wrote in
news:11**********************@g14g2000cwa.googlegr oups.com:
I am trying to accomplish this:

Read in an "orginals" query SELECT using a querydef and setting a
variable called strSQL.
Parse out strSQL to different variables, strSQLWhere, strGroupBy,
strOnOrder, etc.

Then add any additional criteria that a users selects based on
Yes/No fields and/or check boxes.

Then resave the query as their own query with their intitals
attached in the queryname.


Well, sounds like a challenge, but it's not too difficult, assuming
you're not going to be mucking around with the FROM part of the SQL
statement.

It would go something like this (air code):

Dim strSQL As String
Dim strTemp as String
Dim lngOrderBy As Long
Dim strOrderBy As String
Dim lngWhere As Long
Dim strWhere As String
Dim strNewSQL As String
Dim strNewQueryName As String
Dim qdf As DAO.QueryDef

' get the SQL string of the existing query
strSQL = CurrentDB().QueryDefs("qryYourQuery")
' parse out the parts of the SQL string
If Left(Len(strSQL)-1) = ";" Then
strTemp = Left(Len(strSQL)-1) ' strip off the trailing ";"
End If
lngOrderBy = Instr(strTemp,"ORDER BY") ' get the start of ORDER BY
strOrderBy = Mid(strTemp, lngOrderBy) ' pull out ORDER BY clause
strTemp = Left(strTemp, lngOrderBy - 1) ' strip off the ORDER BY
lngWhere = Instr(strTemp, "WHERE") ' get the start of the WHERE
strWhere = Mid(strTemp, lngWhere) ' pull out the WHERE clause
' assign the base of the new SQL
strSQLNew = Mid(strTemp, lngWhere - 1)

' parse the WHERE clause to get it ready for alteration
' this parsing assumes the WHERE clause was written and saved with
' maximum verbosity of parentheses as in:
' 1 2 3 3 2 2 3 3 2 1
' WHERE ( ( (field) = value) AND ( (field2) = value2) )
'
If Mid(strWhere, 7, 1) = "(" Then
' take the starting ( out of the WHERE clause
strWhere = Mid(strWhere, "WHERE " & Mid(strWHERE, 7)
' strip the trailing ) from the WHERE clause
strWhere = Left(strWhere, Len(strWhere) - 1)
End If
' add your new criteria to the original WHERE clause
strWhere = strWhere & " AND " & [your new SQL]

' assemble the new SQL string from the parsed/altered parts
strNewSQL = strNewSQL & vbCrLf & strWhere & vbCrLf & strOrderBy

' prepare to save the new QueryDef
' assume user initials are gotten from CurrentUser()
strNewQueryName = "qryYourQuery" & CurrentUser()
' check if the QueryDef already exists
' function ExistsQuery is defined after my signature
If ExistsQuery(strNewQueryName) Then
CurrentDb().QueryDefs.Delete strNewQueryName
End If
Set qdf = CurrentDB.CreateQueryDef(strNewQueryName, strNewSQL)
qdf.Close
Set qdf = Nothing

Now, all that said, I would suggest that it's mostly a waste of
time.

There is really no reason to save a querydef for something that is
dynamically changing all the time. That is, if you're pulling your
criteria that are being used to alter the WHERE clause from a
query-by-form interface, then just write the SQL in code, rather
than parsing it out of saved QueryDef and then saving it back to a
saved QueryDef that's going to be replaced every time the user wants
to perform a new search. Then use the resulting SQL as the
recordsource of a form to display the results to the user.

--
David W. Fenton http://www.bway.net/~dfenton
dfenton at bway dot net http://www.bway.net/~dfassoc

Public Function ExistsQuery(strQueryName As String, _
Optional db As DAO.Database) As Boolean
Dim bolNoDBPassed As Boolean
Dim qdf As DAO.QueryDef
Dim bolOutput As Boolean

bolNoDBPassed = (db = Nothing)
If bolNoDBPassed Then Set db = CurrentDB()

For Each qdf in db.QueryDefs
bolOutput = (qdf.Name = strQueryName)
If bolOutput Then Exit For
Next qdf

Set qdf = Nothing
if bolNoDBPassed Then Set db = Nothing

ExistsQuery = bolOutput
End Function
Nov 13 '05 #6
Trevor Best <no****@besty.org.uk> wrote in
news:42**********************@news.zen.co.uk:
BerkshireGuy wrote:
I am trying to accomplish this:

Read in an "orginals" query SELECT using a querydef and setting a
variable called strSQL.
Parse out strSQL to different variables, strSQLWhere, strGroupBy,
strOnOrder, etc.

Then add any additional criteria that a users selects based on
Yes/No fields and/or check boxes.

Then resave the query as their own query with their intitals
attached in the queryname.

Any ideas?


Would be 1000% easier to just have a base query and put a SQL
string together to query on that query.


Yes, but that doesn't always work if unless you output all the
fields from the source tables.

I agree, though, that it's something of a waste of time. I wrote out
code to do it, but still wouldn't use it myself. I'd just write the
SQL in code and never worry about the saved querydefs at all. That
would mean I wouldn't have to worry about any pre-existing ORDER BY
and WHERE clauses.

--
David W. Fenton http://www.bway.net/~dfenton
dfenton at bway dot net http://www.bway.net/~dfassoc
Nov 13 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: rick_muller | last post by:
I'm interested in parsing a (simple) Makefile using Python. I need to write a packager for a program I'm supporting, and would like to read the list of files in the makefile so that I only have to...
9
by: Lisa | last post by:
I have a select statement that gives me the following results (for example) "test documentation/software product version document.doc" I need to parse the data to only grab everything between the...
9
by: PedroX | last post by:
Hello: I need to parse some large XML files, and save the data in an Access DB. I was using MSXML 2 and ASP, but it turns out to be extremely slow when then XML documents are like 10 mb in...
0
by: Pentti | last post by:
Can anyone help to understand why re-parsing occurs on a remote database (using database links), even though we are using a prepared statement on the local database: Scenario: ======== We...
2
by: AT | last post by:
I have the ASP page <%@ Language=VBScript %> <% Option Explicit Response.End A = b %>
1
by: Christoph Bisping | last post by:
Hello! Maybe someone is able to give me a little hint on this: I've written a vb.net app which is mainly an interpreter for specialized CAD/CAM files. These files mainly contain simple movement...
1
by: David Hirschfield | last post by:
Anyone out there use simpleparse? If so, I have a problem that I can't seem to solve...I need to be able to parse this line: """Cen2 = Cen(OUT, "Cep", "ies", wh, 544, (wh/ht));""" with this...
6
by: giulianodammando | last post by:
In the development of a simple numerical simulation software i need to read some initialization parameters from a file that looks like: # Global Setup species = 1; \begin{specie}<1>...
12
by: purushotammr | last post by:
Hi Can anyone guide me by providing me the code ,for the following requirement: desining parser for fortran language(parsing fortran)
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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.