vba VarType区分字符串和整数
我正在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能正在字符串中输入数字。使用
IsNumeric
预先转换它或更改您的函数调用。You are probably entering the number in a String. Use
IsNumeric
to transform it beforehand or change your function call.