Solved

Programming Multiple Recordsets

Posted on 2002-07-25
4
145 Views
Last Modified: 2013-11-26
I'm using VB6 against an SQL Server database.
I have a form which has 6 combo boxes on a form, each loaded with ado recordsets.
To load each recordset, I have declared in the General Declarations section a recordset and an sql string for each one.
And, I use the same routine for each of the six comboboxes to load them:
Set RsSubCommCodes = New ADODB.Recordset
SqlObjects = "SELECT * FROM objects ORDER BY Description"
RsSubCommCodes.Open SqlObjects, Cn, adOpenStatic, adLockBatchOptimistic
    If Not (RsSubCommCodes.BOF And RsSubCommCodes.EOF) Then
        cboObjectDesc.AddItem Space
        RsSubCommCodes.MoveFirst
        While Not RsObjects.EOF
            cboObjectDesc.AddItem RsObjects!Description
            RsSubCommCodes.MoveNext
        Wend
    Else
        txtMessage = "You Are Not Assigned Any Agencies"
    End If
---------------------------------------------
My question is:
Is there an easier way to code for this?
I'm concerned because I think using too many recordsets is eating up all of the memory on the machine.
Can someone let me know if there is a better way?

Thanks in Advance
John

0
Comment
Question by:jtrapat1
  • 2
4 Comments
 
LVL 10

Accepted Solution

by:
aeklund earned 50 total points
ID: 7178207
You only have one recordset in your example.  But to make sure the recordset gets released along with the connection, add this to your code where <ConnectionObject> is your connection object:

Set RsSubCommCodes = New ADODB.Recordset
SqlObjects = "SELECT * FROM objects ORDER BY Description"
RsSubCommCodes.Open SqlObjects, Cn, adOpenStatic, adLockBatchOptimistic
Set <ConnectionObject>.ActiveConnection = Nothing
   If Not (RsSubCommCodes.BOF And RsSubCommCodes.EOF) Then
       cboObjectDesc.AddItem Space
       RsSubCommCodes.MoveFirst
       While Not RsObjects.EOF
           cboObjectDesc.AddItem RsObjects!Description
           RsSubCommCodes.MoveNext
       Wend
   Else
       txtMessage = "You Are Not Assigned Any Agencies"
   End If

RsSubCommCodes.Close
Set RsSubCommCodes = Nothing
<ConnectionObject>.Close
Set <ConnectionObject> = Nothing
0
 

Author Comment

by:jtrapat1
ID: 7178238
aeklund,

Thanks for the help.
Where should I put your code to close the recordsets and set the objects to Nothing?

Here's my situation:
The user clicks on a record in a datagrid and is brought to another form with that record in edit-mode.
Here is where I have the six comboboxes (all loaded with active recordsets).

Should I release the recordsets and connections when the user exits this form and returns to the datagrid?

Thanks

John
0
 
LVL 3

Expert Comment

by:Bahnass
ID: 7181739
'''SELECT * FROM objects ORDER BY Description

if U just open recordset for Description

U should use

SELECT Description FROM objects ORDER BY Description

also

adOpenStatic, adLockBatchOptimistic
   is not needed to fill a combo

U can use read only , forward only
thus U can save Ur memory

0
 
LVL 10

Expert Comment

by:aeklund
ID: 7185332
As soon as the user is done editing the records, then I suggest release the resources.... so the queryunload event of the edit form would be a good place to do so.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Access 2016 VB code 9 115
consolidate or create a new vba code for performing tasks 5 65
Add a task in Outlook from access 11 39
Excel Automation VBA 19 67
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.
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

813 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

10 Experts available now in Live!

Get 1:1 Help Now