473,783 Members | 2,317 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 7657
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
"BerkshireG uy" <bd*****@yahoo. com> wrote in
news:11******** **************@ g14g2000cwa.goo glegroups.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().Que ryDefs("qryYour Query")
' 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 = "qryYourQue ry" & CurrentUser()
' check if the QueryDef already exists
' function ExistsQuery is defined after my signature
If ExistsQuery(str NewQueryName) Then
CurrentDb().Que ryDefs.Delete strNewQueryName
End If
Set qdf = CurrentDB.Creat eQueryDef(strNe wQueryName, 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(str QueryName 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.o rg.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
2309
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 update that list once. That is, I have a statement (actually multiple statements) in the Makefile that looks like: FILES = a.f b.f c.c d.py e.c \ f.f g.f h.c
9
1958
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 "/" and ".". So, in other words, "software product version document" - I have absolutley no idea how to do this - can anyone help???? Thanks in advance! Lisa
9
4055
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 size. It's taking over an hour to parse such sizes!? I don't really need to use ASP or a web server at all because I am parsing all in my own computer. Is there any executable that can do this parsing faster than the way I was doing it?
0
4131
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 have an schema (s1) on an Oracle 9i database with database links pointing to a schema (s2) on another Oracle 9i database.
2
362
by: AT | last post by:
I have the ASP page <%@ Language=VBScript %> <% Option Explicit Response.End A = b %>
1
2464
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 and drawing instructions like "move to's" and "change color's" optionally followed by one or more numeric (int or float) arguments. My problem is that the parsing algorithm I've currently implemented is extremely slow.
1
1864
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 grammar: grammar = r''' declaration := ws, line, (ws, line)*, ws line := (statement / assignment), ';', ws
6
2318
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> name = NITROGEN;
12
2678
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
9643
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10147
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10081
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7494
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6735
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4044
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3643
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2875
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.