Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Acess VBA - Export query to Excel + Create New Sheets

Posted on 2006-11-15
4
1,425 Views
Last Modified: 2008-02-01
Hi all,
Just a quick question... I have an access DB that exports a query called "lastName" to Excel. This works fine, but I want to be able to create a new sheet based on a variable called textInput.

Here is my code right now...

Private Sub Command6_Click()
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim querySQL As String
Set db = CurrentDb
Set qdf = db.QueryDefs("lastName")
querySQL = "My SQL query..."
If Me.searchText.Value = "" Then
    MsgBox ("Invalid Input")

Else
qdf.SQL = querySQL
DoCmd.OpenQuery "lastName"
Set qdf = Nothing
Set db = Nothing
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel8, "lastName", "C:\Desktop\MyField.xls", True

End If
End Sub


The query is called "lastName."

I want to take the results of that same query, but export it to a different sheet name. Any ideas?
0
Comment
Question by:gigantorTRON
  • 3
4 Comments
 
LVL 65

Expert Comment

by:rockiroads
ID: 17948706
try specifing the sheetname
e.g

DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel8, "lastName", "C:\Desktop\MyField.xls", True, "Sheet2!"
0
 
LVL 65

Accepted Solution

by:
rockiroads earned 260 total points
ID: 17948715
typo

try this - specify a range
eg

Sheet2$A1:Z65536

instead of Sheet2
0
 
LVL 1

Author Comment

by:gigantorTRON
ID: 17948793
Wow, I really thought that it would be harder than that for some reason!

Thanks for the quick response!
0
 
LVL 65

Expert Comment

by:rockiroads
ID: 17948827
No probs :)
0

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question