Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
Private Sub ParseSQLSelect(sql As String, _
Optional ByRef FieldList As Variant, _
Optional ByRef FromClause As Variant, _
Optional ByRef WhereClause As Variant, _
Optional ByRef OrderByClause As Variant, _
Optional ByRef GroupByClause As Variant, _
Optional ByRef HavingClause As Variant)
' Purpose: Breaks a SQL SELECT statement into its components.
' Accepts: The SQL query string to parse
' Returns: Strings containing the various clauses, without their intro keywords
' 1. The list of fields in the SELECT clause
' 2. The table expression from the FROM clause
' 3. The criteria from the WHERE clause, if present, else ""
' 4. The ordering specs from the ORDER BY clause, if present, else ""
' 5. The list of fields from the GROUP BY clause, if present, else ""
' 6. The criteria from the HAVING clause, if present, else ""
' Remarks: This version supports only SELECT statements without embedded subqueries.
Dim stmt As String
Dim b As Integer, f As Integer
Dim w As Integer, g As Integer, h As Integer, o As Integer, S As Integer
Dim i As Integer
' Copy the string, trimming leading and trailing blanks
' and making sure it ends with a ";"
stmt = Trim$(sql) & ";"
' Translate any CR or LF characters into spaces
Do
i = InStr(i + 1, stmt, vbCr)
If i = 0 Then Exit Do
Mid$(stmt, i, 1) = " "
Loop
Do
i = InStr(i + 1, stmt, vbLf)
If i = 0 Then Exit Do
Mid$(stmt, i, 1) = " "
Loop
' If this query has parameters, delete the PARAMETERS clause
If Left$(stmt, 11) = "PARAMETERS " Then _
stmt = Mid$(stmt, InStr(stmt, "SELECT "))
' Now truncate at the first ";"
stmt = Left$(stmt, InStr(1, stmt, ";"))
' Verify that the statement starts with a SELECT clause, and doesn't
' contain the UNION keyword. This procedure can't parse the statement
' otherwise
If UCase$(Left$(stmt, 7)) <> "SELECT " Then Exit Sub
If InStr(stmt, "UNION ") > 0 Then Exit Sub
' Find the starts of the various clauses, skipping over the keywords
b = InStr(1, stmt, "SELECT ", vbTextCompare): i = b + 7
f = InStr(i, stmt, "FROM ", vbTextCompare): If f > 0 Then i = f + 5
w = InStr(i, stmt, "WHERE ", vbTextCompare): If w > 0 Then i = w + 6
g = InStr(i, stmt, "GROUP BY ", vbTextCompare): If g > 0 Then i = g + 9
h = InStr(i, stmt, "HAVING ", vbTextCompare): If h > 0 Then i = h + 7
o = InStr(i, stmt, "ORDER BY ", vbTextCompare): If o > 0 Then i = o + 9
S = Len(stmt)
' For each missing clause, set its start to the start of the following clause
If o = 0 Then o = S
If h = 0 Then h = o
If g = 0 Then g = h
If w = 0 Then w = g
If f = 0 Then f = w
' Now extract the clauses
b = b + 7
If Not IsMissing(FieldList) Then _
FieldList = Trim$(Mid$(stmt, b, f - b))
If f < w Then f = f + 5
If Not IsMissing(FromClause) Then _
FromClause = Trim$(Mid$(stmt, f, w - f))
If w < g Then w = w + 6
If Not IsMissing(WhereClause) Then _
WhereClause = Trim$(Mid$(stmt, w, g - w))
If g < h Then g = g + 9
If Not IsMissing(GroupByClause) Then _
GroupByClause = Trim$(Mid$(stmt, g, h - g))
If h < o Then h = h + 7
If Not IsMissing(HavingClause) Then _
HavingClause = Trim$(Mid$(stmt, h, o - h))
If o < S Then o = o + 9
If Not IsMissing(OrderByClause) Then _
OrderByClause = Trim$(Mid$(stmt, o, S - o))
End Sub
SELECT MSysObjects.Name, MSysQueries.Attribute, MSysQueries.Expression, MSysQueries.Name1, MSysQueries.Name2
FROM MSysQueries LEFT JOIN MSysObjects ON MSysQueries.ObjectId = MSysObjects.Id
WHERE (((MSysObjects.Name)=[Enter Query Name]))
WITH OWNERACCESS OPTION;
[b]The SQL statement / String of Query Vend[/b]
[code]
SELECT DISTINCTROW tblVend.Vend, tblVend.VendName
FROM tblVend
ORDER BY tblVend.Vend
WITH OWNERACCESS OPTION;
Name[tab][tab]Attribute[tab]Expression[tab]Name1[tab]Name2
qryVend[tab][tab]0
qryVend[tab][tab]255
qryVend[tab][tab]6[tab][tab][tblVend].[Vend]
qryVend[tab][tab]6[tab][tab][tblVend].[VendName]
qryVend[tab][tab]5[tab][tab]tblVend
qryVend[tab][tab]11[tab][tab][tblVend].[Vend]
qryVend[tab][tab]3
[/code
While 'code' is good, I would expect that a brief persual would show the requested information (and a bit more) is provided with a simple (parameter) query. The "Source" objects are (obvioously?) listed in [Name1] & [Name2]. Review / study of the other fields returned may be of interest for other purposes.
It follows basically from some of the more basic rules of relational db, specifically that the schema should be available from within the (relational) data base, although Ms. Version is, perhaps, less straight forward than some.
MichaelRed
m.red@att.net
Searching for employment in all the wrong places