新增文章
文章标题
分类
C#
云星空
K3 BOS
K3 功能
用友
Oracle
python
SQL
MySql
PHP
HTML
script
windows
Access
影视后期
财务
服务
生活
内容
下面的代码示例使用 SQL 语句从名为“Employees”的表中检索三个字段,放入 Recordset 对象中。 然后,它使用 GetRows 方法检索 Recordset 的前三条记录,并将所选的记录存储在一个二维数组中。 然后,它通过使用两个数组索引来选择特定字段和记录,从而输出每条记录(一次一个字段)。 为了显示数组索引的使用方式,以下示例使用一个单独的语句来标识和输出每条记录的每个字段。 在实践中,更加可靠的做法是:使用两个相互嵌套的循环,并为逐步执行数组的两个维度的索引提供整数变量。 Sub GetRowsTest() On Error GoTo aErr Dim aDatabase As DAO.Database Dim aRecordset As DAO.Recordset Dim aSql As String: aSql = "select * from Cust where FCustNo = '" & Me.FCustNo.Value & "'" Dim aRecords As Variant Dim aRows As Integer, aRow As Integer '行数 Dim aCols As Integer, aCol As Integer '列数 Set aDatabase = CurrentDb Set aRecordset = aDatabase.OpenRecordset(aSql) Debug.Print "记录数: " & aRecordset.RecordCount 'aSql = "UPDATE inventory SET stock = " & stock_new & " WHERE item_no = """ & inItemNo & """" 'aDatabase.Execute aSql aRecords = aRecordset.GetRows(3) aRows = UBound(aRecords, 2) + 1 aCols = UBound(aRecords, 1) + 1 aRecordset.MoveFirst '定位记录 移到第一条 For aRow = 0 To aRows - 1 For aCol = 0 To aCols - 1 Debug.Print aRecords(aCol, aRow) Next aCol Debug.Print "FCustName: " & aRecordset.Fields("FCustName").Value '读取记录 字段值 aRecordset.MoveNext '定位记录 移到下一条 Next aRow aRecordset.Close aDatabase.Close Set aRecordset = Nothing Set aDatabase = Nothing Exit Sub aErr: MsgBox "Error #: " & Err.Number & vbCrLf & vbCrLf & Err.Description End Sub
返回
保存