Export Specific Columns To Excel

cutun1217

New member
Local time
Tomorrow, 05:47
Joined
Aug 24, 2006
Messages
8
hi everybody, im have a database with table called "project". there are many column in this table. my user want to export this table to Excel, but only some of column, with particular order ( depend on him) to analyze in Excel.
he asked me to build a form with a list box, drop box,somthing like this, so he can choose what column to export in what order.
i try to make a query like this: " Select Forms!UserInput.combobox1.value , Forms!UserInput.combobox2.value,etc, From Project" but it wont work.
Dou you have any idea.
thanks in advance
 
your code must be
Dim strSQL as String
StrSQL = " Select " & Forms!UserInput.combobox1 & ", " & Forms!UserInput.combobox2 & " From Project"
But make sure combobox1 's data_type is set as text !

Export to excel
this an example 's code (there re more complex ways but this one is simple)
DoCmd.TransferSpreadsheet acExport,, "query_name", "C:\xyz.xls"
 
My full code is like this:

Private Sub Command4_Click()
On Error GoTo Err_Command4_Click

Dim strSQL As String

strSQL = " Select " & Forms!UserInput.Combo0 & ", " &Forms!UserInput.Combo2 & " From Project"

DoCmd.TransferSpreadsheet acExport, , "strSQL", "C:\project.xls"

Exit_Command4_Click:
Exit Sub

Err_Command4_Click:
MsgBox Err.Description
Resume Exit_Command4_Click

End Sub
i received error: MS Jet database coundnt find the object 'strSQL' ...

how to set combobox data type as TEXT? is it default? sorry, i just a newbie, just start learning.
Thanks a lot
 
i try to make a query like this: " Select Forms!UserInput.combobox1.value , Forms!UserInput.combobox2.value,etc, From Project" but it wont work.
to correct you at this i suggest :
StrSQL = " Select " & Forms!UserInput.combobox1 & ", " & Forms!UserInput.combobox2 & " From Project"

But
DoCmd.TransferSpreadsheet acExport, , "strSQL", "C:\project.xls"

this one "strSQL is SQL code (you must change it to query by CreateQueryDef() Or create a new query that have the same SQL

What i mean is you should have to create a query then use this one
DoCmd.TransferSpreadsheet acExport,, "query_name", "C:\xyz.xls"

Or you can take a look in this writting of Ghudson
http://www.access-programmers.co.uk/forums/showthread.php?t=84119
 
Last edited:
What Rich is saying, is please don't post the same query in mutliple areas. Another thing to note is that a more descriptive thread title of your problem is better than telling people you need help urgently. One, because most people here need help in one way or another so it's taken as granted that's what your thread will be about. And two, because the use of urgent makes it sound like you expect people to drop what they are doing and help you. This is a free forum, people don't get paid to advise you, thus you will get help as and when someone decides to.

When it's urgent to you, it's just another post on a busy board to everyone else.

And when posting code, learn to use the CODE tags - it makes reading VBA doodlings much easier.
 
Last edited:
To Cleric: Thanks a lot. i will play around with it.
to McAbney: Im sorry for inconvenience. just little bit underpressure to finish the task on time and i dont know much about Access. i will follow all the rule here. thanks a lot.
 
I got it. Thanks you so much all of you guys. This forum is really like GOLD for me.
 
below is my code
if i use .QueryDef.Delete, it will cause error "Item not found in this collection" ( because nothing to delete :D )

however, if i remove it, it successfully execute 1st time, from the 2nd time, an error display: "Object newQuery already exists"

i want the function will check whether newQuey exist or not then delete it( using If) , but i have some problem with the syntax, still trail n error. do you guys have any idea?

thanks a lot

Code:
Private Sub Command4_Click()
On Error GoTo Err_Command4_Click
'this function will export part of the table to Excel file based on
'bug1:
'if using .QueryDef.Delete, error at the first time use
Dim db As DAO.Database
Dim qdf As QueryDef
Dim strSQL As String

Set db = CurrentDb()

strSQL = "Select distinct " & Forms!UserInput.Combo0 & ", " & Forms!UserInput.Combo2 & " From Project"

With db
[COLOR="Red"].QueryDefs.Delete ("newQuery")[/COLOR]
Set qdf = db.CreateQueryDef("newQuery", strSQL)
.Close
End With

DoCmd.TransferSpreadsheet acExport, , "newQuery", "C:\xyz.xls"
MsgBox ("Data has been exported successfully")
Exit_Command4_Click:

    Exit Sub

Err_Command4_Click:
    MsgBox Err.Description
    Resume Exit_Command4_Click
    
End Sub
 

Users who are viewing this thread

Back
Top Bottom