Solved

macro to return multiple rows from database

Posted on 2010-11-17
6
409 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
  • 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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.
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…

747 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

13 Experts available now in Live!

Get 1:1 Help Now