vba DCount 有 2 个条件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17839572/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 22:23:58  来源:igfitidea点击:

DCount with 2 criteria

vbams-accessms-access-2007access-vba

提问by Patrick

I am using DCountto help display an error message if the data signals an error. I have it working with one criteria where a number is equal to another number. Now I wanted to add another criteria in there where another field, ReturnDate(this field is Text not Date/Time) is equal to a hyphen (-). I'm just not really sure how to format it. I have this:

DCount如果数据发出错误信号,我将用来帮助显示错误消息。我让它使用一个数字等于另一个数字的标准。现在我想在其中添加另一个条件,其中另一个字段ReturnDate(该字段是文本而不是日期/时间)等于连字符 (-)。我只是不确定如何格式化它。我有这个:

If DCount("*", "CrewTable", "KitNumber=" & _
  Me.AssignKit.Value And "ReturnDate=" & _
  "-") > 0 Then
  Cancel = True
  MsgBox "Kit is already assigned!"
  AssignKit = ""
  AssignKit.SetFocus
Else
...

The error pops up with a 'Type Mistmatch' and the debugger highlights the whole statment from 'If -> Then' and has an error pointing to the line with the hyphen in the quotes.

错误弹出“类型不匹配”,调试器突出显示“If -> Then”中的整个语句,并有一个错误指向引号中带连字符的行。

回答by mwolfe02

If DCount("*", "CrewTable", "ReturnDate='-' AND KitNumber=" & _
          Me.AssignKit.Value) > 0 Then

回答by HansUp

It's easier to troubleshoot DCounterrors when you store its Criteriaoption in a string variable.

DCount当您将其Criteria选项存储在字符串变量中时,更容易排除错误。

Dim strCriteria As String
strCriteria = "ReturnDate='-' AND KitNumber=" & Me.AssignKit.Value
Debug.Print strCriteria
If DCount("*", "CrewTable", strCriteria) > 0 Then

If you had used this approach, Access would have alerted you to the fact that the original code which built the Criteriastring was invalid. That should make it clearer that the problem wasn't due to the Ifcondition, and it wasn't exactlya DCountproblem either ... it was a problem with string concatenation.

如果您使用了这种方法,Access 会提醒您构建Criteria字符串的原始代码无效。这应该更清楚地表明问题不是由If条件引起的,也不是完全DCount问题......这是字符串连接的问题。

回答by basdwarf

Me.AssignKit.Value & " And ReturnDate=" & _