Question

Quite simply i need to figure out how to rewrite the following code for VB6 so that my application allows nothing to be passed into a textbox.text value

Function GetDbValue(ByVal tb)
 If tb.Text = "" Then
Return Nothing
Else
 Return tb.Text
End If
  End Function

Im getting a syntax error for "return nothing"

EDIT:

Stored proc

 Set prm = cmdDlrID.CreateParameter("@ContractNumberField", adVarChar, adParamInput, 50, GetDbValue(txtContNum))
 cmdDlrID.Parameters.Append prm
Was it helpful?

Solution

In VB6 there are at least four different types of nothing:

  • Nothing, a null pointer as an object
  • vbNullString, a null pointer as a string
  • Empty, an uninitialized variant
  • Null, a null value passed to / from a database

If you're passing the function into an ADO parameter, you need Null, not Nothing:

Function GetDbValue(ByVal tb)
    If tb.Text = "" Then
        GetDbValue = Null
    Else
        GetDbValue = tb.Text
    End If
End Function

OTHER TIPS

Try it like this (untested):

Function GetDbValue(ByVal tb)
 If tb.Text = "" Then
  Set GetDbValue = Nothing
 Else
  GetDbValue = tb.Text
 End If
End Function

IIRC, you don't use the Return keyword to return a function value in VB6/VBA, you assign the return value to the function's name.

If I remember correctly to return a value in VB6, you set the function equal to that value

So you code would look something like this.

Function GetDbValue(ByVal tb as TextBox) as String
 If tb.Text = "" Then
    GetDbValue =  Nothing
 Else
    GetDbValue = tb.Text
 End If
End Function

A short tutorial on Sub routines and Functions can be found here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top