|
-
Feb 3rd, 2006, 09:13 AM
#1
Thread Starter
Lively Member
[RESOLVED] Export From Microsoft Access to Excel
How do I export data from Microsoft Access to Microsoft Excel?
Both are Version 2002. Thank you.
-
Feb 3rd, 2006, 09:17 AM
#2
Lively Member
Re: Export From Microsoft Access to Excel
You could take your pick from about 10 ways. Are you trying to do it with VBA? Are you looking for a whole table? A built in query? Will the SQL statement be variable?
-
Feb 3rd, 2006, 09:34 AM
#3
Thread Starter
Lively Member
Re: Export From Microsoft Access to Excel
Yes I'm trying to do it with VBA and whole table too. I'm not sure about the other 2.
-
Feb 3rd, 2006, 09:47 AM
#4
Lively Member
Re: Export From Microsoft Access to Excel
Set a reference to the Microsoft DAO Object Library by clicking "Tools" then "References".
Then edit the following.
Code:
Sub PasteTable()
Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Dim i As Integer
Set dbs = OpenDatabase("c:\mydatabase.mdb")
Set rst = dbs.OpenRecordset("SELECT * FROM mytable;")
If Not IsNull(rst.Fields(0)) Then
For i = 0 To rst.Fields.Count - 1
Cells(1, i + 1).Value = rst.Fields(i).name
Next i
Range("A2").CopyFromRecordset rst
End If
rst.Close
dbs.Close
Set rst = Nothing
Set dbs = Nothing
End Sub
-
Feb 3rd, 2006, 09:53 AM
#5
Re: Export From Microsoft Access to Excel
If you are going to run it from Access then just write this
Code:
DoCmd.RunCommand acCmdExport
@mikeyc1204:
The above process will be slow and DAO is too Old now. Use of ADO is always preffered over DAO.
Use [code] source code here[/code] tags when you post source code.
My Articles
-
Feb 3rd, 2006, 09:59 AM
#6
Lively Member
Re: Export From Microsoft Access to Excel
I just assumed the process would be done in Excel. Not sure why :\
And I was under the impression that ADO is better in most cases, but DAO is faster for simple jet interaction. Wrong?
-
Feb 3rd, 2006, 10:13 AM
#7
Re: Export From Microsoft Access to Excel
IF this process is going to be done in Excel then it is even more easier. This is how I would do it
1. Open a Excel File.
2. Select Record New Macro from Tools-->Macro Menu.
3. Once the recording has started, open Data Menu and select Import Data from Import External Data Menu.
4. Select the Database, and then select the table you want to import.
5. All other dialogs are self Explanatory.
6. Once you are done with this, stop the macro recording.
7. Press ALT + F11 and you will see that VBA has written all the code for you.
Use [code] source code here[/code] tags when you post source code.
My Articles
-
Feb 3rd, 2006, 10:27 AM
#8
Re: Export From Microsoft Access to Excel
One line of code in an Access module. alot easier. 
[Highlight=VB]Application.DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "Table1", "D:\Book1.xls", False, "Sheet1$"[/vbcoe]
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Feb 3rd, 2006, 10:30 AM
#9
Re: Export From Microsoft Access to Excel
 Originally Posted by RobDog888
One line of code in an Access module. alot easier. 
[Highlight=VB]Application.DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "Table1", "D:\Book1.xls", False, "Sheet1$"[/vbcoe]
I had also shown a similar one line in my previous post
Use [code] source code here[/code] tags when you post source code.
My Articles
-
Feb 3rd, 2006, 10:36 AM
#10
Re: Export From Microsoft Access to Excel
Yes, but then you get the dialog popup.
Booooo! *SLAP*
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Feb 3rd, 2006, 10:39 AM
#11
Re: Export From Microsoft Access to Excel
 Originally Posted by RobDog888
