Solved

Programming Multiple Recordsets

Posted on 2002-07-25
4
143 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
Comment Utility
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
Comment Utility
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
Comment Utility
'''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
Comment Utility
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

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.
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…
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…

744 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