Solved

macro to return multiple rows from database

Posted on 2010-11-17
6
438 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

730 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