Ubound does not work (1 Viewer)

Wysy

Registered User.
Local time
Today, 09:43
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
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 11:43
Joined
Feb 28, 2001
Messages
26,999
Can you show us sample code of how you used it?
 

Wysy

Registered User.
Local time
Today, 09:43
Joined
Jul 5, 2015
Messages
333
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, 09:43
Joined
Oct 29, 2018
Messages
21,358
What do you get if you use UBound() in testarray2?
 

Gasman

Enthusiastic Amateur
Local time
Today, 16:43
Joined
Sep 21, 2011
Messages
14,044
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, 09:43
Joined
Jul 5, 2015
Messages
333
Actually everything works fine, it is only ubound value that fails
 

Wysy

Registered User.
Local time
Today, 09:43
Joined
Jul 5, 2015
Messages
333
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, 16:43
Joined
Sep 21, 2011
Messages
14,044
So what does your function look like now?
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 11:43
Joined
Feb 28, 2001
Messages
26,999
Try UBound(HlForInvoice, 2) and see what THAT returns.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 12:43
Joined
Feb 19, 2002
Messages
42,971
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, 03:43
Joined
Jan 20, 2009
Messages
12,849
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, 16:43
Joined
Sep 12, 2006
Messages
15,613
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, 19:43
Joined
Jan 28, 2022
Messages
310
Массив HLforInvoice становится общедоступным. Затем из другой формы
Code:
Public testarray()
    msgbox Ubound(HLforInvoice,1)
    msgbox Ubound(HLforInvoice,2)
    
end sub
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 00:43
Joined
May 7, 2009
Messages
19,169
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