设为首页收藏本站Access中国

Office中国论坛/Access中国论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

[帮助] 求导出按钮代码

[复制链接]
1#
发表于 2008-12-23 08:31:28 | 显示全部楼层
2#
发表于 2008-12-23 20:29:32 | 显示全部楼层
Private Sub Command13_Click()
    Dim Qdf As DAO.QueryDef
    Dim rs As New ADODB.Recordset
    Dim strSQL As String, strCriteria As String
    Dim ctl As Control

    If IsNull(Me.Text14) Then
        MsgBox "Enter the Path frist"
        Me.Text14.SetFocus
        Exit Sub
    End If
    Set Qdf = CurrentDb.QueryDefs("Q")
    For Each ctl In Me.Controls
        If TypeOf ctl Is OptionButton Then
            If ctl Then
                strCriteria = strCriteria & "'" & ctl.Name & "',"
            End If
        End If
    Next
    If strCriteria = "" Then
        strSQL = "select * from 表1 order by 籍贯"
    Else
        strSQL = "select * from 表1 where 籍贯  in (" & strCriteria & ") order by 籍贯"
    End If
    Qdf.SQL = strSQL
    strSQL = "select distinct 籍贯 from Q"
    With rs
        .Open strSQL, CurrentProject.Connection, adOpenKeyset, adLockOptimistic
        Do While Not .EOF
            Set Qdf = CurrentDb.QueryDefs("Out")
            strSQL = "select * from 表1 where 籍贯='" & .Fields(0) & "'"
            Qdf.SQL = strSQL
            DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "Out", Me.Text14, , .Fields(0)
            .MoveNext
        Loop
        .Close
    End With
    Set rs = Nothing
    Set Qdf = Nothing
End Sub


[ 本帖最后由 Henry D. Sy 于 2008-12-23 20:31 编辑 ]

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
3#
发表于 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 编辑 ]
4#
发表于 2008-12-24 11:29:47 | 显示全部楼层
需要引用 Microsoft Office 11
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2024-5-22 11:31 , Processed in 0.112128 second(s), 27 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

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