PC Review


Reply
Thread Tools Rate Thread

Bring Queries from Access into Excel

 
 
=?Utf-8?B?SmV6?=
Guest
Posts: n/a
 
      25th Oct 2006
Hi, This is my vb code to bring data from access to excel. How is it possible
to duplicate this and bring in more than 1 sql and paste elsewhere on the
sheet? this below works for the one sql written, how can I write another sql
and paste on another cell in the sheet?

Sub GetASVNDistrict()

On Error GoTo Err:
strDWFilePath = "H:\NCHO\Housing Services\Data Warehouse\HSG Data
Warehouse.mdb"

Set cnnDW = New ADODB.Connection
Set rsDW = New ADODB.Recordset

Sheet4.Range("B5:BB22").ClearContents

cnnDW.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & strDWFilePath & ";"

sQRY = "TRANSFORM Count([qryNoAccess(byAppt)].WRNumber) AS
CountOfWRNumber " & _
"SELECT [qryNoAccess(byAppt)].CouncilName " & _
"FROM [qryNoAccess(byAppt)] " & _
"WHERE ((([qryNoAccess(byAppt)].BANumber) <> 'HSG0008 20') And
(([qryNoAccess(byAppt)].AppointmentOutcomeID) = 'N') And
(([qryNoAccess(byAppt)].ActionTypeID) = 'AS')) " & _
"GROUP BY [qryNoAccess(byAppt)].CouncilName " & _
"PIVOT [qryNoAccess(byAppt)].Week"

rsDW.CursorLocation = adUseClient
rsDW.Open sQRY, cnnDW, adOpenStatic, adLockReadOnly

Application.ScreenUpdating = False
Sheet4.Range("B5").CopyFromRecordset rsDW

rsDW.Close
cnnDW.Close

Set rsDW = Nothing
Set cnnDW = Nothing

Exit Sub

Err:
MsgBox "The following error has occured-" & vbCrLf & vbCrLf & VBA.Error,
vbCritical, "HSG NA Trending"
MsgBox VBA.Err

End Sub

thanks :-)

Jez

 
Reply With Quote
 
 
 
 
=?Utf-8?B?Q2hhZEY=?=
Guest
Posts: n/a
 
      25th Oct 2006
Hey Jez.

You might want to consider the following...

re-define your subroutine to take 2 arguements...

like so,
Sub GetASVNDistrict(sQRY as String, targetCells as Range )

....

end sub

where sQRY is the SQL statement you list down below.
Whatever calls this routine needs to put this together instead of
in the body of this sub.

Same thing goes for the parameter targetCells ... pass the routne
a reference to a cell like this :

call GetASVNDistrict(sQRY, Sheet4.Range("B5") ) ' this is just an
example...

The relevant statements in the original body are :

> rsDW.Open sQRY, cnnDW, adOpenStatic, adLockReadOnly
>
> Application.ScreenUpdating = False
> Sheet4.Range("B5").CopyFromRecordset rsDW


change these statements to reflect the input parameters... like so,

rsDW.Open sQRY, cnnDW, adOpenStatic, adLockReadOnly

Application.ScreenUpdating = False
targetCells.CopyFromRecordset rsDW


you're utilizing the .CopyFromRecordset sub which you can apply to
*any* range. In the example you cite, it goes to a static location -
Sheet4.Range("B5") ... if you cange it to an input parameter, you can
tell the sub to reference anywhere you want. The tradeoff is - the calling
function (sub) has to supply that info. (same thing goes for the SQL
statement.)

Hope this helps,
Chad






"Jez" wrote:

> Hi, This is my vb code to bring data from access to excel. How is it possible
> to duplicate this and bring in more than 1 sql and paste elsewhere on the
> sheet? this below works for the one sql written, how can I write another sql
> and paste on another cell in the sheet?
>
> Sub GetASVNDistrict()
>
> On Error GoTo Err:
> strDWFilePath = "H:\NCHO\Housing Services\Data Warehouse\HSG Data
> Warehouse.mdb"
>
> Set cnnDW = New ADODB.Connection
> Set rsDW = New ADODB.Recordset
>
> Sheet4.Range("B5:BB22").ClearContents
>
> cnnDW.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
> "Data Source=" & strDWFilePath & ";"
>
> sQRY = "TRANSFORM Count([qryNoAccess(byAppt)].WRNumber) AS
> CountOfWRNumber " & _
> "SELECT [qryNoAccess(byAppt)].CouncilName " & _
> "FROM [qryNoAccess(byAppt)] " & _
> "WHERE ((([qryNoAccess(byAppt)].BANumber) <> 'HSG0008 20') And
> (([qryNoAccess(byAppt)].AppointmentOutcomeID) = 'N') And
> (([qryNoAccess(byAppt)].ActionTypeID) = 'AS')) " & _
> "GROUP BY [qryNoAccess(byAppt)].CouncilName " & _
> "PIVOT [qryNoAccess(byAppt)].Week"
>
> rsDW.CursorLocation = adUseClient
> rsDW.Open sQRY, cnnDW, adOpenStatic, adLockReadOnly
>
> Application.ScreenUpdating = False
> Sheet4.Range("B5").CopyFromRecordset rsDW
>
> rsDW.Close
> cnnDW.Close
>
> Set rsDW = Nothing
> Set cnnDW = Nothing
>
> Exit Sub
>
> Err:
> MsgBox "The following error has occured-" & vbCrLf & vbCrLf & VBA.Error,
> vbCritical, "HSG NA Trending"
> MsgBox VBA.Err
>
> End Sub
>
> thanks :-)
>
> Jez
>

 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Transferspreadsheet bring empty data from excel to access sunil vedula Microsoft Access External Data 4 21st May 2009 03:36 AM
Transferspreadsheet bring empty data from excel to access sunil vedula Microsoft Access External Data 0 20th May 2009 06:43 PM
retrieve data from excel and bring into access dnr Microsoft Access VBA Modules 2 10th Jun 2008 04:06 PM
how to bring a value from access into excel based on criteria dreamkeeper Microsoft Excel Programming 0 30th Oct 2007 10:31 PM
Bring together 2 queries =?Utf-8?B?YmRlaG5pbmc=?= Microsoft Access Queries 5 30th Jul 2004 05:45 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:19 PM.