2014-04-22 91 views
0

我有一個連續的表單,其中表單標題包含過濾器選項,而詳細信息部分包含數據。將數據從連續表格導出到Excel無Excel域

我想能夠導出到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 
+0

你爲什麼不把過濾器上的查詢和導出查詢的結果?這聽起來像一個更簡單的方法來做到這一點。 –

+0

從我讀過的內容來看,您不能排除導出的標題行 - 只能在導入時排除。一種選擇是執行導出,然後運行VBA代碼打開Excel文件並刪除第1行。 –

+0

它不是我想要排除的標題行。 在我的訪問表單的標題中,我創建了未綁定的文本框來搜索所有記錄。例如人們可以在文章描述中搜索關鍵字。當他們找到他們需要的東西時,可能需要多個記錄,他們需要能夠將這些結果導出爲ex​​cel。但是當我做這個cia docmd.output時,未綁定的文本框也都成爲我excel文件中的一列。 – Isabel2410

回答

0

我發現這裏sollution:

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 

它的簡單和它的作品對我來說

0

你寫的,用戶可以設置過濾器,所以你必須要有編程類似
Me.RecordSource =「SELECT ... FROM表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

+0

我已經爲我的問題添加了更多信息。我希望這將有助於尋找答案:) – Isabel2410

+0

嗨伊莎貝爾 好吧,當你使用過濾器功能,你可以使用準備好的where語句,並將其與以下格式的記錄源結合: sSQL = Me.RecordSource& 「WHERE」&strWhere 並拿上面的示例代碼創建查詢,然後導出到excel –

+0

謝謝!但是我在哪裏放置了你提到的SQL語句?和你給我的代碼,我可以把它放在我的表單按鈕點擊事件?基本上,我會很感激,如果你可以告訴我,我需要採取的步驟:) – Isabel2410