What is the difference between "Null" and "Nothing" in VB6? - vb6

I have a recordset like this:
Dim rs as Recordset
Set rs as New Recordset
'... a lot of coding ...
if Err.Number <> 0 Then ' oops, something gone wrong!
If rs.State <> adStateClosed Then rs.Close
Set rs = Nothing
end if
' I want to evaluate if rs is Nothing, or Null
if rs is Nothing then
' this doesn't throw errors, and works well :D
end if
if rs is Null then
' this throws an error of "types not compatible"
end if
if rs = Null then
' this throws an error of "types not compatible"
end if
if isNull(rs) then
' never enters here, isNull(rs) evaluates to False
end if
I found out that in VB6 I rarely use "Null" (I used it for evaluating empty recordset schema names), but I use "Nothing" for stuff like images, adodb.connections or recordsets. For strings I have vbNullString. I read it is a pointer to a null string.
Is "Null" like a "unknown variable value" and "Nothing" a true null value?

Null is a specific subtype of a Variant. It has no existence outside of the Variant type, and is created to allow a Variant to model a database null value.
Nothing is a value of an Object variable. It essentially is identical to a null pointer, i.e. there is no object.
The following raises an error because "Is" can only be used with Object variables:
if rs is Null then
' this throws an error of "types not compatible"
end if
The following raises an error because an Object variable can never be Null:
if rs = Null then
' this throws an error of "types not compatible"
end if
The following evaluates False because IsNull() takes a Variant argument.
if isNull(rs) then
' never enters here, isNull(rs) evaluates to False
end if
It is equivalent to:
VarType(rs) = vbNull

The following table explains how to use VBScript keywords.
Empty
The Empty keyword is used to indicate an uninitialized variable value. This is not the same thing as Null.
False
The False keyword has a value equal to 0.
Nothing
The Nothing keyword in VBScript is used to disassociate an object variable from any actual object. Use the Set statement to assign Nothing to an object variable. For example:
Set MyObject = Nothing
Several object variables can refer to the same actual object. When Nothing is assigned to an object variable, that variable no longer refers to any actual object. When several object variables refer to the same object, memory and system resources associated with the object to which the variables refer are released only after all of them have been set to Nothing, either explicitly using Set, or implicitly after the last object variable set to Nothing goes out of scope.
Null
The Null keyword is used to indicate that a variable contains no valid data. This is not the same thing as Empty.
True
The True keyword has a value equal to -1.

You can try as follow:
a = "SELECT SBio.biosrno,YearlyCharges.adnum,Name,sName
FROM SBio RIGHT JOIN YearlyCharges ON SBio.biosrno=YearlyCharges.adnum
WHERE (SBio.biosrno IS NULL );"

Related

Run-time error '91' when adding data to a record set

I want to insert some information into a database in VB6, but I get runtime error '91'.
My code:
Private sub btn_click()
Fname = txtFname.text
Adodc1.Recordset.AddNew
Adodc1.Recordset.Fields("Fname") = Fname
Adodc1.Recordset.Update
End sub
The debuger shows error on: Adodc.Recordset.AddNew
You haven't posted the rest of the code as to where the Adhoc1 variable is created etc...
This post suggests that the RecordSet has not been initialized prior to working with it, but it's hard to be more specific without the code.
Runtime error '91' is Object variable or With block variable not set, which is a slightly confusing way to say that your variable contains Nothing.
You either forgot to initialise the Adodc1 properly, or, and this is more likely, you need to initialise Adodc1.RecordSet to something useful (like a Set Adodc1.RecordSet = New RecordSet or related) before you can use it.
By the way you posted the code, I believe that you will populate a Recordset to insert into the database. Try as follows:
sub btn_click()
dim Adodc1 as adodb.recordset
set Adodc1 = new adodb.recordset
Fname = txtFname.text
Rs.Fields.Append "Fname", adVarChar, 20 'adVarChar = text, followed by the amount of characters
Adodc1.open()
Adodc1.Recordset.AddNew
Adodc1.Recordset.Fields("Fname") = Fname
Adodc1.Recordset.Update
End sub

QTP handling Null values with public functions

