Wrox Programmer Forums
|
Access VBA Discuss using VBA for Access programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 9th, 2009, 09:08 AM
Registered User
 
Join Date: Jun 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Smile Data copy from access to excel

Hallo Friends,

I have a problem with Access VBA. I don't have a lot of experince in VBA. It might be a sily question :)

I want to copy datas from access tabels (queries) to Excel sheet. But when I want to move throught the Recordset. It doesn't work. It shows me the number of recondset -1, but I know there are some records in it.

This is the problem. It doesn't go into the loop. Here is the cutout of the code:
For j = 2 To rst.RecordCount
For l = 1 To rst.Fields.Count
MsgBox rst.Fields(l - 1)
objMappe.WorkSheets("Sheet1").Cells(j, l) = rst.Fields(l - 1)
Next l
rst.MoveNext
Next j


Here is the whole code what I wrote. My question is what do I wrong here.

Thank your for your help.

Greetings,
Tester

Sub DataFromAccessToExcel()
Dim conn As ADODB.Connection
Dim rst As ADODB.Recordset
Dim objXlApp As Object
Dim objMappe As Object
Dim objWorkSheet As Object
Dim objSheet As Object
Dim i As Integer
Dim j As Integer
Dim l As Integer

On Error GoTo Fehler
'Excel-Objekt erzeugen (Exce starten)
Set objXlApp = CreateObject("Excel.Application")
'Excel sichtbar machen, weil das Besipiel dann mehr Spaß macht
objXlApp.Visible = True

'Excel-Datei aus Verzeichnis der Datenbank öffnen
Set objMappe = objXlApp.Workbooks.Open(CurrentProject.Path & "\Test.xls")
Set objSheet = objMappe.WorkSheets("Sheet1")
objSheet.Activate

'Set fs = CreateObject("Scripting.FileSystemObject")
'Set textFile = fs.CreateTextFile("D:\TiM\Testumgebung\AccessDB\te st.txt", True)

Set conn = CurrentProject.Connection
Set rst = New ADODB.Recordset

'Tabelle qryRelApplTAL öffnen (editierbar)
rst.Open "Domestic", conn, adOpenDynamic, adLockOptimistic

'MsgBox rst.MaxRecords

'rst.Open "qryRelApplTAL", conn, adOpenForwardOnly, adLockReadOnly

For i = 1 To rst.Fields.Count
objMappe.WorkSheets("Sheet1").Cells(1, i) = rst.Fields(i - 1).Name
Next i

MsgBox rst.RecordCount

For j = 2 To rst.RecordCount
For l = 1 To rst.Fields.Count
MsgBox rst.Fields(l - 1)
objMappe.WorkSheets("Sheet1").Cells(j, l) = rst.Fields(l - 1)
Next l
rst.MoveNext
Next j

rst.Close

'Excel-Datei schließen
'objXlApp.Quit

Exit Sub
Fehler:
MsgBox Err.Number & " " & Err.Description
End Sub
 
Old June 9th, 2009, 01:41 PM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
Default

It seems to me that you are going the long way around just to dump data in a spreadsheet. Why not try this:

Code:
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel8, "TableOrQueryName", "C:\Documents and Settings\All Users\Desktop\SpreadsheetName.xls", True
Just replace the TableOrQueryName with the name of a table or query in your database, and replace the SpreadsheetName.xls with whatever name you want.

This will dump a copy of the table or query on your desktop in a spreadsheet with the name you designate, with a worksheet that has the same name as your table or query.

Did that help?
__________________
mmcdonal

Look it up at: http://wrox.books24x7.com





Similar Threads
Thread Thread Starter Forum Replies Last Post
Copy data to Excel gjackson Visual Basic 2005 Basics 1 July 1st, 2007 07:56 AM
Copy data from Excel to open HTML form Bartje Excel VBA 1 September 29th, 2006 04:02 AM
Converting excel data to Access using excel VBA ShaileshShinde VB Databases Basics 1 April 26th, 2006 07:57 AM
Copy data from webpage to excel using vba kg8299 Excel VBA 0 April 5th, 2006 03:51 AM
Copy Data from Access.mdb into an Excel Spreadshee KennethMungwira ADO.NET 2 January 23rd, 2004 10:46 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.