NULL的VB6问题 [英] VB6 issue with NULL

查看:218
本文介绍了NULL的VB6问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在VB6中开发一个应用程序.我遇到了一个似乎很小的问题,但是这很烦人,我无法解决.我正在做的是尝试将2个值传递给一个函数....

I'm working on an application in VB6. I came across what seems to be a small issue, however it is annoying and I am unable to fix it. What I'm doing is trying to pass 2 values to a function....

Private Sub cmdSearch2P_Click()
    Call AxisSearch(2, cboDiagId)
End Sub

这是我要传递给...的功能

This is the function I'm passing it to...

Private Sub AxisSearch(plngAxis As Long, pcbo As ComboBox)

所发生的是cboDiagID是该函数不接受的NULL值. 为了使其正常工作,它必须为".我不知道为什么,但是我正在研究另一个具有类似功能的应用程序.所以我尝试做类似的事情

What's happening is cboDiagID is a NULL value which the function does not accept. In order for it to work, it needs to be "". I don't know why, but I'm working off another application where they have a similar function. So I tried to do something like

Private Sub cmdSearch2P_Click()
    If IsNull(cboDiagID) Then
         cbodiagID = ""
    End if
    Call AxisSearch(2, cboDiagId)
End Sub

但是它仍将其作为NULL传递.我知道"为Null,但是有一种方法可以使它不表示NULL而是".

But it's still passing it as NULL. I know "" is Null however, is there a way to make it so it doesn't say NULL and instead is ""?

看起来其他应用程序正在使用VB6组合框,而我正在编辑的应用程序使用了combobox 2.0控件.那是为什么呢?

it looks like the other application is using VB6 combobox, and the application I'm editing uses combobox 2.0 control. Is that why the issue?

推荐答案

只需回答您的原始问题:首先,"" 不是 Null,它是一个空字符串,它是不一样的东西.如果要将Null转换为空字符串,只需向其中添加一个空字符串:Null & ""的计算结果为"".

Just to answer your original question: first, "" isn't Null, it's an empty string, which is not the same thing. If you want to turn a Null into an empty string, just add an empty string to it: Null & "" evaluates to "".

这可能是一个方便的把戏.在您尝试使用数据库表中的值填充控件(例如标签或文本框)的情况下,它会出现很多情况.例如(假设txtMyBox是一个文本框,而rs是一个ADO Recordset对象):

This can be a handy trick. It comes up a lot in situations where you're trying to populate a control (say, a label or text box) with a value from a database table. For example (assume txtMyBox is a text box and rs is an ADO Recordset object):

txtMyBox = rs.Fields("myField")

现在,如果该字段不包含任何数据,这将引发错误,因为您无法将文本框的值设置为Null.要解决此问题,您可以执行以下操作:

Now, if the field doesn't contain any data, this will throw an error, since you can't set a text box's value to Null. To fix the problem, you could do this:

If Not IsNull(rs.Fields("myField")) Then
    txtMyBox = rs.Fields("myField")
Else
    txtMyBox = ""
End If

这很麻烦.您可以使用三元运算符来简化它:

This is cumbersome. You could streamline it by using the ternary operator:

txtMyBox = IIf (Not IsNull(rs.Fields("myField")), rs.Fields("myField"), "") 

哪个更好,但仍然很麻烦.幸运的是,您也可以执行以下操作:

Which is better, but still cumbersome. Fortunately, you can also just do this:

txtMyBox = rs.Fields("myField") & ""

因为将空字符串连接为字符串对它没有影响,而将空字符串连接为null值则会得到一个空字符串.

Because concatenating an empty string to a string has no effect on it, and concatenating an empty string to a null value gives an empty string.

这篇关于NULL的VB6问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