Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 158
Default ADO question

My VBA code which makes an ADO connection to an Excel 2007
file typically looks like this:

'================================================= ==========
' Create the Recorset object and run the query.

szSQL = "SELECT * FROM [Sheet1$A1:A1]"

Set rsData = New ADODB.Recordset
rsData.Open szSQL, szConnect, adOpenForwardOnly, adLockReadOnly, adCmdText

' Extract data from rsData recordset object

rsData.Close
Set rsData = Nothing
'================================================= =========


So, my question is, once I have an open ADO connection, is there any way to
make
another SQL query before running the "rsData.Close" command?? Or, do I
simply
need to close the connection and then run the Open command with a new
"szSQL"
string command??

thanks!


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 226
Default ADO question

Robert Crandal pretended :
My VBA code which makes an ADO connection to an Excel 2007
file typically looks like this:

'================================================= ==========
' Create the Recorset object and run the query.

szSQL = "SELECT * FROM [Sheet1$A1:A1]"

Set rsData = New ADODB.Recordset
rsData.Open szSQL, szConnect, adOpenForwardOnly, adLockReadOnly, adCmdText

' Extract data from rsData recordset object

rsData.Close
Set rsData = Nothing
'================================================= =========


So, my question is, once I have an open ADO connection, is there any way to
make
another SQL query before running the "rsData.Close" command?? Or, do I
simply
need to close the connection and then run the Open command with a new "szSQL"
string command??

thanks!


You can leave the connection open and run new SQLs whenever you need
them. So then, you could pull data from several worksheets and store it
in separate recordsets if desired.

In this case, you need to close the connection when your project is
shutting down.

--
Garry

Free usenet access at http://www.eternal-september.org
ClassicVB Users Regroup! comp.lang.basic.visual.misc


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 158
Default ADO question

That's the problem.... I don't know HOW to run a new SQL command on
an open connection. What is the command to run a new SQL? Is there
some sort of "Execute" method that runs from a recordset object or
something?

Thanks!


"GS" wrote in message
...

You can leave the connection open and run new SQLs whenever you need them.
So then, you could pull data from several worksheets and store it in
separate recordsets if desired.

In this case, you need to close the connection when your project is
shutting down.


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 82
Default ADO question

All you should need to do is close the existing recordset, but don't
close the connection and use code similar to what
you've already posted for the next query.



szSQL = "SELECT * FROM [Sheet1$A1:A1]"


Set rsData = New ADODB.Recordset
rsData.Open szSQL, szConnect, adOpenForwardOnly, adLockReadOnly,
adCmdText


' Extract data from rsData recordset object


rsData.Close

szSQL = "SELECT * FROM [Sheet1$A2:A2]"

rsData.Open szSQL, szConnect, adOpenForwardOnly, adLockReadOnly,
adCmdText

' Extract data from rsData recordset object


rsData.Close

....

Set rsData = Nothing

By the way why does your SQL appear to only be for one cell?

It doesn't make a lot of sense to use ADO to extract such small
amounts of data from a worksheet.
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 158
Default ADO question

In your code below, when you call "rsData.Close" the first time,
doesn't that close the connection?? It looks like you are
closing the first connection, then making a new connection with
a new query. Is that right?

BTW, I will only be extracting data from a couple cells on different
sheets, thats why I am using references line "$A1:A1" or "D12:12".
I was hoping to extract this data as quickly as possible....so I figured
it would make sense (to me) to merely extract the individual data
that I need without closing the connection to the same file with a
series of "Open" and "Close" commands. I will also be extracting the
same data from a couple hundred Excel 2007 files, so I am trying
to consider all possible ways to speed up my search process.


"norie" wrote in message
...
All you should need to do is close the existing recordset, but don't
close the connection and use code similar to what
you've already posted for the next query.



szSQL = "SELECT * FROM [Sheet1$A1:A1]"


Set rsData = New ADODB.Recordset
rsData.Open szSQL, szConnect, adOpenForwardOnly, adLockReadOnly,
adCmdText


' Extract data from rsData recordset object


rsData.Close

szSQL = "SELECT * FROM [Sheet1$A2:A2]"

rsData.Open szSQL, szConnect, adOpenForwardOnly, adLockReadOnly,
adCmdText

' Extract data from rsData recordset object


rsData.Close

...

Set rsData = Nothing

By the way why does your SQL appear to only be for one cell?

It doesn't make a lot of sense to use ADO to extract such small
amounts of data from a worksheet.




  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 236
Default ADO question

