Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 36
Default VB code to run a Query in Access and paste results onto an Excelworksheet

I want to create an application where the user makes a selection from
a 'drop-down' in Excel that triggers a Query in Access, copies the
result of the query and pastes it onto a sheet-tab in the Excel
Workbook.

Is this possible?

If so can anyone help me understand the code I would use?

We are XP using Ecel 2003 and Access 2003

Thank you,

Tony
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 245
Default VB code to run a Query in Access and paste results onto an Excel w

Here is the code for the query

Option Explicit
Sub AccessQuery()
Dim DAO As DAO.DBEngine
Dim MyDb12 As DAO.Database
Dim RS_RSQy As DAO.Recordset
Dim i As Variant, x As Long
Dim WS As Worksheet
Dim Count As Integer

Application.ScreenUpdating = False
Set DAO = New DAO.DBEngine
Set MyDb12 = DAO.OpenDatabase("C:\Documents and Settings\Desktop\db12.mdb")
Set RS_RSQy = MyDb12.OpenRecordset("YourRecodset")
Set WS = ActiveWorkbook.Worksheets(2)
Count = RS_PSQC.Fields.Count

For i = 0 To Count - 1
WS.Cells(1, i + 1).Value = RS_RSQy.Fields(i).Name
Next

WS.Range("A2").CopyFromRecordset RS_RSQy

Application.ScreenUpdating = True
Set RS_RSQy = Nothing
Set MyDb12 = Nothing
Set DAO = Nothing
End Sub


"Tony Bender" wrote:

I want to create an application where the user makes a selection from
a 'drop-down' in Excel that triggers a Query in Access, copies the
result of the query and pastes it onto a sheet-tab in the Excel
Workbook.

Is this possible?

If so can anyone help me understand the code I would use?

We are XP using Ecel 2003 and Access 2003

Thank you,

Tony

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9,101
Default VB code to run a Query in Access and paste results onto an Exc

The easy way of defining the ("YourRecodset") is to manually perform the
query while recording a macro

from worksheet menu
1) Tools - Macro - Record New Macro
2) Data - Import External Data - New Database Query

Set up the Query selecting all your options

3) Tools - Macro - Stop Recording


Modify the recorded macro as necessarry
"Office_Novice" wrote:

Here is the code for the query

Option Explicit
Sub AccessQuery()
Dim DAO As DAO.DBEngine
Dim MyDb12 As DAO.Database
Dim RS_RSQy As DAO.Recordset
Dim i As Variant, x As Long
Dim WS As Worksheet
Dim Count As Integer

Application.ScreenUpdating = False
Set DAO = New DAO.DBEngine
Set MyDb12 = DAO.OpenDatabase("C:\Documents and Settings\Desktop\db12.mdb")
Set RS_RSQy = MyDb12.OpenRecordset("YourRecodset")
Set WS = ActiveWorkbook.Worksheets(2)
Count = RS_PSQC.Fields.Count

For i = 0 To Count - 1
WS.Cells(1, i + 1).Value = RS_RSQy.Fields(i).Name
Next

WS.Range("A2").CopyFromRecordset RS_RSQy

Application.ScreenUpdating = True
Set RS_RSQy = Nothing
Set MyDb12 = Nothing
Set DAO = Nothing
End Sub


"Tony Bender" wrote:

I want to create an application where the user makes a selection from
a 'drop-down' in Excel that triggers a Query in Access, copies the
result of the query and pastes it onto a sheet-tab in the Excel
Workbook.

Is this possible?

If so can anyone help me understand the code I would use?

We are XP using Ecel 2003 and Access 2003

Thank you,

Tony

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 36
Default VB code to run a Query in Access and paste results onto an Excelw

When I try and run the macro here is the error msg I get:

Variable not defined:

Count = RS_PSQC.Fields.Count

Does this mean I need to add this to the other DIM entries? If so how
do I phrase it.

Thank you,




  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 36
Default VB code to run a Query in Access and paste results onto an Exc

Joel,

When I do this which portion is the "RecordSet"?


