?
Solved

macro to return multiple rows from database

Posted on 2010-11-17
6
Medium Priority
?
450 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 93

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 93

Accepted Solution

by:
Patrick Matthews earned 1000 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
Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

 

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 93

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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
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…
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Suggested Courses

770 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