sql—使用动态表的内部联接(vba)执行动态查询时出错

6yjfywim  于 2021-08-09  发布在  Java
关注(0)|答案(0)|浏览(244)

我为我的英语感到抱歉;)
我在网上查阅了很多资料,但找不到解决办法
我必须用一个函数创建一个动态查询。如果表没有链接,但在同一bbdd access 2016中,则此代码对我有效。但我需要他们在另一个bbdd。你能帮助我吗?
它告诉我数据丢失了。
查询在access中进行,并在vba中修改,添加变量。
ano和trimestre是数字,其他是文本。

Public Function PRUEBA_INNER(ByVal TRIMESTRE As String,ByVal ANO As String) As Boolean
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim StrSQL As String
Set db = CurrentDb
    StrSQL = "SELECT " & _
    "PERSONAS.NOMBRE, " & _
    "PERSONAS.MAIL, " & _
    "PERSONAS.[NUMR_COLEG], " & _
    "DATOS_" & ANO & ".ANO, " & _
    "DATOS_" & ANO & ".TRIMESTRE " & _
    "FROM " & _
    "PERSONAS " & _
    "INNER JOIN DATOS_" & ANO & " " & _
    "ON PERSONAS.[NUMR_COLEG] = DATOS_" & ANO & ".NUMR_COLEG " & _
    "GROUP BY " & _
    "PERSONAS.NOMBRE, " & _
    "PERSONAS.MAIL, " & _
    "PERSONAS.[NUMR_COLEG], " & _
    "DATOS_" & ANO & ".ANO, " & _
    "DATOS_" & ANO & ".TRIMESTRE " & _
    "HAVING (((DATOS_" & ANO & ".ANO)=" & ANO & ")" & _
    " AND ((DATOS_" & ANO & ".TRIMESTRE)=" & TRIMESTRE & "))"
Set rs = db.OpenRecordset(StrSQL)
Do Until rs.EOF
    debug.print rs!nombre
    rs.MoveNext
Loop
rs.Close: Set rs = Nothing
db.Close: Set db = Nothing
End Function

调试打印:

SELECT 
PERSONAS.NOMBRE, 
PERSONAS.MAIL, 
PERSONAS.[NUMR_COLEG], 
DATOS_2018.ANO, 
DATOS_2018.TRIMESTRE 
FROM 
PERSONAS 
INNER JOIN 
DATOS_2018 
ON 
PERSONAS.[NUMR_COLEG] = DATOS_2018.NUMR_COLEG 
GROUP BY 
PERSONAS.NOMBRE, 
PERSONAS.MAIL, 
PERSONAS.[NUMR_COLEG], 
DATOS_2018.ANO, 
DATOS_2018.TRIMESTRE 
HAVING (((DATOS_2018.ANO)=2018) AND ((DATOS_2018.TRIMESTRE)=2))

错误消息:

nº 3061
Few parameters.
1 expected

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题