Access交流中心

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

如何将子窗体的数据 生成一个新表

wangydm  发表于:2014-10-11 16:15:59  
复制

因子窗体的数据来源,有可能是 表,查询,SQL语句这几种 ,所以非常麻烦,而且子窗体并不一定会显示完表或查询的内容.

我经过摸索,自己写了一个,勉强可以用,但觉得还是不够好,希望能有大师指点 一下.

sub temp()


    Dim rst As Object           'DAO.Recordset or ADODB.Recordset
    Dim ctl As Control
    Dim strItem As String
    Dim blnIsADORecordset As Boolean

  If Me.sfrList.Form.CurrentRecord < 1 Then
        Exit Sub
    End If
    Const errUnsupportedProperty = 438




    Set rst = Me.sfrList.Form.Recordset
    blnIsADORecordset = IsADORecordset(rst)
    strItem1 = ""
    tblname = "tblstock2"
    strWhere = Me.sfrList.Form.Filter   &&是否有筛选
    If InStr(Me.sfrList.Form.RecordSource, "Select") > 0 Then   &&是否SQL语句
        strSQL = Replace(Me.sfrList.Form.RecordSource, "From", "into " & tblname & " From")
        If Nz(strWhere) <> "" Then
           strSQL = Replace(strSQL, "ORDER BY", "where  " & strWhere & " ORDER BY") 
         End If
         Debug.Print strSQL
        DoCmd.RunSQL strSQL
        
    Else   &&如不是SQL语句,表或查询,则扫描子窗体显示的字段
        For Each ctl In Me.sfrList.Form.Controls
        If InStr(ctl.Name, "_Label") = 0 Then   &&如为标签则不提取
            If Len(ctl.ControlSource) > 0 Then
                If Not ctl.ControlSource Like "=*" Then
                    strItem = "[" & ctl.ControlSource & "]"
                    strItem1 = strItem1 & ", " & strItem
                End If
            End If
            End If
NextField:
        Next
              If Nz(strWhere) <> "" Then
               strWhere = "where  " & strWhere
               Else
               strWhere = " "
         End If
        strSQL = "Select " & Right(strItem1, Len(strItem1) - 1) & "into " & tblname & " From " & Me.sfrList.Form.RecordSource & " " & strWhere
        Debug.Print strSQL
         DoCmd.RunSQL strSQL
    End If
    
    
    
ExitHere:
    Set rst = Nothing
    Exit Sub


ErrorHandler:
    Select Case Err.Number
    Case errUnsupportedProperty
        Resume NextField
    Case Else
        MsgBoxEx Err.Description, vbCritical
        Resume ExitHere
    End Select
end sub

 

Top
煮江品茶 发表于:2014-10-11 20:04:12
没看懂想干什么

朱先生 发表于:2014-10-11 21:18:22

题目:如何将子窗体的数据 生成一个新表

答:将 ME.子窗体,FORM.控件   的数据利用 INSERT INTO 语句 追加到新表中  



朱先生 发表于:2014-10-11 23:02:04

写得太复杂,让人看不懂

只需7-11行代码即可搞定




wangydm 发表于:2014-10-13 09:17:25
感谢大家的回复 ,因为 我想写一个通用的子窗体数据生成 新表,  你们简单的代码给我看看...

朱先生 发表于:2014-10-13 14:18:34
子窗体数据生成的新表存在当前数据库中还是存在My Documents中?

朱先生 发表于:2014-10-13 23:06:12

不要写太多,简单事情简单做

一个按钮,两行代码

一行代码导出,一行代码导入

My Documents中有,当前数据库中也有

改一下子窗体名可通用




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