|
给个现成的
把子窗体内容输出到EXCEL中
On Error GoTo errit
Dim oExcel As Object
Dim oBook As Object
Dim I As Integer
Set oExcel = CreateObject("Excel.Application")
Set oBook = oExcel.Workbooks.Add()
Me.子窗体.Form.Recordset.MoveFirst
For I = 0 To Me.子窗体.Form.Recordset.Fields.Count - 1
oBook.Worksheets(1).Cells(1, I + 1).Value = Me.子窗体.Form.Recordset.Fields(I).Name
Next
oBook.Worksheets(1).Range("A2").CopyFromRecordset Me.子窗体.Form.Recordset
oBook.SaveAs ("d:\Test.xls")
MsgBox "导出成功"
errexit:
oBook.Close False
oExcel.Quit
Set oBook = Nothing
Set oExcel = Nothing
Exit Sub
errit:
MsgBox "错误号为" & Err.Number & " 错误说明:" & Err.Description
Resume errexit
************************
很好,其实改为通用函数岂不更好?
Public Sub rs2xls(rs As Object)
'将子窗体记录复制到XLS中
On Error GoTo errit
'set rs = Me.子窗体.Form.Recordset
Dim oExcel As Object
Dim oBook As Object
Dim I As Integer
Set oExcel = CreateObject("Excel.Application")
Set oBook = oExcel.Workbooks.Add()
rs.MoveFirst
For I = 0 To rs.Fields.Count - 1
oBook.Worksheets(1).Cells(1, I + 1).Value = rs.Fields(I).Name
Next
oBook.Worksheets(1).Range("A2").CopyFromRecordset rs
oBook.SaveAs ("C:\Book1.xls")
MsgBox "导出成功"
'打開文件時用到.
'ShellExecute Application.hWndAccessApp, "Open", "d:\Test.xls", "", "d:\", SW_NORMAL
errexit:
oBook.Close False
oExcel.Quit
Set oBook = Nothing
Set oExcel = Nothing
Exit Sub
errit:
MsgBox "错误号为" & Err.Number & " 错误说明:" & Err.Description
Resume errexit
End Sub
然后在窗体中调用即可:
Private Sub Command1_Click()
rs2xls subfrm.Form.Recordset '子窗体: subfrm
End Sub
******************************************
Public Sub OutputSubForm(frmMainForm As Form, frmSubFormName As String)
'*****************************************************
'使用示例:OutputSubForm Me, Me.订单子窗体.Name
'http://www.accfans.net 李寻欢
'2005-08-16
'******************************************************
Dim strSql As String
Dim strRecordSource As String
Dim strLinkChildfields As String
Dim strLinkMasterFields As String
Dim strFilter As String
Dim blnFilterOn As Boolean
Dim strLinkSQL As String
Dim Rs As Recordset
Dim Qd As QueryDef
On Error GoTo Outputerr:
Set Rs = frmMainForm.Controls(frmSubFormName).Form.RecordsetClone
Set Qd = CurrentDb.CreateQueryDef("qryTemp")
strRecordSource = frmMainForm.Controls(frmSubFormName).Form.RecordSource
strLinkChildfields = frmMainForm.Controls(frmSubFormName).LinkChildFields
strLinkMasterFields = frmMainForm.Controls(frmSubFormName).LinkMasterFields
strFilter = frmMainForm.Controls(frmSubFormName).Form.Filter
blnFilterOn = frmMainForm.Controls(frmSubFormName).Form.FilterOn
If strLinkChildfields <> "" Then
Select Case Rs.Fields(strLinkChildfields)
Case dbChar
strLinkSQL = strLinkChildfields & "='" & frmMainForm.Controls(strLinkMasterFields) & "'"
Case Else
strLinkSQL = strLinkChildfields & "=" & frmMainForm.Controls(strLinkMasterFields)
End Select
End If
If blnFilterOn = True Then
If strLinkSQL <> "" Then
strLinkSQL = strLinkSQL & " and " & strFilter
Else
strLinkSQL = strFilter
End If
End If
If InStr(strRecordSource, "Select ") > 0 Then
strSql = Left(strRecordSource, Len(strRecordSource) - 2)
Else
strSql = "Select * From " & strRecordSource
End If
If InStr(strRecordSource, " where ") > 0 Then
If strLinkSQL <> "" Then
strSql = strSql & " and " & strLinkSQL
End If
Else
If strLinkSQL <> "" Then
strSql = strSql & " where " & strLinkSQL
End If
End If
Qd.SQL = strSql
DoCmd.OutputTo acOutputQuery, "qryTemp"
DoCmd.DeleteObject acQuery, "qryTemp"
Rs.Close
Set Rs = Nothing
Exit Sub
Outputerr:
Rs.Close
Set Rs = Nothing
If IsNull(Dlookup("[Name]", "MSysObjects", "[Name] = 'qryTemp'")) = False Then
DoCmd.DeleteObject acQuery, "qryTemp"
End If
MsgBox Err.Description
End Sub
调用方法:
Private Sub Command5_Click()
OutputSubForm Me, Me.表1子窗体.Name
End Sub |
|