Ubound does not work (1 Viewer)

Wysy

Registered User.
Local time
Today, 11:36
Joined
Jul 5, 2015
Messages
292
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
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 13:36
Joined
Feb 28, 2001
Messages
23,151
Can you show us sample code of how you used it?
 

Wysy

Registered User.
Local time
Today, 11:36
Joined
Jul 5, 2015
Messages
292
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.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 11:36
Joined
Oct 29, 2018
Messages
19,165
What do you get if you use UBound() in testarray2?
 

Gasman

Enthusiastic Amateur
Local time
Today, 19:36
Joined
Sep 21, 2011
Messages
10,853
Not played around much with arrays, but doesn't that last code show a 2 dimensional array, one row by many columns?
 

Wysy

Registered User.
Local time
Today, 11:36
Joined
Jul 5, 2015
Messages
292
Actually everything works fine, it is only ubound value that fails
 

Wysy

Registered User.
Local time
Today, 11:36
Joined
Jul 5, 2015
Messages
292
yes sure and used several times. it is only i am trying to find out why the ubound function fails
 

Gasman

Enthusiastic Amateur
Local time
Today, 19:36
Joined
Sep 21, 2011
Messages
10,853
So what does your function look like now?
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 13:36
Joined
Feb 28, 2001
Messages
23,151
Try UBound(HlForInvoice, 2) and see what THAT returns.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 14:36
Joined
Feb 19, 2002
Messages
36,871
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?
 

Galaxiom

Super Moderator
Staff member
Local time
Tomorrow, 04:36
Joined
Jan 20, 2009
Messages
12,541
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.
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 19:36
Joined
Sep 12, 2006
Messages
14,901
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)
 

SHANEMAC51

Active member
Local time
Today, 21:36
Joined
Jan 28, 2022
Messages
294
Массив HLforInvoice становится общедоступным. Затем из другой формы
Code:
Public testarray()
    msgbox Ubound(HLforInvoice,1)
    msgbox Ubound(HLforInvoice,2)
    
end sub
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 02:36
Joined
May 7, 2009
Messages
16,810
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

Top Bottom