北京 | 上海 | 天津 | 重庆 | 广州 | 深圳 | 珠海 | 汕头 | 佛山 | 中山 | 东莞 | 南京 | 苏州 | 无锡 | 常州 | 南通 | 扬州 | 徐州 | 杭州 | 温州 | 宁波 | 台州 | 福州 | 厦门 | 泉州 | 龙岩 | 合肥 | 芜湖 | 成都 | 遂宁 | 长沙 | 株洲 | 湘潭 | 武汉 | 南昌 | 济南 | 青岛 | 烟台 | 潍坊 | 淄博 | 济宁 | 太原 | 郑州 | 石家庄 | 保定 | 唐山 | 西安 | 大连 | 沈阳 | 长春 | 昆明 | 兰州 | 哈尔滨 | 佳木斯 | 南宁 | 桂林 | 海口 | 贵阳 | 西宁 | 乌鲁木齐 | 包头 |
确定的事件过程代码如下
Private Sub cmdOK_Click()
On Error GoTo out_Excel_Err
Me.Refresh
If IsNull(Me.startDate) Then
MsgBox "请输入开始日期!", vbCritical, "提示"
Me.startDate.SetFocus
Exit Sub
End If
If IsNull(Me.endDate) Then
MsgBox "请输入结束日期!", vbCritical, "提示"
Me.endDate.SetFocus
Exit Sub
End If
If Me.startDate > Me.endDate Then
MsgBox "开始日期不允许大于结束日期!", vbCritical, "提示"
Me.startDate.SetFocus
Exit Sub
End If
'方法二,将Access表中的数据写入到Excel报表相对应的单元格
'需要引用DAO 3.6 和Microsoft EXCEL 9.0 或Microsoft EXCEL 11
Dim ExcelApp As Excel.Application
Dim Book As Excel.Workbook
Dim ws As Worksheet
DoCmd.SetWarnings False
Dim xlsPath As String
xlsPath = "C:\Users\wenqiang.xia\Desktop\proessMIS" & "\daily mill.xlsx"
'xlsPath = CurrentProject.Path & "\Daily Report.xlsx"
Set ExcelApp = New Excel.Application
'打开指定的Sheet表
Set ws = ExcelApp.Workbooks.Open(xlsPath).Worksheets("Processing")
'生成daily actual表 daily actual表
DoCmd.OpenQuery "qrydaily mill", acViewNormal
Dim rst1 As DAO.Recordset
'检测Excel文件是否存在,如果不存在则提醒
If Dir(xlsPath) = "" Then
MsgBox "Daily Report.xls文件不存在,请将Excel文件与mdb文件放在同一目录下", vbCritical, "提示"
End
End If
Set rst1 = CurrentDb.OpenRecordset("daily mill", dbOpenDynaset)
rst1.MoveFirst
Do Until rst1.EOF
Dim a As Integer
a = DateDiff("day", [startDate], [endDate])
Dim b As Integer
If a = 0 Then b = 6 Else: b = a + 6
ws.Cells(b, 2) = rst1("Tonnes Milled")
ws.Cells(b, 4) = rst1("CV6# Grade")
ws.Cells(b, 6) = rst1("CV6# Recovery")
ws.Cells(b, 8) = rst1("CV6# Gold oz Produced")
rst1.MoveNext
Loop
ExcelApp.Visible = True '显示EXCEL让操作员可以看得见,不然就只是在进程中而不可见
Set ExcelApp = Nothing
Set Book = Nothing
out_Excel_Exit:
Exit Sub
out_Excel_Err:
MsgBox Error$
Resume out_Excel_Exit
End Sub
哪位老师有时间能把我检查一下,究竟哪里出问题了,谢谢!