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
Solved

macro to return multiple rows from database

Posted on 2010-11-17
6
435 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

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.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

856 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