VB6 issue with NULL

别来无恙 提交于 2019-12-25 06:47:52

问题


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)

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

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 ""?

EDIT: 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?


回答1:


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 "".

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")

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") & ""

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.




回答2:


Figured it out. It doesn't matter if it's NULL or "", because the combobox that's in place is combobox 2.0, I couldn't just pass it as combobox in my function, but this fixed it..

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

more specifically msforms.combobox



来源:https://stackoverflow.com/questions/37057665/vb6-issue-with-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!