Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to clear a Variant

Status
Not open for further replies.

tfstom

Programmer
Sep 28, 2002
190
US
I have a variant I am using to hold my recordset data.

Dim HolidayList
HolidayList = SqlRs.GetRows
lastRow = UBound(HolidayList, 2)

This works fine. But I want to be able to clear HolidayList. I tried setting it to NULL and "" and both give me an error when I try to do the "UBound" statement.

What I want is for it to return either a 0 or a Null, if possible.

Thanks,

Tom.
 
?? Set HolidayList = Nothing ??

I was standing in the park, wondering why frisbees got bigger as they came closer... then it hit me!
 
No, that doesn't work.

If I assign it to Nothing, it the variant is set up as something (in my case, it gets the data from rs.GetRows() method) and becomes an array.

If I have nothing set to it, it gives me an error about invalid for that type of object.

I get the same type of problem if I try to test for Null or "".

This is driving me crazy. The problem is that I need to prevent the user from pushing the button before I have done any reading of a file into the array (or it gets an error).

Tom.
 
Why not set the enable of the button to false until you have completed your reading ? or set a module level variable to stop the code in the button from executing ?

ie

Private blnReady As Boolean

Private Sub Button_Click

If blnReady Then
'Execute your code
End If

End Sub

Private Function Read_Your Stuff

blnReady = False
'Do whatever
blnReady = True

End Function

I was standing in the park, wondering why frisbees got bigger as they came closer... then it hit me!
 
You write that you want to "clear" the Variant. What exactly does this mean to you? To many VB programmers, it would mean to assign an Empty value to it, as in [tt]HolidayList = Empty[/tt]. (Review the VB Help on the VarType function to see what a Variant can contain, and click the See Also link to see the related functions.)

<< I tried setting it to NULL and "" and both give me an error when I try to do the "UBound" statement. >>
If it hurts when you do that, then don't do that. ;-) That is, don't execute the UBound function on the Variant unless you have verified that the Variant contains an array:

Code:
If IsArray(HolidayList) Then
    lastRow = UBound(HolidayList, 2)
Else
    ' What value to assign to lastRow is up to you.
End If

Alternatively, you could rearrange your logic and use a function like IsNull or IsEmpty, depending on what value you assigned to the Variant.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top