【问题标题】:Export data from continuous form to Excel w/o header fields将数据从连续表单导出到 Excel 无标题字段
【发布时间】:2014-05-07 20:55:53
【问题描述】:

我有一个连续表单,其中表单标题包含过滤选项,详细信息部分包含数据。

我希望能够将其导出到 Excel。基本的 VBA 代码有效

DoCmd.OutputTo

但是当我导出到 Excel 时,它还包括每一行的表单标题控件。

有没有什么方法可以设置一个属性来排除表单标题被包含在导出中?基本上只导出表单详情部分?

我不喜欢使用查询

我在标题中有 6 个未绑定的 txt 框: -artnr - Artnr 供应商 - 描述 - 文章状态 - 供应商名称 - 供应商编号 我有一个搜索按钮,其中包含以下代码:

Private Sub cmdSearch_Click()

    Dim strWhere As String
    Dim lngLen As Long

'artikel zoeken
    If Not IsNull(Me.txtSearchArtnr) Then
        strWhere = strWhere & "([Material] Like ""*" & Me.txtSearchArtnr & "*"") AND "
    End If

'artnr leverancier zoeken
    If Not IsNull(Me.txtSearchSupplArt) Then
        strWhere = strWhere & "([LiefMat] Like ""*" & Me.txtSearchSupplArt & "*"") AND "
    End If

'trefwoord zoeken
    If Not IsNull(Me.txtSearchKeyword) Then
        strWhere = strWhere & "([Materialkurztext] Like ""*" & Me.txtSearchKeyword & "*"") AND "
    End If

'artikelstatus zoeken
    If Not IsNull(Me.txtSearchStatus) Then
        strWhere = strWhere & "([Status] Like ""*" & Me.txtSearchStatus & "*"") AND "
    End If

'leverancier naam zoeken
    If Not IsNull(Me.txtSearchSupplName) Then
        strWhere = strWhere & "([Name 1] Like ""*" & Me.txtSearchSupplName & "*"") AND "
    End If

'leverancier nummer zoeken
    If Not IsNull(Me.txtSearchSupplNumber) Then
        strWhere = strWhere & "([Lieferant] Like ""*" & Me.txtSearchSupplNumber & "*"") AND "
    End If

'***********************************************************************
'Chop off the trailing " AND ", and use the string as the form's Filter.
'***********************************************************************
'See if the string has more than 5 characters (a trailng " AND ") to remove.

    lngLen = Len(strWhere) - 5
    If lngLen <= 0 Then     'Nah: there was nothing in the string.
        MsgBox "Geen criteria gevonden", vbInformation, "Geen resultaten."

    Else                    'Yep: there is something there, so remove the " AND " at the end.
        strWhere = Left$(strWhere, lngLen)

        'Apply the string as the form's Filter.
        Me.Filter = strWhere
        Me.FilterOn = True
    End If
End Sub

【问题讨论】:

  • 为什么不将过滤器放在查询上并导出查询结果?这听起来是一种更简单的方法。
  • 根据我的阅读,您不能排除导出的标题行 - 仅在导入时。一种选择是进行导出,然后运行 ​​VBA 代码打开 Excel 文件并删除第 1 行。
  • 这不是我要排除的标题行。在我的访问表单的标题中,我创建了未绑定的文本框来搜索所有记录。例如人们可以在文章描述中搜索关键字。当他们找到他们需要的东西(可能是多条记录)时,他们需要能够将这些结果导出到 Excel。但是当我对 docmd.output 执行此操作时,未绑定的文本框每个也成为我的 excel 文件中的一列。
  • 您可以将未绑定的文本框放在表单页脚而不是页眉中吗?
  • 这是我刚刚发现并测试过的好东西...access-programmers.co.uk/forums/showthread.php?t=149974

标签: ms-access ms-access-2007 vba export-to-excel


【解决方案1】:

我在这里找到了解决方案:

Exporting selected records to excel - hiding certain columns

DoCmd.Echo False

Me.Field1.Visible = False
Me.Field2.Visible = False
Me.Field3.Visible = False

DoCmd.RunCommand acCmdOutputToExcel

Me.Field1.Visible = True
Me.Field2.Visible = True
Me.Field3.Visible = True

DoCmd.Echo True

End Sub

很简单,对我有用

【讨论】:

    【解决方案2】:

    您写道,用户可以设置过滤器,所以您必须编写类似
    Me.RecordSource = "SELECT ... FROM table WHERE --这里是标准--"
    Me.Requery

    因此您可以获取 SQL 语句并将其用于导出,您首先必须创建一个查询

        Dim sSQL As String
        Dim qd As QueryDef
    
        Set qd = CurrentDb.CreateQueryDef("tmp_Query")
        qd.SQL = "Select * from T_Personal"
    
        DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12, "tmp_Query", "yourfile", True
    
        CurrentDb.QueryDefs.Delete qd.Name
    

    将它包装成一个函数,这样你就可以在任何地方触发它并传递 sql 和文件名...

    HTH

    【讨论】:

    • 我在我的问题中添加了更多信息。我希望这将有助于寻找答案:)
    • 您好 Isabel,当您使用过滤器功能时,您可以使用准备好的 where 语句并将其与以下形式的记录源结合:sSQL = Me.RecordSource & "WHERE" & strWhere and take我上面的示例代码创建查询然后导出到 excel
    • 谢谢!但是我把你提到的 SQL 语句放在哪里呢?和你给我的代码,我可以把它放在点击事件的表单按钮中吗?基本上,如果您能告诉我我需要采取的步骤,我将不胜感激:)
    • 嗨 Isabel - 好吧,您可以将它添加到现有过滤器代码的末尾,但我假设您不想在每次用户更改过滤器时都导出。所以我会在表单上看到一个按钮来触发导出。在那里你可以放代码。但是你必须像这样组合 SQL:sSQL = Me.RecordSource & " WHERE " & Me.Filter (正如你在过滤器按钮中定义的 strWhere ) - 或者你在声明部分中定义表单级别的 strWhere
    • 所以我将代码中的 qd.SQL 行替换为 sSQL = Me.RecordSource & " WHERE " & Me.Filter?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-27
    相关资源
    最近更新 更多