Ubound does not work

Wysy

Registered User.
Local time
Today, 15:52
Joined
Jul 5, 2015
Messages
333
Hi,
I have a modul that creates a dynamic array which is made public. The array works, contains the appropriate values.
However if i want to check ubound function it gives a result of 0. The array is not empty. Just curious why.
thank you
 
Can you show us sample code of how you used it?
 
Code:
Public Sub HorseSelectionForInvoice()
Dim hl As Recordset
Dim strHL As String

On Error Resume Next
Dim rc As Long

strHL = "SELECT HorseID FROM tbHorse WHERE Mark=-1;"
Set hl = CurrentDb.OpenRecordset(strHL)
hl.MoveLast
hl.MoveFirst
HLforInvoice = hl.GetRows(hl.RecordCount)
rc = hl.RecordCount - 1
End Sub

HLforInvoice array is made public. Then from another form

PHP:
Public testarray()
    msgbox Ubound(HLforInvoice)
end sub

The result is 0 though the array is not empty, It is checked by
Code:
Public testarray2()
for i=0 to rc
    testarr=HLforInvoice(0,i)
    msgbox testarr
next i
end sub
This shows the expected values.
 
What do you get if you use UBound() in testarray2?
 
Not played around much with arrays, but doesn't that last code show a 2 dimensional array, one row by many columns?
 
Actually everything works fine, it is only ubound value that fails
 
yes sure and used several times. it is only i am trying to find out why the ubound function fails
 
So what does your function look like now?
 
Try UBound(HlForInvoice, 2) and see what THAT returns.
 
Is the array declared in a form or in a module? If it is in a form, it is only available when the form is open. Are you sure the array is loaded at the time you reference it?
 
Is the array declared in a form or in a module? If it is in a form, it is only available when the form is open.
Plus it would need to be referred to via its form name if it is in a form module.

Do you have Option Explicit at the top of your modules? If you don't then the array on the module where you use UBound() would be instantiated as an empty array rather than an getting an error to say it doesn't exist when it can't be seen because you did not include the formname in the reference.
 
if there is one element in a (zero based) array, then ubound will return 0, as the highest element

eg, an array {1,2} will return a ubound of 1, not 2, and the elements are arrayname(0) and arrayname(1)
 
Массив HLforInvoice становится общедоступным. Затем из другой формы
Code:
Public testarray()
    msgbox Ubound(HLforInvoice,1)
    msgbox Ubound(HLforInvoice,2)
    
end sub
 
you can also Create yet another Public variable for the Ubound of array HLforInvoice:
on a Module:

Public HLUbound As Long


on your code:

Public Sub HorseSelectionForInvoice()
Dim hl As Recordset
Dim strHL As String

On Error Resume Next
Dim rc As Long

strHL = "SELECT HorseID FROM tbHorse WHERE Mark=-1;"
Set hl = CurrentDb.OpenRecordset(strHL)
hl.MoveLast
hl.MoveFirst
HLUbound = hl.RecordCount
HLforInvoice = hl.GetRows(hl.RecordCount)
rc = hl.RecordCount - 1
End Sub
 

Users who are viewing this thread

Back
Top Bottom