FatBoySlim7
FatBoySlim7

Reputation: 232

VB6 issue with NULL

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?

Upvotes: 0

Views: 1800

Answers (2)

BobRodes
BobRodes

Reputation: 6165

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.

Upvotes: 2

FatBoySlim7
FatBoySlim7

Reputation: 232

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

Upvotes: 0

Related Questions