I am trying to write a QTP Script which works as follows
* takes a column from table checks if it is null
if yes
it adds that column name in a variable .
Later that column name will be used to display to user that all such fields are left blank .
Something like this:
Set rsRecordset1 = dbConnection.Execute("select formcd,sndrpstlcd,recppstlcd,sndrcntrycd, from test_data where trk in ('"&tin&"')")
If rsRecordset1.EOF = false Then
Blank_field="blank_fields->"
formcd = rsRecordset1("formcd").value
typcd="formcd"
Call validatenull (typcd,Blank_field)
packaging = rsRecordset1("packaging").value
typcd="packaging"
Call validatenull (typcd,Blank_field)
......
Public function validatenull (typcd,Blank_field)
If (isnull(typcd) )Then
Blank_field = Blank_field & " " & typcd &", "
End If
End Function
Value of blank_Field is expected to catch values as "blank_fields->Form_id,packaging (Considering Form_id, Packaging passes Null values) {It shouldnt add values which are not Null to blank_fields}
In simpler words
ss = Null
call nnn(ss)
Public function nnn(ss)
If isnull(ss) Then
msgbox("yes")
End If
End Function
Has Error that msgbox does not populates But,
ss= Null
nnn(ss)
If isnull(ss) Then
msgbox("yes")
End If
Populates Msgbox as Yes
But I want Case 1 to be working and I do not want to add any more variables to the function name.
IF By making an Array we can do it, please suggest .
It appears your function works fine, but you are passing the wrong values. From your code snippet:
formcd = rsRecordset1("formcd").value
typcd="formcd"
Call validatenull (typcd,Blank_field)
When you call 'validatenull', you are passing the variable 'typecd'. On the previous line, you populate this variable with a string, so it will never be null. It appears what you intended to pass was the variable 'formcd'. Your second call to 'validatenull' does the same thing. Here is your first call modified to use the other variable.
formcd = rsRecordset1("formcd").value
typcd="formcd"
Call validatenull (formcd,Blank_field)

Error "object doesn't support this property or method: dbrowser.GetRoProperty"

I was trying to run below script, but it's giving me an error that says:
object doesn't support this property or method: "dbrowser.GetRoProperty"
SystemUtil.Run "iexplore.exe","http://usps.com/"
Set dbrowser = description.Create
dbrowser ("micclass").value = "Browser"
dbrowser("openurl").value = "https://www.usps.com"
dbrowser("title").value = "USPS - The United States Postal Service (U.S. Postal Service)"
print(dbrowser.getroproperty("title"))
Your dbrowser object is of type Description not Browser you need to create a Browser object based on this description. Replace the last line with:
Print Browser(dbrowser).GetROProperty("title")
Note, there are two changes here
Using Browser(dbrowser)
Removing the parens from the print sub.
Edit: also note that descriptions are regular expressions by default so the parens in the title may cause problems, you should mark it as not regex.
dbrowser("title").RegularExpression = False
Description.Create is used to create a 0-based Properties collection object. The variable dbrowser is preceded by the Set statement. Usage of Set statement binds an object as a reference to another object. Therefore, dbrowser becomes an object reference to the description object represented by Description.Create
A description object does not have a stand-alone use, but coupled with the ChildObjects method, it becomes an extremely powerful approach in dealing with AUT’s objects .For More Info, check link
So the code should be like
SystemUtil.Run "iexplore.exe","http://usps.com/"
wait(10)
Set dbrowser = description.Create
dbrowser ("micclass").value = "Browser"
dbrowser("openurl").value = "https://www.usps.com"
dbrowser("title").value = "USPS.*" ''Using Regular Expression here
Set colObject = Desktop.ChildObjects( dbrowser )
Print (colObject(0).GetROProperty("title"))

Is Nothing comparison gives type mismatch

