代码之家  ›  专栏  ›  技术社区  ›  IcyPopTarts

VBA中的DCount()始终返回空值

  •  0
  • IcyPopTarts  · 技术社区  · 8 年前

    我试图使用DCount()函数从表中返回一个计数。我的问题是它总是返回空值。

    ReturnedCount = DCount("CountOfItems", "[__TestTable]", "NameOfItem = " & ItemName)
    Debug.Print ReturnedCount
    
    2 回复  |  直到 5 年前
        1
  •  1
  •   user6432984 user6432984    8 年前

    NameOfItem 表示字符串。当将字符串作为参数传递给D函数时,需要将其括在单引号中;就像在查询中作为参数传递一样。

    ReturnedCount=DCount(“CountOfItems”、“[\u TestTable]、“NameOfItem='”、“&ItemName&'”)

    使用即时窗口测试D函数将简化调试。

    enter image description here

        2
  •  0
  •   Gustav    8 年前

    您应该使用:

    On Error Goto 0
    ReturnedCount = DCount("*", "[__TestTable]", "NameOfItem = '" & ItemName & "'")
    

    它将至少返回0(零)。。。表名和字段名是否正确。

    推荐文章