I use a DAO Data component to data bind control elements on a form. The query I create dynamically in a recordset which I bind to the Data component. This works pretty well. However, when I run Data.Refresh and the SQL Where statement contains references to the same table via different aliasses then an error shown.
Note: the sql queries below run fine in the MS Access query designer
Global gDB As DataBase
Set Data1.Recordset = GetData(select, from, where, order)
Data1.Refresh
Public Function GetData(select As String, from As String, where As String, order As String) As Recordset
Dim sql As String
sql = "SELECT " & select & " FROM " & from & " WHERE " & where & " ORDER BY " & order
Set GetData = gDB.OpenRecordset(sql, dbOpenDynaset)
End Function
The following will work:
SELECT
WIZ_APPL.*,
TRANS_PRI.Text AS LocalizedText
FROM
TRANSLATIONS AS TRANS_PRI,
WIZ
WHERE
TRANS_PRI.Tag="prog" & WIZ_APPL.Id AND
TRANS_PRI.LanguageId=1 AND
WIZ_APPL.Enabled <> 0
ORDER BY
WIZ_APPL.Id;
Until I try this:
SELECT
WIZ_APPL.*,
TRANS_PRI.Text AS LocalizedText,
TRANS_ALT.Text As AlternativeText
FROM
TRANSLATIONS AS TRANS_PRI,
TRANSLATIONS AS TRANS_ALT,
WIZ_APPL
WHERE
TRANS_PRI.tag="prog" & WIZ_APPL.Id AND
TRANS_ALT.tag="prog" & WIZ_APPL.Id AND
TRANS_PRI.LanguageId=1 AND
TRANS_ALT.LanguageId=2 AND
WIZ_APPL.Enabled <> 0
ORDER BY
WIZ_APPL.Id;
I get a Run-time error '3061':
Too few parameters. Expected 1.
With a slightly different query, which includes an INNER JOIN on another table I get a Run-time error '3075':
Syntax error (missing operator) in query expression 'TRANS_PRI.tag = "prog'.
SELECT
WIZ_APPL.*,
TRANS_PRI.Text AS LocalizedText,
TRANS_ALT.Text As AlternativeText
FROM
TRANSLATIONS AS TRANS_PRI,
TRANSLATIONS AS TRANS_ALT,
WIZ_APPL
INNER JOIN
WIZ_COUNTRY_APPL ON WIZ_APPL.Id = WIZ_COUNTRY_APPL.APPL
WHERE
TRANS_PRI.tag="prog" & WIZ_APPL.Id AND
TRANS_ALT.tag="prog" & WIZ_APPL.Id AND
TRANS_PRI.LanguageId=1 AND
TRANS_ALT.LanguageId=2 AND
WIZ_COUNTRY_APPL.Country=1
ORDER BY
WIZ_APPL.Id;
The bizar thing is that the created recordset is fine and I can print the data. Also, the controls on the form are binding and showing data. However, as soon as I call Data1.Refresh I get the run-time error. Also, refresh() probably destroys the recordset (which is probably normal behaviour).
Set Data1.Recordset = GetData(select, from, where, order)
Do While Not Data1.Recordset.EOF
Debug.Print Data1.Recordset!LocalizedText
Debug.Print Data1.Recordset!AlternativeText
Data1.Recordset.MoveNext
Loop
Data1.Refresh
UPDATE: If I assign the SQL query as String to Data1.RecordSource then the refresh works fine. However, when I assign the SQL query to a recordset then recordset.Name contains only a part of the SQL query. After a Refresh() call Data1.RecordSource is the same as Data1.RecordSet.Name. If the Data component tries to build a query from the shortened Recordset name then it obviously would not work.
Is this a known VB6 DAO Recordset issue?