I am trying to check if the 'Listivew.Tag property is nothing'.
I used to do the 'Is Nothing' check universally for all scenarios as first check to avoid errors
Can someone explain how to do it in VB 6?
If Not .lvwLocation.Tag Is Nothing Then
'COMPANY
str = str & IIf(Len(.lvwLocation.Tag) > 0, " and u.location_id in " & .lvwLocation.Tag, "")
End If
Gives error 'type-mismatch'
Nothing is a valid value for Object variables, and Is is the way to compare object pointers.
But a VB6 control's Tag property is a String, and VB6's String type is not an Object; it's a primitive type. That means a String variable can't be assigned Nothing -- its emptiest possible value is the empty string. (And an Object variable can't be assigned a String value.) For strings just use the same equality/inequality/comparision operators that you use for other primitive (numeric/boolean/date) types:
If .lvwLocation.Tag <> "" Then ...
In VB6 it appears that using Is Nothing to compare Objects works, Every other data type that I tried did not. In .Net Nothing represents the default value of any data type and will work like you expect.
Dim test as Object
If Not test Is Nothing Then
/////
End If
Since it appears the data type of th Tag property in VB6 is a string. I would use something like:
If .lvwLocation.Tag <> "" Then
/////
End If

"Invalid use of Null" when using Str() with a Null Recordset field, but Str(Null) works fine

I'm banging my head against the wall on this one. I was looking at some old database reporting code written in VB6 and came across this line (the code is moving data from a "source" database into a reporting database):
rsTarget!VehYear = Trim(Str(rsSource!VehYear))
When rsSource!VehYear is Null, the above line generates an "Invalid use of Null" run-time error. If I break on the above line and type the following in the Immediate pane:
?rsSource!VehYear
It outputs Null. Fine, that makes sense. Next, I try to reproduce the error:
?Str(rsSource!VehYear)
I get an "Invalid use of Null" error.
However, if I type the following into the Immediate window:
?Str(Null)
I don't get an error. It simply outputs Null.
If I repeat the same experiment with Trim() instead of Str(), everything works fine. ?Trim(rsSource!VehYear) returns Null, as does ?Trim(Null). No run-time errors.
So, my question is, how can Str(rsSource!VehYear) possibly throw an "Invalid use of Null" error when Str(Null) does not, when I know that rsSource!VehYear is equal to Null?
Update: If I type the following in the Immediate window, it works as expected (no error occurs):
?Str(rsSource!VehYear.Value)
This outputs Null. Now, I know that rsSource!VehYear is actually an ADODB.Field instance, but Value is its default property, so Str should be operating on the Value property (which is Null). Even the error message ("Invalid use of Null") suggests that Str is receiving a Null parameter, but how can it treat Null differently in one case and not the other?
My only guess is the internal implementation of Str() is somehow failing to get the default property, and the "Invalid use of Null" error is happening for a different reason (something other than the parameter is causing the "Invalid use of Null", perhaps when it is trying to retrieve the default property from the Field object).
Does anyone have a more detailed, technical explanation for what is actually happening here?
In short:
?Str(rsSource!VehYear)
throws an "Invalid use of Null" error when rsSource!VehYear is Null, but
?Str(rsSource!VehYear.Value)
returns Null.
However, both Trim(rsSource!VehYear) and Trim(rsSource!VehYear.Value) return Null.
If you need a value other than a string, try using IsNull instead:
rsTarget!VehYear = IIf(IsNull(rsSource!VehYear), 0, rsSource!VehYear)
' Note 0 is the default value
The Str function will specifically check if a Null value is passed in and deal with it accordingly. When you pass in an object it attempts to convert the result of a default method to a String. The result of the default method is not passed into the Str method, but Field object is, so a check for the initial Null will fail. The Str function will continue to check the parameter type for datatypes that it supports when it realizes that it has an object, it will attempt to retrieve the default value. It doesn't re-attempt to deal with the default value as it did with the passed in argument, so the attempt to return a Null as a String will fail. It seems MS didn't expect a default value to be Null or any other invalid value for Str. For instance Str doesn't support an empty string either.
This was my workaround in the vb6-days:
rsTarget!VehYear = Trim(Str(rsSource!VehYear & ""))
the & "" will make sure there is allways at least an empty string to work with.
From memory, null database fields are Nothing (or possibly vbNull), which do not have the same rules applied to them as Null. You should just be able to do a quick check:
If (rsSource!VehYear Is Nothing) Then
' Null
Else
' Not null
End If

Resources