如何将变量替换为OpenRecordSet查询

时间:2017-07-12 15:47:00

标签: access-vba

我快到了:

变量 strCons 包含一个数字,我想将其添加到SQL中。当我这样做时,我得到这个运行时错误:

  

3061参数太少。预计1。

当我用一个数字替换strCons时,它有效。试图找出没有运气的原因。

Set rs = CurrentDb.OpenRecordset("SELECT Member.HomeEmail FROM Member INNER JOIN MemberRole ON Member.MemberID = MemberRole.MemberID WHERE (((MemberRole.AreaID)= strCons) AND ((MemberRole.RoleID)=1) AND ((MemberRole.FinishDate) Is Null)) ORDER BY Member.Surname")

strEmailTD = rs.Fields(0).Value rs.Close

1 个答案:

答案 0 :(得分:0)

您可以使用:

Set rs = CurrentDb.OpenRecordset("SELECT Member.HomeEmail FROM Member INNER JOIN MemberRole ON Member.MemberID = MemberRole.MemberID WHERE (((MemberRole.AreaID)=" & strCons & ") AND ((MemberRole.RoleID)=1) AND ((MemberRole.FinishDate) Is Null)) ORDER BY Member.Surname")