 |
|
 |
yronium Level: Moderator

 Registered: 14-04-2002 Posts: 907
|
Re: how to change an ado connection's recordsource(made by adodc) at run time?
No, you do not need three Connection objects to open three recordsets on the same database.
For instance, assuming you are using a MSAccess database named MyDb.mdb, you need to use one single Connection object, say it's named conn1, to connect to it. You keep it open, so every time you need to retrieve some data from the MyDb.mdb file, you can open a recordset using the same open connection conn1. So, if you need to retrieve all the records from the table named Table1 you open a recordset, named rs1, and if you need also to get the first ten records from the Table2 table you open another recordset, rs2. If you don't need anymore to keep the first recordset object open, you can close and re-open it on second table's top 10 records.
But the connection is still the same: you will need another one if you have the need to operate into another .mdb file in the same time the first one is still connected. In this case you'll declare another Connection object and open it.
Say you got to copy in the table ExtractedItems, contained in the ExtractedData.mdb file, some records from ProvidingTable and AdditionalTable contained in the OriginalData.mdb file, and take a log of it into another table, LogTable, into the OriginalData.mdb file.
The pseudo-code would be like following: Dim cn1 As ADODB.Connection, cn2 As ADODB.Connection
Dim rs1 as ADODB.Recordset
Dim cnt, total
' when you need to connect to the first .mdb file, you open a connection
' (usually during application startup)
cn1.Open on the file "OriginalData.mdb"
' open the recordset on the first table to read
rs1.Open on table "ProvidingTable", with cn1
' now you need to connect to the second .mdb
cn2.Open on the file "ExtractedData.mdb"
Loop thru rs1
cn2 insert into ExtractedItems table, from the first table
rs1.MoveNext
cnt = cnt + 1 ' increase records counter
End loop
rs1.Close
' open the recordset on the second table to read
rs1.Open on table "AdditionalTable", with cn1
Loop again thru rs1
cn2 insert again into ExtractedItems table, from the second table
rs1.MoveNext
cnt = cnt + 1 ' increase records counter
End loop
rs1.Close
' get the updated recordcount from the ExtractedItems table
rs1.Open "Select Count ExtractedItems", with cn2
total = rs1.Fields(0).Value
rs1.Close and Nothing ' we don't need anymore the recordset
cn2.Close and Nothing ' no need to keep the second .mdb connected anymore
cn1 insert into LogTable(RecordsAffected, TotalExtracted, Date) values (cnt, total, CurrentDate)
' when you finished working on the first .mdb, you can close and destroy it
' (usually when unloading the application)
cn1.Close and Nothing | Hope it's clear. There are many other samples on this site. Have a search.
About the chart I don't know it as I never used it, but the connection code seem to be okay, so I argue your problem belongs from the chart control. Hope somebody else can say more.
____________________________
Real Programmer can count up to 1024 on his fingers
|
|
28-02-2007 at 01:07 AM |
|
|
aak_neo Level: Scholar

 Registered: 11-02-2007 Posts: 30
|
Re: how to change an ado connection's recordsource(made by adodc) at run time?
Thanx for the elaborate example. it made my idea of the recordset
much more clearer.but it hasn't quite solved my problem.Actually i didn't frame my question properly? i.e
can i have multiple recordsets to the same table?
In ur example u are either working on two different db or two different tables of same db. but nowhere u have shown a different recordsets to same table. i know u will say then i should use other
coding manipulations of same recordset to retreive data but a chart control does nothing but read a datasource(recordset) directly. so my question is simple that,
Is it possible to have two recordsets rs1,rs2 such that they have same connection string & access the same table in the db?
sql1 = "SELECT * FROM chdetails WHERE ID BETWEEN " & start_id & " AND " & end_id
sql2 = "SELECT * FROM chdetails WHERE ID=2582"
rs1.Open sql1, connString, , , adCmdText
rs2.Open sql2, connString, , , adCmdText
notice both access same table chdetails.
sorry for the trouble but it gave me a method to transfer data from one db to other. 
____________________________
You never see past the choices you don't understand
|
|
28-02-2007 at 05:36 AM |
|
|
yronium Level: Moderator

 Registered: 14-04-2002 Posts: 907
|
Re: how to change an ado connection's recordsource(made by adodc) at run time?
quote: aak_neo wrote:Is it possible to have two recordsets rs1,rs2 such that they have same connection string & access the same table in the db?
sql1 = "SELECT * FROM chdetails WHERE ID BETWEEN " & start_id & " AND " & end_id
sql2 = "SELECT * FROM chdetails WHERE ID=2582"
rs1.Open sql1, connString, , , adCmdText
rs2.Open sql2, connString, , , adCmdText
notice both access same table chdetails.
Yes, you can, of course. But you got to keep in mind some details: if you edit a record into rs1, the record is not automatically updated in rs2. In order to do it you could use a clone of rs1, by Set rs2 = rs1.Clone(LockType_argument) but it has its limits too, and there are cases in which developers prefer to use two different recordsets on the same table or even on the same query.
You have to be aware of this if you mind to modify data into the recordset, as data are updated into the table at once you move on another record, but not on the other recordset so you have to explicitly requery it too.
You can find more details on Clone method on ADO's Online Help.
Hope it helps
____________________________
Real Programmer can count up to 1024 on his fingers
|
|
28-02-2007 at 02:03 PM |
|
|
yronium Level: Moderator

 Registered: 14-04-2002 Posts: 907
|
Re: how to change an ado connection's recordsource(made by adodc) at run time?
quote: aak_neo wrote:...but somehow the chart doesn't show this & gets blanked out completely so i suppose i will be stuck with the same 3 adodc's as
1.charts not working for the new recordset as flex grids do
2.my table is updated constantly so updating all recordsets becomes a problem.
Unfortunately, I can't help you much with chart control, as I never used it, but I'm quite sure it's the only responsible of misworking, as you can load and view data without it.
quote: ...also,a clone is just the same recordset so i don't think it can show something different than the parent recordset like only select no. of fields, records etc.
Yes, it's the same recordset, but sometimes developers use to filter it, so you could show all the records aside of only the criteria matching ones. Indeed sometimes programmers like to compare two recordset open on the same query, to verify if there are some modified records: in some cases, you can do it with a clone, as well.
quote: i m new to the forum so plz tell me if the topic is solved...
Well, the topic should be solved when you got rid of all the initial problems. But if you identified a different point of view, such as the chart control, maybe you better open a new thread with another topic.
Anyway, marking a topic as Solved it's not obligatory.
Hope somebody can help you more on chart control matter, and hope we'll talk again.
____________________________
Real Programmer can count up to 1024 on his fingers
|
|
01-03-2007 at 05:44 PM |
|
|
|
|
 |
 |