I could be wrong, but the bit you're missing seem to be another object
varialbe of type ADODB.Connection
You should go like:
Dim cn as ADODB.Connection
Set cn= new ADODB.Connection
cn.open (put all the connection bits in here)
Now - you've got your connection object open.
Then, when you run your
rst.open sql
instead of the szConnect (which i'm guessing is just a string and not
an object) you'd use the newly created (and opened) cn object.
Then, when your'e closing your rst - the connection object will retain
it's state (open in this case).

That is, if i havnet misunderstood your setup.



On Sep 30, 11:36*pm, "Robert Crandal" wrote:
In your code below, when you call "rsData.Close" the first time,
doesn't that close the connection?? * It looks like you are
closing the first connection, then making a new connection with
a new query. *Is that right?

BTW, I will only be extracting data from a couple cells on different
sheets, thats why I am using references line "$A1:A1" or "D12:12".
I was hoping to extract this data as quickly as possible....so I figured
it would make sense (to me) to merely extract the individual data
that I need without closing the connection to the same file with a
series of "Open" and "Close" commands. * I will also be extracting the
same data from a couple hundred Excel 2007 files, so I am trying
to consider all possible ways to speed up my search process.

"norie" wrote in message

...



All you should need to do is close the existing recordset, but don't
close the connection and use code similar to what
you've already posted for the next *query.


szSQL = "SELECT * FROM [Sheet1$A1:A1]"


Set rsData = New ADODB.Recordset
rsData.Open szSQL, szConnect, adOpenForwardOnly, adLockReadOnly,
adCmdText


' Extract data from rsData recordset object


rsData.Close


szSQL = "SELECT * FROM [Sheet1$A2:A2]"


rsData.Open szSQL, szConnect, adOpenForwardOnly, adLockReadOnly,
adCmdText


' Extract data from rsData recordset object


rsData.Close


...


Set rsData = Nothing


By the way why does your SQL appear to only be for one cell?


It doesn't make a lot of sense to use ADO to extract such small
amounts of data from a worksheet.- Hide quoted text -


- Show quoted text -


  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 82
Default ADO question

Robert

No, closing the <brecordset</b does not close the connection - well
it shouldn't be.

I'm also still a bit confused why you would want to use ADO to extract
only a few cells from a worksheet.

The easiest, probably the quickest and maybe the least problematic way
to do that would be to open the workbook, get the data, close the
workbook...

  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 226
Default ADO question

Hi Robert,
Sorry I haven't been able to reply due to being hospitalized. I'm out
on a day pass right now and so I wanted to let you know.

Glad to see you're getting help. Hope you're able to resolve something.

I'll look in when I can...

regards,

--
Garry

Free usenet access at http://www.eternal-september.org
ClassicVB Users Regroup! comp.lang.basic.visual.misc


  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 158
Default ADO question

Hi bud.... so sorry to hear about your hospitalization.
Excel programming probably hospitalized you, huh?
Just kidding.... Hope you get better! 8)

"GS" wrote in message
...
Hi Robert,
Sorry I haven't been able to reply due to being hospitalized. I'm out on a
day pass right now and so I wanted to let you know.

Glad to see you're getting help. Hope you're able to resolve something.

I'll look in when I can...

regards,

--
Garry

Free usenet access at http://www.eternal-september.org
ClassicVB Users Regroup! comp.lang.basic.visual.misc



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Excel 2007 Macro/VB Question DDE Question MadDog22 Excel Worksheet Functions 1 March 10th 10 01:47 AM
Good morning or good evening depending upon your location. I want to ask you the most important question of your life. Your joy or sorrow for all eternity depends upon your answer. The question is: Are you saved? It is not a question of how good davegb Excel Programming 1 May 6th 05 06:35 PM
Good morning or good evening depending upon your location. I want to ask you the most important question of your life. Your joy or sorrow for all eternity depends upon your answer. The question is: Are you saved? It is not a question of how good you [email protected] Excel Programming 0 April 27th 05 07:46 PM
Good morning or good evening depending upon your location. I want to ask you the most important question of your life. Your joy or sorrow for all eternity depends upon your answer. The question is: Are you saved? It is not a question of how good you [email protected] Excel Programming 23 April 23rd 05 09:26 PM
Good morning or good evening depending upon your location. I want to ask you the most important question of your life. Your joy or sorrow for all eternity depends upon your answer. The question is: Are you saved? It is not a question of how good you [email protected] Excel Programming 0 April 22nd 05 03:30 PM


All times are GMT +1. The time now is 09:48 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"