Function GetReferencedTables(sqlText As String) As Variant
Dim regex As Object
Set regex = CreateObject("VBScript.RegExp")
regex.Global = True
regex.Pattern = "(FROM|JOIN)\s+(\[?\w+\]?\.)?\[?(\w+)\]?"
Dim tables As Object
Set tables = CreateObject("Scripting.Dictionary")
Dim matches As Object
Set matches = regex.Execute(sqlText)
Dim i As Long
For i = 0 To matches.Count - 1
Dim tableName As String
tableName = matches.Item(i).SubMatches.Item(2)
If Not tables.Exists(tableName) Then
tables.Add tableName, True
End If
Next i
GetReferencedTables = tables.Keys
End Function