p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


Go Back   p2p.wrox.com Forums > Microsoft Office > Access and Access VBA > Access VBA
I forgot my password Register Now
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 p2p Programmer to Programmer discussion community. This is a community of more than 40,000 computer programmers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining our free Wrox p2p community you can post your own programming questions and respond to other programmers’ questions. Registered users also don't have to see the ads that are displayed to guests. Registration is fast, simple and absolutely free so please, join today!
Join today and post to win prizes! Post more to increase your chances of being Wrox’s top poster of the month.

Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old June 9th, 2009, 10:08 AM
Registered User
Points: 5, Level: 1
Points: 5, Level: 1 Points: 5, Level: 1 Points: 5, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #2 (permalink)  
Old June 9th, 2009, 02:41 PM
Friend of Wrox
Points: 9,516, Level: 42
Points: 9,516, Level: 42 Points: 9,516, Level: 42 Points: 9,516, Level: 42
Activity: 11%
Activity: 11% Activity: 11% Activity: 11%
 
Join Date: Mar 2004
Location: Washington, DC, USA.
Posts: 3,060
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

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

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



All times are GMT -4. The time now is 05:11 PM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
© 2008 Wiley Publishing, Inc