Here's the skinny: I'm creating a database tool in an excel book that can automate some queries against an external Database (Teradata, not that it matters).
My basic setup is:
I have a list of query files in the excel book.
Each entry in the list is the name of a .sql file.
Each query, before being edited to work with my tool, runs just fine, and we've been using them for ages.
However, I've made a few changes. Wherever there is a part of the query which will or might change, I have replaced that section with a string that looks like "$THIS$".
I've written some VBA code that opens the .sql as a textstream, finds instances of text delimited by "$", searches a table I've set up for whatever text it finds, and then returns a comma delimited list of every item below the text that it finds.
Example:
If the .sql file has $ONE$ in it, it returns "1".
If it has $TWO$, it returns "1, 2"
After it returns this text, it replaces the original string.
Then, it updates the CommandText of a querytable on another sheet with the altered query text, and refreshes.
After that, it copies and saves the results off to a text file.
All of this works as intended... My biggest problem, though, comes from formatting the .sql files so that the queries don't bomb. And my main culprit there is when there's some simple, 2nd grade arithmetic in the query, which we mostly use on dates.
Example:
I'm sure I can alter my code to ignore them. But I'm greedy. I want to calculate the date fields using these formula.
Here's the code that searches the query:
and the fillquery function:
So I'm looking for a way to capture formula strings, and parse them along with the date values.
My basic setup is:
I have a list of query files in the excel book.
Each entry in the list is the name of a .sql file.
Each query, before being edited to work with my tool, runs just fine, and we've been using them for ages.
However, I've made a few changes. Wherever there is a part of the query which will or might change, I have replaced that section with a string that looks like "$THIS$".
I've written some VBA code that opens the .sql as a textstream, finds instances of text delimited by "$", searches a table I've set up for whatever text it finds, and then returns a comma delimited list of every item below the text that it finds.
Example:
Code:
$ONE$ $TWO$
1 1
2
If it has $TWO$, it returns "1, 2"
After it returns this text, it replaces the original string.
Then, it updates the CommandText of a querytable on another sheet with the altered query text, and refreshes.
After that, it copies and saves the results off to a text file.
All of this works as intended... My biggest problem, though, comes from formatting the .sql files so that the queries don't bomb. And my main culprit there is when there's some simple, 2nd grade arithmetic in the query, which we mostly use on dates.
Example:
Code:
WHERE table.datefield >= $WE_DATE$ - ($FISCAL_WEEKS$ * 4) -7
I'm sure I can alter my code to ignore them. But I'm greedy. I want to calculate the date fields using these formula.
Here's the code that searches the query:
Code:
Sub runqueries()
Dim strQryName As Variant
Dim qt As QueryTable
Dim qryFile As Workbook
Set qt = Sheets("QueryTable").QueryTables(1)
Application.Calculation = xlCalculationManual
Application.DisplayAlerts = False
For Each strQryName In Sheets("Control").Range("queries")
qt.CommandText = fillquery(strQryName)
qt.Refresh
Sheets("QueryTable").UsedRange.Copy
Set qryFile = Workbooks.Add
qryFile.Sheets(1).Range("A1").PasteSpecial xlPasteValues
qryFile.SaveAs "X:\Path\" & Left(strQryName, Len(strQryName) - 4) & ".txt", xlTextWindows
qryFile.Close
Next strQryName
Application.Calculation = xlCalculationAutomatic
Application.DisplayAlerts = True
Set qt = Nothing
Set qryFile = Nothing
End Sub
and the fillquery function:
Code:
Function fillquery(ByVal queryfile As String)
Dim fs, ts As Object
Dim strQry, strQryLine As String
Dim qrySplit As Variant
'Debug.Assert 0
Set fs = CreateObject("Scripting.FileSystemObject")
Set ts = fs.OpenTextFile("X:\Path2\" & queryfile, 1)
While ts.AtEndOFStream <> True
strQryLine = ts.ReadLine
If InStr(strQryLine, "$") Then
ReDim qrySplit(0)
qrySplit = Split(strQryLine, "$")
For i = 0 To UBound(qrySplit)
If Len(qrySplit(i)) < 50 Then
If qrySplit(i) = UCase(qrySplit(i)) Then
If Len(qrySplit(i)) > 4 Then
qrySplit(i) = getList("$" & qrySplit(i) & "$")
End If
End If
End If
Next i
strQry = strQry & Join(qrySplit, " ") & " "
Else
strQry = strQry & strQryLine & " "
End If
Wend
ts.Close
fillquery = strQry
Set ts = fs.CreateTextFile("X:\Path2\test.sql")
ts.write fillquery
ts.Close
Set fs = Nothing
Set ts = Nothing
End Function
So I'm looking for a way to capture formula strings, and parse them along with the date values.