index > Visual Basic for Applications (VBA) > Access report using VBA

Access report using VBA


Hey All!

I have a problem. I am creating a screen that includes persons first_name, last_name, phone_number (this is a fields in access)

Now I have created a command button and I want that when user clicks on the command button the information in those fields would go to excel page.

Sincerely,

Boris




B.T.
tarzan123_us

There's a function in Access that lets you export information to Excel. It's called TransferSpreadsheet.

DoCmd.TransferSpreadsheet acImport, 3, _
"Employees","C:\Lotus\Newemps.wk3", True, "A1:G12"

acImport can be set to acExport

3 indicates what version of Excel you want to target

True is for has field names which includes a top row containing the names of the fields in the table or query.

You can supply a SELECT statement or table name.




www.dsmyth.net/blog
Derek Smyth

Where should I write this code?

B




B.T.
tarzan123_us

Put it behind the command button you want the user to press.

Your screen, if it's bound, will have a table or a query behind it, by that I mean the information on the screen will be coming from a query or a table. You'll be displaying the person's last name, first name, etc information in textboxes on this screen.

So what you want to do is take the information in the text boxes, filter the query, and export the filtered information to Excel. Lets say the screen is bound to a query named [qryEmployees] and the text boxes are called txtFirst and txtLast.

Dim sSQL as String

sSQL = "SELECT * FROM [qryEmployees] WHERE [Firstname] = '" & txtFirst.Value & "' AND [Lastname] = '" & txtLast.Value & "';"

DoCmd.TransferSpreadsheet acExport, 3, sSQL,"C:\Exported.xls", True, "A1:G12"




www.dsmyth.net/blog
Derek Smyth

Good answer! Maybe you can help me for a further more complex question:

How can I parametrize Docmd.TransferSpreadsheet ?

I created a form to let the user choose disk unit, folder and filename, but I don't know how to insert correctly the user choice in the TransferSpreadsheet syntax.

Can you or someone else help me ?

It seems I'm the only one in the world to experiment this problem... ;-(

roberto mariconda

Hello Roberto,

You need to pass the path to the method like this...

Dim sFile as String

sFile = "C:\Folder\Filename.xls"

DoCmd.TransferSpreadsheet acExport, 3, sSQL, sFile, True, "A1:G12"

Whatever the user enters store it as a string and pass it into the transfer spreadsheet method as shown.




www.dsmyth.net/blog
Derek Smyth
reply 6

You can use google to search for other answers

 

More Articles

• dbclick listbox additem
• How to handle CLOB Data in VBA for Excel?
• run a macro in excel
• Reading text file and accessing specific columns in VBA
• DataGridView and ComboBox Column Problem
• VBA Visio 2002 and OpenEx
• Novice VBA user trying to consolidate info from mult wrkshts and ...
• mysterious error: RUNTIME ERROR '451'
• How to change line within one statement?
• SELECT syntax in VBA for retrieving information
Welcome to Bokebb   New Update   Joins the collection  
 

New Articles

• RunTime Error '13' Type Mismatch
• Accessing a subform, error 2455
• Automation to send a Microsoft Outlook m
• Exit problem
• Excel macro question - ref: sheets (hope
• VBA TaskBox (Windows XP) & HTML Mult
• fast search
• CreateProcessA problem - no DOS windows
• Creating a Toolbar and a button that is
• PPT VBA - Automatic undo
• Emails via Excel
• macro in .xml
• key in date and filter the data in a week
• VBA Queston on Formatting Macro
• updating an Visio OLE object on an Acces

Hot Articles

• Getting Excel Formulas to work in VBA
• How to get ASCII code of the character?
• RunTime Error '13' Type Mismatch
• whats wrong with this code?
• Is VBA SDK free and ...?
• Microsoft Access Chart Question
• Distributing a Macro across an organizat
• Run-time error '3085' - Undefined Functi
• Read Data from Execel with VBA and write
• PPT VBA - Searching a file that is not o
• datagrid control in access
• day of the week
• Send Outlook message via VBA Macro in Ex
• Code erroring out.
• Source Object as a query

Recommend Articles

• Adding the VBA Environment to an Applica
• Executing an insert statement in VBA
• Copy text from textbox to clipboard?
• Help: How can I deploy my VBA?
• Access and DWGs
• Display a drawing on an MS Access Form
• Excel FTP using msinet.ocx - PROBLEM
• Hyperlink to a subroutine
• How do I create and use 3D ranges in Exc
• Loop til you drop
• Generating Outlook Appointments from dat
• document title
• winHTTP access to secure ASP site?
• VB Excel - userinput -> UserForms
• CDONTS