使用变量运行Docmd runsql时出错

时间:2014-09-18 06:07:18

标签: vba ms-access access-vba ms-access-2007 ms-access-2010

我知道我在问一个简单的问题, 我无法找到问题的原因。 当我运行followng代码时,它在Docmd RunSqL命令中显示错误,显示无效的争论。似乎可能是由于变量ListTablename。

这是我的代码

 Function RefreshedLinks()
    Dim rs1 As DAO.Recordset
    Dim rs2 As DAO.Recordset
    Dim stringsql As String
    Dim strPath As String
    Dim Foldername As String
    Dim strTableNameCheck
    Dim obj As AccessObject, dbs As Object
    Set dbs = Application.CurrentData
    Set rs1 = CurrentDb.OpenRecordset("Table Valued Parameter")
    ListTablename = "SELECT [LE Based Table].[Table Name], [LE Based Table].LE FROM [LE Based Table] WHERE ((([LE Based Table].LE)=[Forms]![Combo Box]![LE Select]));"

    DoCmd.SetWarnings False

    DoCmd.RunSQL ListTablename

    DoCmd.SetWarnings True
    End Function

查询的where条件是一个字符串函数,基于用户在表单组合框中完成的选择,如XLL,XLI

我的查询有问题吗?

先谢谢

1 个答案:

答案 0 :(得分:0)

在这种情况下,您为什么不在SQL中插入值:

 ListTablename = "SELECT [LE Based Table].[Table Name], [LE Based Table].LE FROM [LE Based Table]
 WHERE ((([LE Based Table].LE)=" & [Forms]![Combo Box]![LE Select] & "));"

抱歉,RunSQL可用于操作查询或数据定义查询。因此,创建查询,将其保存到db,然后使用DoCmd.OpenQuery