Public Function GetFile() As String
Dim dlgOpen As FileDialog
Set dlgOpen = Application.FileDialog(msoFileDialogOpen)
With dlgOpen
.AllowMultiSelect = True
.Show
End With
GetFile = dlgOpen.SelectedItems(1)
Set dlgOpen = Nothing
End Function
Private Sub 导到EXCEL_Click()
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim rs As New ADODB.Recordset
Dim sql As String
Dim i As Long, j As Long, m As Long
Dim fname As String
On Error GoTo 导出_Err
fname = GetFile
Set xlApp = CreateObject("Excel.Application")
xlApp.Application.Visible = True
Set xlBook = xlApp.Workbooks.Open(fname)
DoCmd.GoToRecord acDataForm, "主窗", acFirst
m = 0
For j = 1 To DCount("*", "主表")
sql = "select * from 子表 where 票ID=" & Me.票ID.Value
rs.Open sql, CurrentProject.Connection, adOpenKeyset, adLockOptimistic
'导出主表
xlBook.Application.Sheets("Sheet1").Select
xlBook.Application.Cells(m + 1, 1).Value = "票ID"
xlBook.Application.Cells(m + 2, 1).Value = "进货日期"
xlBook.Application.Cells(m + 3, 1).Value = "制单人"
xlBook.Application.Cells(m + 1, 2).Value = Me.票ID.Value
xlBook.Application.Cells(m + 2, 2).Value = Me.进货日期.Value
xlBook.Application.Cells(m + 3, 2).Value = Me.制单人.Value
'导出子表
xlBook.Application.Cells(m + 4, 1).Value = "产品ID"
xlBook.Application.Cells(m + 4, 2).Value = "票ID"
xlBook.Application.Cells(m + 4, 3).Value = "产品"
xlBook.Application.Cells(m + 4, 4).Value = "数量"
For i = 1 To rs.RecordCount
xlBook.Application.Cells(m + i + 4, 1).Value = rs("产品ID")
xlBook.Application.Cells(m + i + 4, 2).Value = rs("票ID")
xlBook.Application.Cells(m + i + 4, 3).Value = rs("产品")
xlBook.Application.Cells(m + i + 4, 4).Value = rs("数量")
rs.MoveNext
Next
m = m + i + 4 + 1
rs.Close
DoCmd.GoToRecord acDataForm, "主窗", acNext
Next
DoCmd.GoToRecord acDataForm, "主窗", acFirst
xlBook.Save
xlApp.Quit
Set xlApp = Nothing
Set xlBook = Nothing
导出_Exit:
Exit Sub
导出_Err:
MsgBox "数据错误,请检查!"
Resume 导出_Exit
End Sub作者: goto2008 时间: 2009-12-7 11:00
谢谢todaynew 。。我好好瞄瞄。。作者: todaynew 时间: 2009-12-7 11:16
在中间加一段代码,可以清除表中原来的内容:
。。。。。
xlApp.Application.Visible = True
Set xlBook = xlApp.Workbooks.Open(fname)
xlBook.Application.Sheets("Sheet1").Select
xlBook.Application.Cells.Select
xlBook.Application.Selection.ClearContents
xlBook.Application.Range("A1").Select
DoCmd.GoToRecord acDataForm, "主窗", acFirst
。。。。。作者: goto2008 时间: 2009-12-7 11:45
大哥,你这样变成导出表里当所记录。。。。。汗。
我是想导出当前主子窗体对应ID关联的记录。。。。作者: todaynew 时间: 2009-12-7 12:15