thechazm
VBA, VB.net, C#, Java
- Local time
- Today, 18:04
- Joined
- Mar 7, 2011
- Messages
- 515
Ok so this is sort of a side fun project of mine (if there ever is such a thing) where I designed an interface that uses a treeview control to populate the table names and field names I want the users to have access to.
Then all they do is select the fields they want to include in their query and click the add selected and it adds those fields to a sub form so they can specify other options.
This part works great so far but I am having some problems with building the SQL in code that relates the fields that need to be related in a normalized database environment.
It works 100% with just a single table but if I add more than one table I don't know what the best way is to handle that.
I currently have a table build to feed the treeview where I specify the related table to the field but being able to dynamically create the SQL for this I just can't picture.
Any help or guidance is appreciated. I am attaching a picture of the screen and also my code I use to build the SQL if anyone is interested and maybe help me fill in the gaps.


Then all they do is select the fields they want to include in their query and click the add selected and it adds those fields to a sub form so they can specify other options.
This part works great so far but I am having some problems with building the SQL in code that relates the fields that need to be related in a normalized database environment.
It works 100% with just a single table but if I add more than one table I don't know what the best way is to handle that.
I currently have a table build to feed the treeview where I specify the related table to the field but being able to dynamically create the SQL for this I just can't picture.
Any help or guidance is appreciated. I am attaching a picture of the screen and also my code I use to build the SQL if anyone is interested and maybe help me fill in the gaps.


Code:
Function BuildQuery(rs As DAO.Recordset)
Dim strSQL As String, strTblName As String, bSorted As Boolean, qdf As QueryDef, db As Database, rs2 As DAO.Recordset, lQueryID As Long
'On Error GoTo ErrHandler
bSorted = False
Set db = CurrentDb
rs.MoveFirst
rs.Sort = "[C Order]"
Set rs = rs.OpenRecordset
lQueryID = rs("C Query ID")
Set rs2 = db.OpenRecordset("Select * from [Custom Query Relationships Query] WHERE((([CQ ID]) = " & lQueryID & "))", dbOpenSnapshot)
'Build Initial SQL
strSQL = "Select "
' Get Field Names
Do While rs.EOF = False
If rs.AbsolutePosition = 0 Then
strSQL = strSQL & "[" & rs("C Field Table") & "].[" & rs("C Field Name") & "]"
Else
strSQL = strSQL & ", [" & rs("C Field Table") & "].[" & rs("C Field Name") & "]"
End If
rs.MoveNext
Loop
strSQL = strSQL & " From "
rs.MoveFirst
' Get Table Names
Do While rs.EOF = False
' Personnel INNER JOIN [Personnel Shops] ON Personnel.ID = [Personnel Shops].[Personnel ID]
If strtablename <> rs("C Field Table") Then
If rs.AbsolutePosition = 0 Then
strSQL = strSQL & "[" & rs("C Field Table") & "]"
strtablename = rs("C Field Table")
ElseIf strtablename <> rs("C Field Table") And rs2.EOF = False Then
Do While rs2.EOF = False
strSQL = strSQL & " INNER JOIN [" & rs2("Child Table") & "] ON [" & rs2("Master Table") & "].[" & rs2("Master Field") & "] = [" & rs2("Child Table") & "].[" & rs2("Child Field") & "]"
rs2.MoveNext
Loop
strtablename = rs("C Field Table")
Else
strSQL = strSQL & ", [" & rs("C Field Table") & "]"
strtablename = rs("C Field Table")
End If
End If
rs.MoveNext
Loop
rs.MoveFirst
' Setup WHERE Statement
' Setup Order By
Do While rs.EOF = False
If rs("C Sort") = True And bSorted = False Then
bSorted = True
strSQL = strSQL & " ORDER BY [" & rs("C Field Name") & "]"
ElseIf rs("C Sort") = True And bSorted = True Then
strSQL = strSQL & ", [" & rs("C Field Name") & "]"
End If
rs.MoveNext
Loop
strSQL = strSQL & ";"
If CurrentData.AllQueries("TempQuery").IsLoaded = True Then
DoCmd.Close acQuery, "TempQuery", acSaveNo
End If
If ObjectExists("Query", "TempQuery") Then
DoCmd.DeleteObject acQuery, "TempQuery"
End If
Set qdf = db.CreateQueryDef("TempQuery", strSQL)
DoCmd.OpenQuery "TempQuery", acViewNormal, acReadOnly
rs.Close
Set rs = Nothing
Exit Function
ErrHandler:
If Err.Number = 3021 Then
rs.Close
Set rs = Nothing
Exit Function
Else
MsgBox Err.Number & " - " & Err.Description
End If
rs.Close
Set rs = Nothing
End Function