Solved

vba getting proper uboundin an array

Posted on 2016-08-08
6
47 Views
Last Modified: 2016-08-11
access vba 2010

After i get a recordset count
I'm trying to put the items into an array.
The array keeps coming up with only one item.
even though i have 10 records ?
' put the items into an array for emailing each row.
                    dArrp = rs1.GetRows()
                    
                    ' just in case i need it
d = rs1.RecordCount

lngCount = UBound(dArrp, 2) + 1
                                
 For i = LBound(dArrp, 2) To UBound(dArrp, 2) ' my ubound always ends up being "one"  

Open in new window



Thanks
fordraiders
0
Comment
Question by:fordraiders
6 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 41748256
I found that to happen if the recordset is on the last record.
so, please try to use :
rs1.MoveFirst()
dArrp = rs1.GetRows()

Open in new window

1
 
LVL 2

Expert Comment

by:Antonio Salva Ripoll
ID: 41748258
Hi.

First you mustcheck if the recordset has data:

if not (rs1.bof and rs1.eof) then

Open in new window


Later you must fill the recordset
rs1.movelast
rs1.movefirst

Open in new window

and then you can get anytjing from the recordset.

Best regards.

Antonio (Barcelona, Spain)
1
 
LVL 49

Expert Comment

by:Gustav Brock
ID: 41748394
GetRows need to know how many records to pull. Thus, to get all records:
rs1.MoveLast
rs1.MoveFirst
dArrp = rs1.GetRows(rs.RecordCount)
                    
lngCount = UBound(dArrp, 2) + 1

Open in new window

/gustav
1
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 41748400
Gustav, I will need to disagree...
https://msdn.microsoft.com/en-us/library/ms675120(v=vs.85).aspx
Rows
    Optional. A GetRowsOptionEnum value that indicates the number of records to retrieve. The default is adGetRowsRest.

which means that if you are on the last record, it will only get that one
using MoveFirst, it will get "the rest", no need to specify the number of records you want.

to test .BOF and .EOF is not needed, because if you "moved" the cursor to the last record, you know you have some records indeed.
0
 
LVL 30

Expert Comment

by:hnasr
ID: 41748689
Unable to reproduce the issue:
All the following give the same result:
    Dim trs1 As Recordset
    Dim ta As Variant
    
    Set trs1 = CurrentDb.OpenRecordset("a")
    trs1.MoveLast
    ta = trs1.GetRows()
    Debug.Print UBound(ta)
    
    Set trs1 = Nothing
    Set trs1 = CurrentDb.OpenRecordset("a")
    ta = trs1.GetRows()
    Debug.Print UBound(ta)
    
    Set trs1 = Nothing
    Set trs1 = CurrentDb.OpenRecordset("a")
    ta = trs1.GetRows(trs1.RecordCount)
    Debug.Print UBound(ta)

Open in new window

0
 
LVL 3

Author Closing Comment

by:fordraiders
ID: 41753382
thnx
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now