473,498 Members | 1,992 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

copy Access 2003 table over to excel 2007 on startup

63 New Member
I am trying to write some code to copy over a Microsoft Access (2003) table to a specific sheet of a Microsoft Excel (2007) workbook

I want the code to reside in the excel workbook and copy the table into excel when the excel file is opened.

I have been struggling at the first hurdle just getting the table copied over. I found the following code but when I copy it into a module I cannot step into the macro to run it - or see the macro in the list of available macros.

I just want the whole table copying over!

Any ideas gratefully received!!! Thanks in advance



Sub ADOImportFromAccessTable(DBFullName As String, _
TableName As String, TargetRange As Range)
ADOImportFromAccessTable "Q:\IT\Database Masters\Master_Data.mdb", _
"FERC Database", Range("A2")
Dim cn As ADODB.Connection, rs As ADODB.Recordset, intColIndex As Integer
Set TargetRange = TargetRange.Cells(1, 1)
' open the database
Set cn = New ADODB.Connection
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & _
DBFullName & ";"
Set rs = New ADODB.Recordset
With rs
' open the recordset
.Open TableName, cn, adOpenStatic, adLockOptimistic, adCmdTable
' all records
'.Open "SELECT * FROM " & TableName & _
" WHERE [FieldName] = 'MyCriteria'", cn, , , adCmdText
' filter records

RS2WS rs, TargetRange ' write data from the recordset to the worksheet

' ' optional approach for Excel 2000 or later (RS2WS is not necessary)
' For intColIndex = 0 To rs.Fields.Count - 1 ' the field names
' TargetRange.Offset(0, intColIndex).Value = rs.Fields(intColIndex).Name
' Next
' TargetRange.Offset(1, 0).CopyFromRecordset rs ' the recordset data

End With
rs.Close
Set rs = Nothing
cn.Close
Set cn = Nothing
End Sub
Jun 19 '12 #1
1 1796
MikeTheBike
639 Recognized Expert Contributor
Hi

You will not see the macro in the macro window because that sub has arguments, only subs with no argument are listed (because it would not know what to use as an argument!?).

For the code to auto run on opening the workbook you need to run it from the Workbook_Open() event in the ThisWorkbook module.
ie like
Expand|Select|Wrap|Line Numbers
  1. Private Sub Workbook_Open()
  2.     ADOImportFromAccessTable "C:\You Folder\YourDB.mdb", "tblName", ActiveSheet.Range("A:1")
  3. End Sub
I am not sure the code will work, obviously, but I think this
RS2WS rs, TargetRange
should changed to
TargetRange.CopyFromRecordset rs

HTH

MTB
Jun 29 '12 #2

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

Similar topics

0
1087
by: john | last post by:
When I copy an Access record to the clipboard it automatically copies the field names along with it. In this particular case I don't want that to happen. Is there by any chance a way to only copy...
0
1705
by: silverear | last post by:
Hello everybody, I have written an application that is running as an Office project within Excel in C#. I created a setup project for my tool. When I install my program on a machine that has...
1
3855
by: jjjoic | last post by:
Hi, I use Access 2003 to generate the back-end data for a ColdFusion report at work. The report is sorted by a column and based on the sorting, rankings are assigned to each row(i.e. the biggest...
2
2040
by: eskelies | last post by:
Hello all, I have data, which is separated into account numbers (ie. 10, 20, 30), but it exists in one query. Right now, I have an access macro "transferspreadsheet," which is exporting all the...
3
2248
by: Bobby | last post by:
Hi Can anybody tell me why the VBA code below works fine in Access 2003 but doesn't work in Access 2007? I do not get any error message, even if I put a deliberate error into the code. Also, if I...
7
2491
by: AkosBeres | last post by:
I’m in the process of setting a sample local Access table called "Fruits" to store data in the following sample fileds: Week Amount Description Version or sequence The data will be loaded into...
1
1801
by: KrazyKasper | last post by:
MS Access 2003 – table inside a report I have an Access report that I use in a mail merge (export to Excel and then mail-merge into a Word document). The report currently shows: CustomerId...
0
1641
geolemon
by: geolemon | last post by:
I developed an Access database on my laptop, which has Access 2003. The workstations in our office have Office 2007 installations, which did not include Access (Student and Teacher edition...
5
3864
by: Jock | last post by:
My purchase order system sequentially added the next number to the new order when I clicked the macro button set up with copy/paste functions and it worked well. I cannot do this now as 2007 does...
0
1181
by: evilbungle | last post by:
Good Morning, I am trying to build an application that will take the details from an access database and use them as log in details but I can not get the App to open the table as I keep getting...
0
7002
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7203
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...
1
6885
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
7379
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
5462
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,...
1
4908
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3093
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1417
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
290
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.