 |
| 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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

March 31st, 2004, 05:04 AM
|
|
Authorized User
|
|
Join Date: Mar 2004
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

March 31st, 2004, 05:16 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
|
|
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
|
|

March 31st, 2004, 05:27 AM
|
|
Authorized User
|
|
Join Date: Mar 2004
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

March 31st, 2004, 05:44 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
|
|
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?
|
|

March 31st, 2004, 06:00 AM
|
|
Authorized User
|
|
Join Date: Mar 2004
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

March 31st, 2004, 06:29 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
|
|
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
|
|

March 31st, 2004, 06:53 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
|
|
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!
|
|

March 31st, 2004, 07:07 AM
|
|
Authorized User
|
|
Join Date: Mar 2004
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

March 31st, 2004, 07:21 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
|
|
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
|
|

March 31st, 2004, 09:17 AM
|
|
Authorized User
|
|
Join Date: Mar 2004
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi Phil
Thanks for your help
Cheers
Ciaran
|
|
 |