Yes, but then you get the dialog popup.
Booooo! *SLAP* 
But then you have the liberty of saving the file anywhere in your PC (even on your network share) 
slap slap..
Use [code] source code here[/code] tags when you post source code.
My Articles
-
Feb 3rd, 2006, 10:43 AM
#12
Re: Export From Microsoft Access to Excel
But if you want to export the table or query results to a specific range in a specific sheet you cant do that with the runcommand. Plus you can always prompt the user for the export location/file if need be. The main plus is the ability to place the data in a particular sheet/cell. 
Ps, SLAP SLAP SLAP
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Feb 3rd, 2006, 10:49 AM
#13
Re: Export From Microsoft Access to Excel
Lets do it then 
Your are n MVP so you will be better.
Use [code] source code here[/code] tags when you post source code.
My Articles
-
Feb 3rd, 2006, 10:55 AM
#14
Re: Export From Microsoft Access to Excel
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Feb 3rd, 2006, 11:07 AM
#15
Re: Export From Microsoft Access to Excel
Note: my code above is for Appending to an EXISTING workbook only. If you want it to work with new files only then you need to use a different file picker dialog.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Feb 3rd, 2006, 05:43 PM
#16
New Member
Re: Export From Microsoft Access to Excel
Last edited by DakDarie; Feb 6th, 2006 at 08:18 AM.
-
Feb 3rd, 2006, 05:47 PM
#17
Re: Export From Microsoft Access to Excel
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Feb 3rd, 2006, 06:00 PM
#18
New Member
Re: Export From Microsoft Access to Excel
-
Feb 3rd, 2006, 11:48 PM
#19
Re: Export From Microsoft Access to Excel
Office SP, considering this code, I highly doubt it. Windows SP, still unlikely.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Feb 4th, 2006, 02:09 AM
#20
Thread Starter
Lively Member
Re: Export From Microsoft Access to Excel
Wow, there are so many options that I duno which one to follow. 
How do we open data menu after the recording has started?
-
Feb 4th, 2006, 02:18 AM
#21
Re: Export From Microsoft Access to Excel
When your recording a macro just use your mouse to click the menu items that you would normally do to export it.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Feb 4th, 2006, 02:45 AM
#22
Thread Starter
Lively Member
Re: Export From Microsoft Access to Excel
I dun have an existing Excel workbook so how do I change the file picker dialog?
-
Feb 4th, 2006, 02:54 AM
#23
Re: Export From Microsoft Access to Excel
Quick and simple example of a basic save as dialog.
Code:
Dim oDLG As FileDialog
Dim sFile As String
Set oDLG = Application.FileDialog(msoFileDialogSaveAs)
oDLG.Show
If oDLG.SelectedItems.Count = 1 Then
sFile = oDLG.SelectedItems.Item(1)
End If
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Feb 4th, 2006, 03:20 AM
#24
Thread Starter
Lively Member
Re: Export From Microsoft Access to Excel
This is what I have done. Can u check whether it is correct?
'from Shuja Ali
1. Open a Excel File.
2. Select Record New Macro from Tools-->Macro Menu.
3. Once the recording has started, open Data Menu and select Import Data from Import External Data Menu.
4. Select car.mdb
5. Stop the macro recording.
7. Press ALT + F11
In the Macro..module window i type
VB Code:
Private Sub ExportMeDynamically()
Dim oDLG As FileDialog
Dim sFile As String
Set oDLG = Application.FileDialog(msoFileDialogSaveAs)
oDLG.Show
If oDLG.SelectedItems.Count = 1 Then
sFile = oDLG.SelectedItems.Item(1)
End If
End Sub
Did I miss something? Sorry I'm new in this.
-
Feb 4th, 2006, 04:02 AM
#25
Re: Export From Microsoft Access to Excel
No, if you want to go the recording a macro route then all you need to do is record the macro doing your manual steps necessary to import the access data.
Then press Alt+F11 to view the generated code in the VBA IDE's Modules folder.
You were mixing two different proposed methods
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Feb 4th, 2006, 04:26 AM
#26
New Member
Re: Export From Microsoft Access to Excel
You need a chat people :P
-
Feb 4th, 2006, 07:28 AM
#27
Re: Export From Microsoft Access to Excel
Lets cheat.. here's one I prepared earlier..
http://www.vbforums.com/showthread.php?t=352849
Look at my first post
Danny
Never Think Impossible
If you find my answer helpful then please add to my reputation
-
Feb 4th, 2006, 02:28 PM
#28
Thread Starter
Lively Member
Re: Export From Microsoft Access to Excel
RobDog888, I had saw one post which you helped and I tried writing it in my VB application.
VB Code:
Private Sub cmdData_Click()
Dim oRs As adodb.Recordset
Dim oCnn As adodb.Connection
Dim oApp As Excel.Application
Dim oWB As Excel.Workbook
Dim i As Integer
'Connect to your Access db
Set oCnn = New adodb.Connection
oCnn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
App.Path & "\Car.mdb;Persist Security Info=False"
oCnn.Open
'Create your recordset
Set oRs = New adodb.Recordset
oRs.Open "SELECT * FROM Table1;", oCnn, adOpenKeyset, adCmdText
'Create an instance of Excel and add a new blank workbook
Set oApp = New Excel.Application
oApp.Workbooks.Open (App.Path & "\Book1.xls")
oApp.Visible = True
Set oWB = oApp.Workbooks.Add
'Add the field names as column headers (optional)
For i = 0 To oRs.Fields.Count - 1
oWB.Sheets(1).Cells(1, i + 1).Value = oRs.Fields(i).Name
Next
oWB.Sheets(1).Range("1:1").Font.Bold = True
oWB.Sheets(1).Cells(2, 1).CopyFromRecordset oRs
oRs.Close
Set oRs = Nothing
oCnn.Close
Set oCnn = Nothing
Set oWB = Nothing
Set oApp = Nothing
End Sub
May I know why does duplicate worksheets of Book1 appear? Eg: Book2, 3, 4 of the same data...
-
Feb 4th, 2006, 02:54 PM
#29
Re: Export From Microsoft Access to Excel
Not sure what context the code was concerning for the thread but its adding a new workbook with this line. If you dont need/want it then use the ammedments below.
Code:
oApp.Workbooks.Open (App.Path & "\Book1.xls")
'Should be
Set oWB = oApp.Workbooks.Open (App.Path & "\Book1.xls")
'Take this line out.
'Set oWB = oApp.Workbooks.Add
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Feb 4th, 2006, 03:50 PM
#30
Thread Starter
Lively Member
Re: Export From Microsoft Access to Excel
Great. Thank u so much. I hope I did the correct thing of exporting Microsoft Access to Excel dynamically.
 I think I'm starting to like programming, but I'm still far from good
