Access交流中心

北京 | 上海 | 天津 | 重庆 | 广州 | 深圳 | 珠海 | 汕头 | 佛山 | 中山 | 东莞 | 南京 | 苏州 | 无锡 | 常州 | 南通 | 扬州 | 徐州 | 杭州 | 温州 | 宁波 | 台州 | 福州 | 厦门 | 泉州 | 龙岩 | 合肥 | 芜湖 | 成都 | 遂宁 | 长沙 | 株洲 | 湘潭 | 武汉 | 南昌 | 济南 | 青岛 | 烟台 | 潍坊 | 淄博 | 济宁 | 太原 | 郑州 | 石家庄 | 保定 | 唐山 | 西安 | 大连 | 沈阳 | 长春 | 昆明 | 兰州 | 哈尔滨 | 佳木斯 | 南宁 | 桂林 | 海口 | 贵阳 | 西宁 | 乌鲁木齐 | 包头 |

如何利用窗体的筛选结果,生成一个新表?

羽扇子君  发表于:2010-12-23 07:46:30  
复制

如题

 

Top
煮江品茶 发表于:2010-12-23 08:54:35
请参见《有选择的导出Excel》一文。

煮江品茶 发表于:2010-12-23 09:12:43

上文的地址为:http://accessoft.com/blog/article-show.asp?userid=10802&Id=4734

 

主要是SQL语句的写法。可以利用子查询来写SQL语句:
ssql="select a.* from (select * from tbname) as a where a.名称 like '*公司*'" 
无论窗体的数据源时表或查询的名称,还是SQL语句,则都可以写为:
tbname=me.form.RecordSource
if mid(tbname,len(tbname)-1,1)=";" then
   tbname=left(tbname,len(tbname)-1)
end if
strWh=me.form.Filter
ssql="select * from " & tbname & " where " &  strWh



煮江品茶 发表于:2010-12-23 11:43:21

请参见《门道与热闹》一文。

 



羽扇子君 发表于:2010-12-24 17:14:52

谢谢老头子.

参照了你的"【Access小品】传递子窗体中选中记录",但因为是利用生成表导出 到Excel因此重点在窗体的RecordSetClone属性的使用.

为了后来者,特将之也分享一下:

Private Sub Command导出_Click()
Dim theTempData As Variant, fd As FileDialog, thePath As String, theTableName As String
Dim ctls As Fields, ctl As Field, rst As Recordset, rst2 As Recordset
    Set fd = Application.FileDialog(msoFileDialogSaveAs)
    With fd
        .Title = "导出至 .xls类型"
        .AllowMultiSelect = False
        .ButtonName = "确定(&O)"
    End With
    theAddTextBox "请选择一个 Excel格式(.xls)文件,或单击“取消”按钮以退出!"
    If fd.Show = -1 Then
        thePath = fd.SelectedItems(1)
        Set fd = Nothing
        Hourglass "正在导出,请稍候", True
        theTableName = theOptions("公司名称") & "猪群资料『部分』"
        '生成一个基于子窗体格式的空表
        DoCmd.RunSQL "CREATE TABLE " & theTableName & " (耳号 TEXT, 性别 TEXT,种类 TEXT,品种 TEXT,状态 TEXT,饲养员 TEXT,所在猪舍 TEXT)"
        '打开这个空表
        Set rst = CurrentDb.OpenRecordset(theTableName, dbOpenDynaset)
        'rst2 为欲导出的窗体的记录源
        Set rst2 = Me.Child猪群档案查询子窗体.Form.RecordsetClone
        rst2.MoveFirst
        Set ctls = rst.Fields
        Do Until rst2.EOF
            rst.AddNew
            For Each ctl In ctls
                ctl.Value = rst2.Fields(ctl.Name).Value
            Next ctl
            rst.Update
            rst2.MoveNext
            DoEvents
        Loop
        DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, theTableName, thePath & IIf(Right(thePath, 3) = "xls", "", ".xls")
        rst.Close
        Set rst = Nothing
        DoCmd.DeleteObject acTable, theTableName
        Hourglass , False
        Set rst2 = Nothing
        theAddTextBox
    End If
End Sub
注:theOptions("公司名称")  函数是用来读出公司名称的.可以自己替换

再次鸣谢老头子



总记录:4篇  页次:1/1 9 1 :