473,406 Members | 2,208 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Running an Access query from Excel

I am trying to run an Access query from Excel. The query, called "ProdActs1" works in Access and is run from using information from a table called "Queries". When a button is clicked in Excel, data currently in the table is deleted and new data is sent based on cell value on the Excel worksheet. So far, I can delete the old records and add new data. I just cannot seem to return results from the query "ProdActs1". Also, is it possible to specify a range to return the query results? Below is the relevant code that I have so far.

Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset

'If database hasn't been opened yet, user is prompted to choose database
If file = "" Then 'file is a Global String
file = Application.GetOpenFilename
If WB1 = "False" Then Exit Sub
End If


Set cn = New ADODB.Connection 'Opens database connection
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0; " & _
"Data Source=" & file

Set rs = New ADODB.Recordset ' open a recordset
rs.Open "Queries", cn, adOpenKeyset, adLockOptimistic, adCmdTable

' all records in a table


With rs
.Delete ' deletes old records
End With

With rs
.AddNew ' create a new record
' add values to each field in the record
.Fields("AssocId") = ActiveSheet.Cells(4, 2).Value
.Fields("Start") = ActiveSheet.Cells(1, 2).Value
.Fields("Principle") = ActiveSheet.Cells(6, 2).Value
.Fields("Activity") = ActiveSheet.Cells(7, 2).Value
.Fields("End") = ActiveSheet.Cells(2, 2).Value
' add more fields if necessary...
.Update ' stores the new record
End With


Thanks in advance.
Mar 23 '07 #1
3 7958
Denburt
1,356 Expert 1GB
Select the location where you want to display the results, select Data then import external data, then Import Data. Once the results are there you can requery it at anytime.

ActiveWorkbook.Sheets("Sheet1").Select
Range("A1").Select
Selection.QueryTable.Refresh BackgroundQuery:=True

Does this help?
Mar 23 '07 #2
Unfortunately not. I need to automate this process. There are several buttons on a page. If I can get this working, each will pull several queries and perform some formatting on the worksheet and some calculations depending on the button pressed. In addition, once I try this, is causing errors if I try to connect to the DB again. In order to send more data, I have to close out the spreadsheet and reopen it.





Select the location where you want to display the results, select Data then import external data, then Import Data. Once the results are there you can requery it at anytime.

ActiveWorkbook.Sheets("Sheet1").Select
Range("A1").Select
Selection.QueryTable.Refresh BackgroundQuery:=True

Does this help?
Mar 23 '07 #3
Denburt
1,356 Expert 1GB
O.K. Change the bold sections and this should get you started. :)

Expand|Select|Wrap|Line Numbers
  1. '
  2. ' Macro1 Macro
  3. ' Macro recorded 3/23/2007 by denburt
  4. '
  5.  
  6. '
  7.     With ActiveSheet.QueryTables.Add(Connection:=Array( _
  8.         "OLEDB;Provider=Microsoft.Jet.OLEDB.4.0;Password="""";User ID=Admin;Data Source=YourFullDataBasePathHere;Mode=Share Deny Writ" _
  9.         , _
  10.         "e;Extended Properties="""";Jet OLEDB:System database="""";Jet OLEDB:Registry Path="""";Jet OLEDB:Database Password="""";Jet OLEDB:Engine" _
  11.         , _
  12.         " Type=5;Jet OLEDB:Database Locking Mode=0;Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Global Bulk Transactions=1;Jet OLEDB:New" _
  13.         , _
  14.         " Database Password="""";Jet OLEDB:Create System Database=False;Jet OLEDB:Encrypt Database=False;Jet OLEDB:Don't Copy Locale on Com" _
  15.         , _
  16.         "pact=False;Jet OLEDB:Compact Without Replica Repair=False;Jet OLEDB:SFP=False" _
  17.         ), Destination:=Range("A1"))
  18.         .CommandType = xlCmdTable
  19.         .CommandText = Array("YourTable/QueryName")
  20.         .Name = "Accnt_be"
  21.         .FieldNames = True
  22.         .RowNumbers = False
  23.         .FillAdjacentFormulas = False
  24.         .PreserveFormatting = True
  25.         .RefreshOnFileOpen = False
  26.         .BackgroundQuery = True
  27.         .RefreshStyle = xlInsertDeleteCells
  28.         .SavePassword = False
  29.         .SaveData = True
  30.         .AdjustColumnWidth = True
  31.         .RefreshPeriod = 0
  32.         .PreserveColumnInfo = True
  33.         .SourceDataFile = "YourFullDatabasePathHere"
  34.         .Refresh BackgroundQuery:=False
  35.     End With
  36.  
Mar 23 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Fred | last post by:
Hi. How do I import while mapping an excel table to an access table please??? I've searched around and all I can find is a software product or code that does the same thing as the access...
3
by: etwebbox | last post by:
Hoping someone has some ideas on how to solve this issue: I have a macro in excel which runs a query against an Access DB (it actually calls a query inside of Access). The query is not bringing...
8
by: Jerome Ranch | last post by:
Okay So I've got pivot tables setup in Access 2003. Only about 30K records in the current 2005 databases...the pivots summarize the info in a number of nice ways. I need to get the pivot tables...
0
by: schan | last post by:
Hi there, I was wondering if someone could shed some light on a problem I have no idea on how to fix. I created an Excel Add-In that uses an ADO connection to an Access database on a file...
1
by: smaczylo | last post by:
Hello, I've recently been asked to work with Microsoft Access, and while I feel quite comfortable with Excel, I'm at a complete loss with databases. If someone could help me with this issue I'm...
3
by: Richard Hollenbeck | last post by:
I hope this isn't too confusing. The following query runs pretty fast by itself, but when I want to use it in a report (pasted below the query), it takes at least fifteen seconds to run! Then I...
1
by: CoolFactor | last post by:
MY CODE IS NEAR THE BOTTOM I want to export this Access query into Excel using a command button on an Access form in the following way I describe below. Below you will find the simple query I am...
0
NeoPa
by: NeoPa | last post by:
Many of us have noticed that there are some very useful functions available to you when using Excel, but these same functions are not available, as standard, in Access. A particular issue I had...
15
by: OzNet | last post by:
I have a query (with calculated fields) in Access (2007) and the data changes depending on the dates and staff person selected. I need to produce a series of graphs based on the data in this query...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.