在Access中使用SQL查询中的变量

时间:2018-07-10 15:00:39

标签: sql vba ms-access access-vba

如何在Access VBA中修改下面的SQL查询,其中6021、1和EachYear是变量。

SELECT  & Edgar_Gen.[SIC Code], Edgar_Fin.EntityID, Edgar_Fin.IsANN, Edgar_Fin.[Fiscal Year],
  Formula AS val
FROM Edgar_Fin INNER JOIN Edgar_Gen ON Edgar_Fin.EntityID = Edgar_Gen.EntityID
WHERE (((Edgar_Gen.[SIC Code]) = 6021) AND
 ((Edgar_Fin.IsANN)="1") AND ((Edgar_Fin.[Fiscal Year])="EachYear"));

1 个答案:

答案 0 :(得分:0)

当您要求为6021 1EachYear“修改下面的SQL查询”时,并不清楚您指的是什么,但这是基本思想,您似乎想要完成的工作的结构:

Variable1 = 6021
Variable2 = 1
Variable3 = 2018

CurrentDb.Execute "SELECT Edgar_Gen.[SIC Code], " & _
    "Edgar_Fin.EntityID, " & _
    "Edgar_Fin.IsANN, " & _
    "Edgar_Fin.[Fiscal Year], " & _
    "Formula AS val " & _
"FROM Edgar_Fin " & _
"INNER JOIN Edgar_Gen ON Edgar_Fin.EntityID = Edgar_Gen.EntityID " & _
"WHERE ( " & _
       "((Edgar_Gen.[SIC Code]) = " & Variable1 & ") " & _
       "AND ((Edgar_Fin.IsANN) = " & Variable2 & ") " & _
       "AND ((Edgar_Fin.[Fiscal Year]) = " & Variable3 & ") " & _
       ") "