Private Sub cmd导出_Click()
On Error GoTo Err_cmd导出_Click
Dim qdf As DAO.QueryDef
Dim strWhere, strSQL As String
strWhere = Me.数据查询查询子窗体.Form.Filter
If strWhere = "" Then
strSQL = "SELECT * FROM [数据查询]"
Else
strSQL = "SELECT * FROM [数据查询] WHERE " & strWhere
End If
Set qdf = CurrentDb.QueryDefs("查询结果")
qdf.SQL = strSQL
qdf.Close
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
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")
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作者: rjacky 时间: 2008-1-24 23:01
这里有个现成的例子供参考