【问题标题】:Combining 3 strings in Access VBA在 Access VBA 中组合 3 个字符串
【发布时间】:2013-07-12 22:05:08
【问题描述】:

我有一个表单,可以按用户、日期范围和列的特定值过滤数据,具体取决于是否勾选了复选框,如下所示;

    strWhere = " WHERE [user] in (" & Left(strIN, Len(strIN) - 1) & ") And [Month] Between [forms]![frm_user]![txtStartDate] And [forms]![frm_user]![txtEndDate]"

    If Me!checkbox = True Then
        strtcheck = " (Satified Vs Dissatisfied Like Dissatisfied) "
    End If

    strSql = strSql & strwhere & strtcheck 

所以我想要但我无法让它工作的是,如果 Me!checkbox 为真,那么 Satified Vs Dissatisfied 必须等于 Dissatisfied,然后我想将它传递给 strSql,但是当我运行时在Access中它不起作用,有人可以帮忙吗?

【问题讨论】:

  • 合成的 where 子句中似乎缺少一些引号。试试strWhere = " WHERE [user] in ('" & Left(strIN, Len(strIN) - 1) & "') And [Month] Between [forms]![frm_user]![txtStartDate] And [forms]![frm_user]![txtEndDate]"。 strtcheck 中的 Vs 运算符是什么意思? Satisfied、Dissatisfied 是表属性、表单属性还是文字? like 运算符仅在与 sql 通配符 (%) 一起使用时才有意义。如果您将 strtcheck 附加到部分 where 子句,则您忘记了运算符 (AND?)。 strIN的内容是什么?。
  • “满意与不满意”列的名称
  • 好的,刚刚在 hansup 的回答中看到了(在 where 子句的第一部分加上单引号应该可以解决问题)。

标签: ms-access vba


【解决方案1】:

如果您的字符串旨在为名为 Satified Vs Dissatisfied 的字段包含 WHERE 条件,请将字段名称括在方括号中。并将您与该字段进行比较的字符串括在引号中。

strtcheck = " ([Satified Vs Dissatisfied] Like 'Dissatisfied') "

实际上该条件不使用模式匹配,因此您可以只使用= 而不是Like。

strtcheck = " ([Satified Vs Dissatisfied] = 'Dissatisfied') "

您可能还需要包含 SQL AND 以将该条件与其他 WHERE 条件结合起来。

strtcheck = " AND ([Satified Vs Dissatisfied] = 'Dissatisfied') "

【讨论】:

  • 谢谢,但我真正的问题是 strSql = strSql & strwhere & strtcheck,它不起作用
  • 不包含任何内容,它是空的。我可以像上图那样组合 3 个字符串吗?
  • @qebas:你需要在strtcheck 后面加上一个操作符(我猜是AND)以获得语法上有效的where 子句。将strtcheck = " (Satified Vs Dissatisfied Like Dissatisfied) " 变成strtcheck = " AND ([Satified Vs Dissatisfied] = 'Dissatisfied') "。并确保您的列名中没有拼写错误 (Sati_s_fied)。
  • @collapsar 好点,谢谢。我将其添加到我的答案中。但是我不明白为什么strSql 是空的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-12-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多