Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Visual Basic > VB 6 Visual Basic 6 > VB How-To
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
VB How-To Ask your "How do I do this with VB?" questions in this forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB How-To section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old March 31st, 2004, 05:04 AM
Authorized User
 
Join Date: Mar 2004
Location: , , .
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to check if you have an empty array

Hi guys

I've got a calling function that calls a function called GetScenarioLog and it returns an array called ChangeLogArray. It then assigns the contents of the array to a listbox (ChangeLog) with multiple columns

ChangeLogArray = GetScenarioLog()
ChangeLog.Column() = ChangeLogArray


If no records are retrieved, no recordset will be assigned to the array and therefore the I get errors trying to assign an empty array to the listbox.

I need to be able to check if the array is empty before I try to assign the contents of the array to a listbox.

How do I check for this?

Many thanks

Cheers

Ciaran
Reply With Quote
  #2 (permalink)  
Old March 31st, 2004, 05:16 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , United Kingdom.
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

If its declared as an array:
If UBound(ChangeLogArray) > 0 then ' it has data.

If its declared as a variant:
If VarType(ChangeLogArray)> vbArray then ' it has data

hth
Phil
Reply With Quote
  #3 (permalink)  
Old March 31st, 2004, 05:27 AM
Authorized User
 
Join Date: Mar 2004
Location: , , .
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Phil

The ChangeLogArray is declared as a variant and the GetScenarioLog also returns a variant but I tried the VarType(ChangeLogArray)> vbArray and this is TRUE but in my watches (when debugging) there is no data.

Is this correct?

Cheers

Ciaran
Reply With Quote
  #4 (permalink)  
Old March 31st, 2004, 05:44 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , United Kingdom.
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hmm, that's unusual. Sounds like you'd better use both tests:
If VarType(ChangeLogArray)> vbArray then
    If UBound(ChangeLogArray) > 0 then
        ' it has data

I'm assuming here that the array is zero-based?
Reply With Quote
  #5 (permalink)  
Old March 31st, 2004, 06:00 AM
Authorized User
 
Join Date: Mar 2004
Location: , , .
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Phil

I get subscript is out of range errors when I use the UBound presumably because the array is not dimensioned.

Here is the GetScenarioLog function

Function GetScenarioLog() As Variant
Dim SQL_Records As Recordset
Dim SQLCollection As New Collection
Dim HoldingArray() As Variant

Set objComm = New ADODB.Command
objComm.ActiveConnection = objConn
objComm.CommandText = "select sc_owner, sc_updated_date, sc_username, sc_change_log from olapsys.users_scenario_log where sc_scenario = '" & AttachedAW & "' and sc_owner = '" & AWOwner & "' order by sc_updated_date desc"
objComm.CommandType = adCmdText

Set SQL_Records = New ADODB.Recordset
SQL_Records.ActiveConnection = objConn
Set SQL_Records.Source = objComm

SQL_Records.Open

If Not SQL_Records.BOF And Not SQL_Records.EOF Then
    HoldingArray = SQL_Records.GetRows
End If

GetScenarioLog = HoldingArray
Set SQL_Records = Nothing

End Function

If no records are returned in the recordset, then the .GetRows function is not used, therefore the HoldingArray has no data assigned to it and hence the ChangeLogArray in the calling function is the same.

If for instance there is one row with four columns in the users_scenario_log table, then .GetRows function will populate the HoldingArray like so

HoldingArray(0,0) = sc_owner
HoldingArray(1,0) = sc_updated_date
HoldingArray(2,0) = sc_username
HoldingArray(3,0) = sc_change_log

Cheers

Ciaran
Reply With Quote
  #6 (permalink)  
Old March 31st, 2004, 06:29 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , United Kingdom.
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

OIC its a dynamic array which hasn't been re-dimmed.

Looking at your code I think it would be better to change your function GetScenarioLog to return a boolean to indicate whether there are any records - if there are return them as a byref argument.

Alternatively you can redim the array to (0,0) if there are no records, then in your code check UBound(ChangeLogArray, 1)

hth
Phil
Reply With Quote
  #7 (permalink)  
Old March 31st, 2004, 06:53 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , United Kingdom.
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

Just looked back at some previous code I've written which returns a GetRows array, and in that I returned Null if there were no records. Then in the calling function I just check If Not IsNull(returnVar) Then ' there is data

Just another alternative. Basically, dynamic arrays are a pain!
Reply With Quote
  #8 (permalink)  
Old March 31st, 2004, 07:07 AM
Authorized User
 
Join Date: Mar 2004
Location: , , .
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Phil

Thanks for your comments

You mentioned that you returned Null if there were no records. Then in the calling function you just check If Not IsNull(returnVar)

How did you return Null, would you be able to post up your code so that I can see what you mean.

I firmly agree with your last statement - dynamic arrays are a pain!

Cheers for your help on this one!

Ciaran
Reply With Quote
  #9 (permalink)  
Old March 31st, 2004, 07:21 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , United Kingdom.
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

Null is a recognised keyword in VB, so you could just do something like this:
Code:
...
If Not SQL_Records.BOF And Not SQL_Records.EOF Then
    GetScenarioLog = SQL_Records.GetRows
Else
    GetScenarioLog = Null
End If

' remove this line:
GetScenarioLog = HoldingArray
...
rgds
Phil
Reply With Quote
  #10 (permalink)  
Old March 31st, 2004, 09:17 AM
Authorized User
 
Join Date: Mar 2004
Location: , , .
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Phil

Thanks for your help

Cheers

Ciaran
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Recordset Empty Check Coby Access VBA 2 April 27th, 2007 05:09 PM
best way to check for variable being empty crmpicco Classic ASP Basics 3 March 28th, 2006 12:11 PM
How to check for empty fields in a form. crmpicco Javascript How-To 7 February 15th, 2005 10:38 AM
check if empty hosefo81 Javascript How-To 12 March 9th, 2004 08:43 AM
How to check if collection is empty Ciarano Beginning VB 6 2 March 5th, 2004 08:11 AM



All times are GMT -4. The time now is 02:11 AM.


Powered by vBulletin®
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.