Solved

Declare Recordsets on the fly VB6?

Posted on 2009-05-15
5
585 Views
Last Modified: 2012-05-07
Hi, I'm trying to Declare and execute recordsets inside a loop.  I can't declare them all upfront because I don't know how many there might be.  Is it possible to pass a string as the name of a recordset?

I cleaned up the code to execute 10 times, but like I said, this isn't really the case.  The loop would execute a varying number of times.

Thanks
Dim intCounter As Integer

Dim strRS As String

Do Until intCounter = 10
 

    strRS = "rs" & intCounter

    

    Dim (strRS) As Recordset

    (strRS).Open strcon

   intcounter = intcounter + 1

Loop

Open in new window

0
Comment
Question by:zigmpls
5 Comments
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 43 total points
ID: 24399996
If you have to keep all the recordsets open and hence cannot reuse the Recordset variable, you could declare an unsized array and ReDim it.

Dim intCounter As Integer
Dim RS () As Recordset

Do Until intCounter = 10
    redim preserve RS(intCounter)
   
    RS(intCounter).Open strcon
   intcounter = intcounter + 1
Loop
0
 
LVL 20

Assisted Solution

by:hes
hes earned 41 total points
ID: 24400224
Not to get into the why, that you need that open recordsets, I personally can't remember ever having to have open more than 2. But anyway, remember you are going to have to keep track of all of them no matter how you do open them, to close each one of them.
0
 

Author Comment

by:zigmpls
ID: 24400540
Thanks both for your feedback.  You're right; I was making this way too complicated.  I just declared one recordset before starting the loop, kept the information in an array and then closed the recordset at each iteration of the loop.


0
 
LVL 6

Assisted Solution

by:nirojexpert
nirojexpert earned 41 total points
ID: 24404785
also note that opening recordsets for each record (in a loop)  is not a good practise.
if possible, try to combine the sql so that you do not have to execute them in a loop. YOu should try to lessen the number of server requests.
maybe your first record set gets you a filtered list and inside the loop, you are getting additional value, setting value, etc.
you should attempt to combine the sql which gets you the list and what you want to do with time.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

705 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

18 Experts available now in Live!

Get 1:1 Help Now