-
Feb 6th, 2006, 03:58 AM
#31
Re: Export From Microsoft Access to Excel
-
Feb 6th, 2006, 07:30 AM
#32
Thread Starter
Lively Member
Re: Export From Microsoft Access to Excel
Thanks Radjesh for the tips but I tink I just stick to RobDog888's since it worked. One question though.
After using the code below, I managed to export the whole table from Access to Excel. How do I specify that I want to export eg 10 out of 14 fields from Access to Excel?
Meaning though my table has 14 fields, I want to show only the first 10 fields in Excel.
I tried to use oApp.Columns = 10 but instead all the Excel cells has digit 10 in it.
VB Code:
Private Sub cmdData_Click()
Dim oRs As adodb.Recordset
Dim oCnn As adodb.Connection
Dim oApp As Excel.Application
Dim oWB As Excel.Workbook
Dim i As Integer
'Connect to your Access db
Set oCnn = New adodb.Connection
oCnn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
App.Path & "\Car.mdb;Persist Security Info=False"
oCnn.Open
'Create your recordset
Set oRs = New adodb.Recordset
oRs.Open "SELECT * FROM Table1;", oCnn, adOpenKeyset, adCmdText
'Create an instance of Excel and add a new blank workbook
Set oApp = New Excel.Application
Set oWB = oApp.Workbooks.Open (App.Path & "\Book1.xls")
oApp.Visible = True
'Add the field names as column headers (optional)
For i = 0 To oRs.Fields.Count - 1
oWB.Sheets(1).Cells(1, i + 1).Value = oRs.Fields(i).Name
Next
oWB.Sheets(1).Range("1:1").Font.Bold = True
oWB.Sheets(1).Cells(2, 1).CopyFromRecordset oRs
oRs.Close
Set oRs = Nothing
oCnn.Close
Set oCnn = Nothing
Set oWB = Nothing
Set oApp = Nothing
End Sub
 I think I'm starting to like programming, but I'm still far from good
