|
我也来凑个热闹
导出窗体Recordset为任意Access支持格式的数据文件
以下代码将DAO的RecordSet(包括Access窗体的Recordset)中的当前数据导出为任何Access支持的导出格式。无论你这个Recordset是从窗体的,还是代码创建的,是筛选后结果,还是链接了字段的主窗体,本程序均能正确导出结果,格式包括任何Access支持的导出格式。
'调用范例
Private Sub Botton_Click()
Call Output_Recordset(Me.Recordset)
End Sub
<DIV class=quote>
'公共模块
Option Compare Database
Option Explicit
Public Sub Output_Recordset(ByRef frmRs As DAO.Recordset)
Dim frmField As DAO.Field
Dim daoDbs As DAO.Database
Dim daoRs As DAO.Recordset
Dim strSQL As String
Dim strFields As String
Set daoDbs = Application.CurrentDb
Set daoRs = frmRs.Clone
strSQL = "CREATE TABLE USysDAORecordsetOutport"
strFields = "("
For Each frmField In daoRs.Fields
strFields = strFields & frmField.Name & " "
Select Case frmField.Type
Case dbBigInt:
strFields = strFields & "Currency"
Case dbBinary:
strFields = strFields & "Binary"
Case dbBoolean:
strFields = strFields & "Bit"
Case dbByte:
strFields = strFields & "TinyInt"
Case dbChar:
strFields = strFields & "Char"
Case dbCurrency:
strFields = strFields & "Money"
Case dbDate:
strFields = strFields & "DateTime"
Case dbDecimal:
strFields = strFields & "Decimal"
Case dbDouble:
strFields = strFields & "Double"
Case dbFloat:
strFields = strFields & "Float"
Case dbGUID:
strFields = strFields & "Guid"
Case dbInteger:
strFields = strFields & "Integer"
Case dbLong:
strFields = strFields & "Long"
Case dbLongBinary:
strFields = strFields & "LongBinary"
Case dbMemo :
strFields = strFields & "Memo"
Case dbNumeric:
strFields = strFields & "Numeric"
Case dbSingle:
strFields = strFields & "Single"
Case dbText:
strFields = strFields & "Text(" & frmField.Size & ")"
Case dbTime:
strFields = strFields & "Time"
Case dbTimeStamp:
strFields = strFields & "DateTime"
Case dbVarBinary:
strFields = strFields & "VarBinary"
End Select
strFields = strFields & ","
Next frmField
strFields = Left(strFields, Len(strFields) - 1) & ")"
On Error Resume Next
daoDbs.Execute "DROP TABLE USysDAORecordsetOutport"
On Error GoTo 0
daoDbs.Execute strSQL & strFields
daoRs.MoveFirst
Do Until daoRs.EOF
strSQL = "INSERT INTO USysDAORecordsetOutport("
strFields = " Values("
For Each frmField In daoRs.Fields
If Not IsNull(frmField.Value) And Not IsEmpty(frmField.Value) Then
strSQL = strSQL & frmField.Name & ","
If frmField.Type = dbText Then
strFields = strFields & "'" & frmField.Value & "',"
Else
strFields = strFields & frmField.Value & ","
End If
End If
Next frmField
strSQL = Left(strSQL, Len(strSQL) - 1) & ")"
strFields = Left(strFields, Len(strFields) - 1) & ")"
daoDbs.Execute strSQL & strFields
daoRs.MoveNext
Loop
DoCmd.OutputTo acOutputTable, "USysDAORecordsetOutport"
daoDbs.Execute "DR |
|