vba VarType区分字符串和整数

发布于 2024-11-25 18:50:46 字数 766 浏览 0 评论 0原文

我正在 VBA 中准备一个函数来创建查询来搜索数据库。参数之一“Value”可以是字符串(例如:“ON”)或数字(例如:123),如果它是字符串,我必须选择与其不同的值(例如:“OFF”)并且如果它是一个数字,我必须选择大于它的值(例如:234)。

我准备了下面总结的函数,传递值以将“valueParam”作为变体进行比较,然后尝试检测“valueParam”是字符串还是整数。问题在于函数 VarType 将每次“valueParam”都视为字符串。

Function prepareQuery(ByVal valueParam As Variant) as String
    Dim STR_Query as String
    STR_Query = "Select * FROM tablename"

    If VarType(valueParam) = vbInteger Then
        STR_Query = STR_Query & " WHERE Value>" & valueParam
    ElseIf VarType(valueParam) = vbString Then
        STR_Query = STR_Query & " WHERE Value<>'" & valueParam & "'"
    End If

    prepareQuery = STR_Query
End Function

有谁知道为什么 VarType 不能识别整数或者有另一个想法来区分数字和字符串?

非常感谢

I am preparing a function in VBA to create a query to search a database. One of the parameters, "Value", can be a string (e.g.: "ON") or a number (e.g.: 123), if it's a string, I have to select values different from it (e.g.: "OFF") and if it's a number, I have to select values larger than it (e.g.: 234).

I have prepared the function summarised below, passing the value to compare "valueParam" as a Variant, then trying to detect if "valueParam" is a String or an Integer. The problem is that the function VarType treat each time "valueParam" as a String.

Function prepareQuery(ByVal valueParam As Variant) as String
    Dim STR_Query as String
    STR_Query = "Select * FROM tablename"

    If VarType(valueParam) = vbInteger Then
        STR_Query = STR_Query & " WHERE Value>" & valueParam
    ElseIf VarType(valueParam) = vbString Then
        STR_Query = STR_Query & " WHERE Value<>'" & valueParam & "'"
    End If

    prepareQuery = STR_Query
End Function

Has anyone an idea of why VarType does not recognise the Integer or has another idea to distinguish between numbers and strings?

Thank you very much

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你穿错了嫁妆 2024-12-02 18:50:46

您可能正在字符串中输入数字。使用 IsNumeric 预先转换它或更改您的函数调用。

If IsNumeric(valueParam) Then
  valueParam = CLng(valueParam) 'or CDbl, depending on what you want ...
End If

You are probably entering the number in a String. Use IsNumeric to transform it beforehand or change your function call.

If IsNumeric(valueParam) Then
  valueParam = CLng(valueParam) 'or CDbl, depending on what you want ...
End If
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文