Solved

macro to return multiple rows from database

Posted on 2010-11-17
6
444 Views
Last Modified: 2012-05-10
hello,

I have a macro which is working fine as test case.But the query returns only one row.
I will change the query to return multiple rows.
I need to change the macro to update data in multiple rows { same column} the output will be same as grid layout in the TOAD.

The macro is as follows , please modify for above.

Private Sub cmdTest_Click()

'Defining variables
Dim cnOra As ADODB.Connection
Dim rsOra As ADODB.Recordset
Dim db_name As String
Dim UserName As String
Dim Password As String

Set cnOra = New ADODB.Connection
Set rsOra = New ADODB.Recordset

db_name = "KRIJGS"
UserName = "bertuser"
Password = "abcxyz123"

'Making an ODBC connection according to ADO
cnOra.Open "DSN=" + db_name + ";UID=" + UserName + ";PWD=" _
& Password + ";"
rsOra.CursorLocation = adUseServer

'Running a query
rsOra.Open "select global_name from global_name", cnOra, adOpenForwardOnly

'Passing on data from the recordset to a variable or cell.
'Notice that the column name or alias is used to address
'data in the recordset.
While Not rsOra.EOF
Worksheets("Sheet1").Range("A1") = rsOra![global_name]
rsOra.MoveNext
Wend

rsOra.Close
rsOra.Open "select sysdate from dual", cnOra, adOpenForwardOnly

While Not rsOra.EOF
Worksheets("Sheet1").Range("A2") = rsOra![sysdate]
rsOra.MoveNext
Wend

'Forgetting to close your connection will sometimes result in
'Dr.Watsons
rsOra.Close
cnOra.Close
Set rsOra = Nothing

End Sub

Best
bond
0
Comment
Question by:bond7
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 34155228
Replace your While loop with:

Worksheets("Sheet1").Range("A2").CopyFromRecordset rsOra

Open in new window


That will write the full contents of the recordset in one go.
0
 

Author Comment

by:bond7
ID: 34155398
Hi,

Just confirm my understanding.

While Not rsOra.EOF
Worksheets("Sheet1").Range("A2") = rsOra![sysdate]
rsOra.MoveNext
Wend

TO

While Not rsOra.EOF
Worksheets("Sheet1").Range("A2").CopyFromRecordset rsOra
rsOra.MoveNext
Wend

Please confirm.


0
 
LVL 92

Accepted Solution

by:
Patrick Matthews earned 250 total points
ID: 34155705
No, I mean replace:


While Not rsOra.EOF
Worksheets("Sheet1").Range("A2") = rsOra![sysdate]
rsOra.MoveNext
Wend

Open in new window



with:


Worksheets("Sheet1").Range("A2").CopyFromRecordset rsOra

Open in new window

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:bond7
ID: 34164332
Hello,


It worked just fine.one other doubt ?
My query is big and it is going to next line .

e.g. str = "select somethng from table where condition1
                and condition2 and ....................
                and condition3........."

How do I represent this in the vba ? Is there any option to write a query ?
0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 34164598
bond7,

With respect, I answered your original question.

If your SQL statement spans more than one line, then use a line continuator (i.e., the underscore).

Patrick
0
 

Author Closing Comment

by:bond7
ID: 34171699
Precise and on target solution.Thanks again.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Shell script to create broker configuration file using current broker Configuration, solely for purpose of backup on Linux. Script may need to be modified depending on OS-installation. Please deploy and verify the script in a test environment.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

718 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