With Me.subMC.Form '子表单subMC
Dim intStartR As Integer '定义用户选择的开始记录号
Dim intEndR As Integer '定义用户选择的结束记录号
Dim intRows As Integer '定义用户选择的行数
'Dim i As Integer
'Dim strSQL As String
If .SelHeight > 0 Then
intStartR = .CurrentRecord
intEndR = .SelTop + .SelHeight
intRows = intEndR - intStartR '获取用户选择的记录,注意的是用户必须从上而下选择记录,而不能从下而上选择记录
'strSQL = "SELECT * INTO tmpTemp FROM whMC2 WHERE MCNo='" & .MCNo & "' AND Item='" & .Item & "'"
'If intEndR - intStartR >= 1 Then
' For i = (intStartR + 1) To intEndR
' strSql1 = "再把其他记录追加到表"
' Next i
'End If
Dim rst As ADODB.Recordset '定义当前记录集
Set rst = .RecordsetClone
Dim intFieldCount As Integer '定义并获取当前记录集的字段数量
intFieldCount = rst.Fields.Count
Dim arrGetR As Variant, x As Integer, y As Integer '定义记录集数组,上标和下标
arrGetR = rst.GetRows(intRows, .Bookmark) '获取记录集的指定行数
'=================================================================循环输出记录
For x = 0 To intRows - 1 '
For y = 0 To intFieldCount - 1
Debug.Print arrGetR(y, x) & ";";
Next y
Debug.Print vbCrLf
Next x
'=================================================================循环输出记录
End If
'DoCmd.RunSQL strSQL
'DoCmd.OutputTo acOutputTable, "tmpTemp", , , True
End With
' 建立电子表格 (用一个空的数据源新建一个电子表格,这样表格只有字段名一行)
CurrentDb.Execute "SELECT * INTO [Excel 8.0;DATABASE=" & sName & "].Sheet1 FROM (Select * From 人员表 Where false)"
' 以记录集方式打开电子表格
rst.Open "Select * from [Sheet1$]", dbs, adOpenForwardOnly, adLockOptimistic
' 然后在这里把数组中的数据循环添加到表格记录集中
rst.AddNew
rst!("姓名") = "张三"
rst.Update作者: tmtony 时间: 2008-3-11 23:03
看看, 他的原目的未达到, 倒引出了不少精彩代码作者: t小宝 时间: 2008-3-11 23:19 把10楼的代码改一点加到1楼的这段代码中应该可以实现吧,把 Debug.Print 改为 Add,不过我还没试过
'=================================================================循环输出记录
For x = 0 To intRows - 1 '
For y = 0 To intFieldCount - 1
Debug.Print arrGetR(y, x) & ";";
Next y
Debug.Print vbCrLf
Next x
'=================================================================循环输出记录作者: andymark 时间: 2008-3-11 23:21
如果要输出字段名称, X须从1开始作者: t小宝 时间: 2008-3-11 23:55 下面代码我测试过了,可行。在子窗体中选择记录,在主窗体的一个标签上单击。
Private Sub Label2_Click()
With Me.Xzc.Form '子表单subMC
If .SelHeight > 0 Then
Dim intStartR As Integer '定义用户选择的开始记录号
Dim intEndR As Integer '定义用户选择的结束记录号
Dim intRows As Integer '定义用户选择的行数
Dim dbs As New adodb.Connection
Dim rst1 As New adodb.Recordset ' 电子表格记录集
Dim rst As dao.Recordset '定义当前记录集
Dim arrGetR As Variant, x As Integer, y As Integer '定义记录集数组,上标和下标
Dim intFieldCount As Integer '定义并获取当前记录集的字段数量
Dim sName As String, st1 As String
Set rst = .RecordsetClone
'=================================================================循环输出记录
For x = 0 To intRows - 1
rst1.AddNew
For y = 0 To intFieldCount - 1
rst1.Fields(y) = arrGetR(y, x)
Next y
rst1.Update
Next x
'=================================================================循环输出记录
End If
End With