Here is what I get when I record the macro:

With ActiveSheet.QueryTables.Add(Connection:=Array( _
"OLEDB;Provider=Microsoft.Jet.OLEDB.4.0;Password=" """;User
ID=Admin;Data Source=C:\Data\TripMission 1.mdb;Mode=ReadWrite;Exte" _
, _
"nded Properties="""";Jet OLEDB:System database="""";Jet
OLEDB:Registry Path="""";Jet OLEDB:Database Password="""";Jet
OLEDB:Engine Type=" _
, _
"5;Jet OLEDB:Database Locking Mode=1;Jet OLEDB:Global Partial
Bulk Ops=2;Jet OLEDB:Global Bulk Transactions=1;Jet OLEDB:New Datab" _
, _
"ase Password="""";Jet OLEDB:Create System Database=False;Jet
OLEDB:Encrypt Database=False;Jet OLEDB:Don't Copy Locale on Compact=F"
_
, "alse;Jet OLEDB:Compact Without Replica Repair=False;Jet
OLEDB:SFP=False"), _
Destination:=Range("A1"))
.CommandType = xlCmdTable
.CommandText = Array("qryTRIPGROUP1")
.Name = "TripMission 1"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.SourceDataFile = "C:\Data\TripMission 1.mdb"
.Refresh BackgroundQuery:=False
End With

Thank you,


  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9,101
Default VB code to run a Query in Access and paste results onto an Exc

I reformated the code. Try this macro and see if it works. I would consider
the Recordset the table you are extracting which is defined by
Array("qryTRIPGROUP1"). technically the recordset should include the file
name. I number of database command I've worked with are SQL. In this case
your are using OLEDB. The code below should be much easier to understand by
putting each command on its own line.

I don't see from these commands why the error Count = RS_PSQC.Fields.Count
is being displayed. Is there more code after the command below?


Sub test()

With ActiveSheet.QueryTables.Add(Connection:=Array( "OLEDB;" & _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Password="""";" & _
"UserID=Admin;" & _
"Data Source=C:\Data\TripMission 1.mdb;" & _
"Mode=ReadWrite;Extended Properties="""";" & _
"Jet OLEDB:System database="""";" & _
"Jet OLEDB:Registry Path="""";" & _
"Jet OLEDB:Database Password="""";" & _
"Jet OLEDB:Engine Type=5;" & _
"Jet OLEDB:Database Locking Mode=1;" & _
"Jet OLEDB:Global Partial Bulk Ops=2;" & _
"Jet OLEDB:Global Bulk Transactions=1;" & _
"Jet OLEDB:New Database Password="""";" & _
"Jet OLEDB:Create System Database=False;" & _
"Jet OLEDB:Encrypt Database=False;" & _
"Jet OLEDB:Don't Copy Locale on Compact=False;" & _
"Jet OLEDB:Compact Without Replica Repair=False;" & _
"Jet OLEDB:SFP=False"), _
Destination:=Range("A1"))

.CommandType = xlCmdTable
.CommandText = Array("qryTRIPGROUP1")
.Name = "TripMission 1"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.SourceDataFile = "C:\Data\TripMission 1.mdb"
.Refresh BackgroundQuery:=False
End With

End Sub

"Tony Bender" wrote:

When I try and run the macro here is the error msg I get:

Variable not defined:

Count = RS_PSQC.Fields.Count

Does this mean I need to add this to the other DIM entries? If so how
do I phrase it.

Thank you,





Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
No Results returned from Access query Chuck W[_2_] Excel Discussion (Misc queries) 3 September 23rd 09 03:37 PM
Access query not returning results rjchristensen Excel Programming 0 October 22nd 07 04:52 PM
Why are access query results different in Excel DannyD Excel Discussion (Misc queries) 0 July 29th 07 04:54 AM
Access query results to Excel piano banger Excel Discussion (Misc queries) 4 December 8th 06 09:32 AM
VBA - query an Access table - results in Excel rirus Excel Programming 3 August 21st 06 07:29 PM


All times are GMT +1. The time now is 11:59 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"