设为首页收藏本站Access中国

Office中国论坛/Access中国论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

12
返回列表 发新帖
楼主: qp370982
打印 上一主题 下一主题

[帮助] 求导出按钮代码

[复制链接]
11#
发表于 2008-12-24 08:10:06 | 只看该作者
  1. Dim ctl As Control
  2. Private Sub Command13_Click()
  3.     Dim Qdf As DAO.QueryDef
  4.     Dim rs As New ADODB.Recordset
  5.     Dim strSQL As String, strCriteria As String
  6.     Dim strPath As String
  7.     Dim diaFs As FileDialog
  8.     Set diaFs = Application.FileDialog(msoFileDialogSaveAs)
  9.     With diaFs
  10.         .Title = "导出为........"
  11.         .Show
  12.     End With
  13.     If diaFs.SelectedItems.Count > 0 Then
  14.         strPath = diaFs.SelectedItems(1)
  15.     End If
  16.     If strPath = "" Then
  17.         strPath = CurrentProject.Path & "\out.xls"
  18.     ElseIf Right(strPath, 4) <> ".xls" Then
  19.         strPath = strPath & ".xls"
  20.     End If
  21.     Set Qdf = CurrentDb.QueryDefs("Q")
  22.     For Each ctl In Me.Controls
  23.         If TypeOf ctl Is OptionButton Then
  24.             If ctl Then
  25.                 strCriteria = strCriteria & "'" & ctl.Name & "',"
  26.             End If
  27.         End If
  28.     Next
  29.     If strCriteria = "" Then
  30.         strSQL = "select * from 表1 order by 籍贯"
  31.     Else
  32.         strSQL = "select * from 表1 where 籍贯 in (" & strCriteria & ") order by 籍贯"
  33.     End If
  34.     Qdf.SQL = strSQL
  35.     strSQL = "select distinct 籍贯 from Q"
  36.     With rs
  37.         Set Qdf = CurrentDb.QueryDefs("Out")
  38.         .Open strSQL, CurrentProject.Connection, adOpenKeyset, adLockOptimistic
  39.         Do While Not .EOF

  40.             strSQL = "select * from 表1 where 籍贯='" & .Fields(0) & "'"
  41.             Qdf.SQL = strSQL
  42.             DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "Out", strPath, , .Fields(0)
  43.             .MoveNext
  44.         Loop
  45.         .Close
  46.     End With
  47.     Set rs = Nothing
  48.     Set Qdf = Nothing
  49.     Set diaFs = Nothing
  50. End Sub

  51. Private Sub Form_Load()
  52.     For Each ctl In Me.Controls
  53.         If TypeOf ctl Is OptionButton Then
  54.             ctl = False
  55.         End If
  56.     Next
  57. End Sub
复制代码

[ 本帖最后由 Henry D. Sy 于 2008-12-24 08:11 编辑 ]
12#
 楼主| 发表于 2008-12-24 11:26:15 | 只看该作者
Dim diaFs As FileDialog提示用户定义类型未定义
13#
发表于 2008-12-24 11:29:47 | 只看该作者
需要引用 Microsoft Office 11
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|站长邮箱|小黑屋|手机版|Office中国/Access中国 ( 粤ICP备10043721号-1 )  

GMT+8, 2024-5-2 06:16 , Processed in 0.100554 second(s), 25 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表