-
Feb 6th, 2006, 07:38 AM
#33
Re: Export From Microsoft Access to Excel
In your select query instead of writing * explicitly mention the Field Names that you want to export
Use [code] source code here[/code] tags when you post source code.
My Articles
-
Feb 6th, 2006, 08:02 AM
#34
Thread Starter
Lively Member
Re: Export From Microsoft Access to Excel
Like this? But Excel still shows all the fields in my table
oRs.Open "SELECT CustId,CustCom,CustName,ComCertNo,CustAddr,CustOPhone,CustMobile,CustFax,CustEmail,CustRemark FROM Table1 ", oCnn, adOpenKeyset, adCmdText
 I think I'm starting to like programming, but I'm still far from good
-
Feb 6th, 2006, 08:11 AM
#35
Re: Export From Microsoft Access to Excel
I don't see why Excel should be doing this. Your recordset contains just 10 fields and it should export only 10 fields.
Use [code] source code here[/code] tags when you post source code.
My Articles
-
Feb 6th, 2006, 12:20 PM
#36
Re: Export From Microsoft Access to Excel
This is an easy one, my code contains a loop where its listing out the columns in the sql query. So if your adding the field names to the select list then your loop will only contain that number of fields. You have 10 fields listed in your sql statement so that is how many columns your going to have.
The MaxColumns argument is another way to limit the number of columns copied from the recordset.
VB Code:
oRs.Open "SELECT CustId, CustName FROM Table1 ", oCnn, adOpenKeyset, adCmdText
'Now you will have only 2 columns listed
'Add the field names as column headers (optional)
For i = 0 To oRs.Fields.Count - 1
oWB.Sheets(1).Cells(1, i + 1).Value = oRs.Fields(i).Name
Next
oWB.Sheets(1).Range("1:1").Font.Bold = True
oWB.Sheets(1).Cells(2, 1).CopyFromRecordset Data:=oRs, ,MaxColumns:=2
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Feb 6th, 2006, 01:19 PM
#37
Thread Starter
Lively Member
Re: Export From Microsoft Access to Excel
Sorry but I still can't get it to show me the specified no. of fields. I've been using Adodc1 as ADO Data Control. Is this the case cos I didn't define it in the code.
The reason I've to show lesser fields is becos, my MSHFlexgrid only shows 10 of the fields. I kind of want to show what is on the MSHFlexgrid, is displayed in Excel.
 I think I'm starting to like programming, but I'm still far from good
-
Feb 6th, 2006, 01:23 PM
#38
Re: Export From Microsoft Access to Excel
Can you post your code? I dont think it may be the same if using the ADODC Data control. Not 100% sure as I havent tried it with it but it is an ADO control so lets see what we can do.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Feb 6th, 2006, 01:46 PM
#39
Thread Starter
Lively Member
Re: Export From Microsoft Access to Excel
This is the code i usually connect to my database.
VB Code:
Adodc1.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
App.Path & "\Car.mdb;Persist Security Info=False"
Adodc1.RecordSource = "SELECT * FROM Table1 "
I tried to change to Adodc and it gives me a headache.
VB Code:
Private Sub cmdData_Click()
Dim oRs As adodb.Recordset
Dim oApp As Excel.Application
Dim oWB As Excel.Workbook
Dim i As Integer
'Connect to your Access db
'Set oCnn = New adodb.Connection
Adodc1.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
App.Path & "\Car.mdb;Persist Security Info=False"
Adodc1.Recordset.Open
'Create your recordset
Set oRs = Adodc1.Recordset
oRs.Open "SELECT Custid, CustName FROM Table1;", Adodc1, adOpenKeyset, adCmdText
'Create an instance of Excel and add a new blank workbook
Set oApp = New Excel.Application
Set oWB = oApp.Workbooks.Open(App.Path & "\Book1.xls")
oApp.Visible = True
'Add the field names as column headers (optional)
For i = 0 To oRs.Fields.Count - 1
oWB.Sheets(1).Cells(1, i + 1).Value = oRs.Fields(i).Name
Next
oWB.Sheets(1).Range("1:1").Font.Bold = True
oWB.Sheets(1).Cells(2, 1).CopyFromRecordset Data:=oRs, MaxColumns:=2
oRs.Close
Set oRs = Nothing
Adodc1.Recordset.Close
'Set oCnn = Nothing
Set oWB = Nothing
Set oApp = Nothing
End Sub
 I think I'm starting to like programming, but I'm still far from good
-
Feb 6th, 2006, 01:52 PM
#40
Re: Export From Microsoft Access to Excel
Are you getting any errors?
Looks like your running tow recordsets. One adodc and one ors.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|