Can I use DoCmd.OutputTo to export the current record only?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Tim F
    New Member
    • Aug 2010
    • 16

    Can I use DoCmd.OutputTo to export the current record only?

    Hello all,

    I would like to create a command button on a form that will allow the user to export the current record to .xls.

    The following exports the entire table. Is there a way to export the current record being viewed only?

    Code:
    DoCmd.OutputTo acOutputForm, "frmInputView", acFormatXLS, "filename.xls", False
    Many thanks,
    Tim
  • ADezii
    Recognized Expert Expert
    • Apr 2006
    • 8834

    #2
    Just subscribing for now...will return with an idea later.

    Comment

    • ADezii
      Recognized Expert Expert
      • Apr 2006
      • 8834

      #3
      1. Declare Variables to hold the Original and Modified Record Sources
        Code:
        Dim strOriginalRecordSource As String
        Dim strNewRecordSource As String
      2. Retrieve the Original Record Source of the Form
        Code:
        strOriginalRecordSource = Me.RecordSource
      3. Create a New Record Source based on the Primary Key displayed on the Form
        Code:
        strNewRecordSource = "SELECT * FROM tblEmployees WHERE [ID] = " & Me![ID]
      4. Apply the Filtered Record Source to the Form
        Code:
        Me.RecordSource = strNewRecordSource
      5. Output the Single, Form Record
        Code:
        DoCmd.OutputTo acOutputForm, "frmInputView", acFormatXLS, "filename.xls", False
      6. Restore the Original Record Source
        Code:
        Me.RecordSource = strOriginalRecordSource
      7. Complete Code with Comments stripped:
        Code:
        Dim strOriginalRecordSource As String
        Dim strNewRecordSource As String
        
        strOriginalRecordSource = Me.RecordSource
        strNewRecordSource = "SELECT * FROM tblEmployees WHERE [ID] = " & Me![ID]
        
        Me.RecordSource = strNewRecordSource
        
        DoCmd.OutputTo acOutputForm, "frmInputView", acFormatXLS, "filename.xls", False
        
        Me.RecordSource = strOriginalRecordSource

      Comment

      Working...