General discussion

Locked

the infamous null

By setivi ·
What is the most direct and foolproof way to
check for null using VB6 and tables from MS SQL Server 2000.

This conversation is currently closed to new comments.

11 total posts (Page 1 of 2)   01 | 02   Next
| Thread display: Collapse - | Expand +

All Comments

Collapse -

the infamous null

by TINK In reply to the infamous null
Collapse -

the infamous null

by setivi In reply to the infamous null

The question was auto-closed by TechRepublic

Collapse -

the infamous null

by sl-campbell In reply to the infamous null

When assigning a variable to a recordset field you can do the following:

If it is a text field:
strName=rs("Name") & vbNullString

If it is a number, date OR text field:
If Not IsNull(rs("DateBooked")) then
strDate=rs("DateBooked")
End IF
You could also use a variant, which accepts Nulls:

vFieldValue=rs("DateBooked")


You may consider using a function that gets the data from your recordset for each field checks ,which checks for Nulls, or does any needed formating, (by checking the field type):

Public Function GetFieldValue (rs As Recordset, ByVal sField As String) As String

If Not IsNull(rsField)) Then
GetFieldValue =rs(sField)

Else
GetFieldValue =vbNullString
End IF

End Function

This is a very simpleexample, but gives the idea.
I would use a variant as a return value, and in the function check for the field type, (rs(rsField).Type, check if the field has data, and using a Select Case on the field type format the return value, (vbNullString for Text and Date fields, 0 for Number fields, False for Boolean, etc.)

Collapse -

the infamous null

by setivi In reply to the infamous null

The question was auto-closed by TechRepublic

Collapse -

the infamous null

by satheesh In reply to the infamous null

HI,

Suppose ur getting a value from table,
Consider the field name as empname.
Take a variable.

tmp_var_empname=iif(isnull(rs.fields("empname")),"0",rs.fields("empname"))

The result of the variable will be either the data from the field or 0

With regards
S.Satheesh

Collapse -

the infamous null

by setivi In reply to the infamous null

The question was auto-closed by TechRepublic

Collapse -

the infamous null

by PID In reply to the infamous null

Hi

if you mean the error that you get when doing

TextBox1.Text = RS.Fields("Name")

when Name is null in the table.

I use this function.

Public Function NulltoString(strInput As Variant)

If IsNull(strInput) Then
NulltoString = ""
Else
NulltoString = strInput
End If

End Function

like this

TextBox1.Text = NulltoString( RS.Field("Name"))

This way I get a valid string containing the value of what is in Name or an empty stringif Name was NULL.

May be I should rename the function FieldtoString.

Cheers
Pierre

Collapse -

the infamous null

by setivi In reply to the infamous null

The question was auto-closed by TechRepublic

Collapse -

the infamous null

by Peyison In reply to the infamous null

On Answer #4 - you can do the same thing with:

TextBox1.Text = RS.Fields("Name") & ""

Adding a blank string to the value will turn any null value to a blank string, and will leave any text value as is.

Collapse -

the infamous null

by setivi In reply to the infamous null

The question was auto-closed by TechRepublic

Back to Web Development Forum
11 total posts (Page 1 of 2)   01 | 02   Next

Related Discussions

Related Forums