1、写两个自定义函数如下:
Public Function AccToExl(frm As Form)
'示例:AccToExl(me.子窗体.form)
Dim ssql As String
Dim strwh As String
ssql = frm.RecordSource
ssql = Trim(ssql)
If Right(ssql, 1) = ";" Then
ssql = Left(ssql, Len(ssql) - 1)
End If
ssql = "select * from (" & ssql & ") where "
strwh = "True"
If frm.Filter <> "" Then
strwh = strwh & " and " & frm.Filter
End If
ssql = ssql & strwh
CrtQDef "TempQ", ssql
DoCmd.OutputTo acOutputQuery, "TempQ", acFormatXLS
DoCmd.DeleteObject acQuery, "TempQ"
End Function
Public Function CrtQDef(strname As String, strsql As String)
'创建查询
Dim Qdef As QueryDef
Set Qdef = CurrentDb.CreateQueryDef(strname)
Qdef.SQL = strsql
Qdef.Close
Set Qdef = Nothing
End Function