2

我正在开发一个程序,该程序可以从表中的一个字段中获取数据,并将整列放入一个数组中,甚至只是从表本身中读取。代码似乎使用了表单或其他我想使用数组的东西。

4

4 回答 4

8

这将起作用:

Dim rstData    As DAO.Recordset
Dim v          As Variant

Set rstData = CurrentDb.OpenRecordset("select FirstName from FaxBook3")
v = rstData.GetRows(rstData.RecordCount)

“v”现在将是所有名字的数组。如果您的查询有多个列,则该数组将/可以是多维的。

于 2014-08-12T23:22:33.563 回答
5

这是一个简单的示例,说明如何获取表中列的内容并将其动态添加到数组中:

Option Compare Database
Option Explicit

Public Sub loadIntoArray()

Dim rstTableName As DAO.Recordset   'Your table
Dim myArray() As String             'Your dynamic array
Dim intArraySize As Integer         'The size of your array
Dim iCounter As Integer             'Index of the array

'Open your table
Set rstTableName = CurrentDb.OpenRecordset("Table1")

If Not rstTableName.EOF Then

    rstTableName.MoveFirst   'Ensure we begin on the first row

    'The size of the array should be equal to the number of rows in the table
    intArraySize = rstTableName.RecordCount - 1
    iCounter = 0
    ReDim myArray(intArraySize) 'Need to size the array

    Do Until rstTableName.EOF

        myArray(iCounter) = rstTableName.Fields("Field1")
        Debug.Print "Item: "; iCounter & " " & myArray(iCounter)

        iCounter = iCounter + 1
        rstTableName.MoveNext
    Loop

End If

If IsObject(rstTableName) Then Set rstTableName = Nothing

End Sub
于 2013-04-30T23:02:11.330 回答
0

另一种方法是使用以下语句: 西班牙语 (español): Otra forma de hacerlo es de la siguiente manera:

CLIENTSLIST 表:一列

Me.ListFileContent.RowSourceType = "表/查询"

Me.ListFileContent.RowSource = "客户列表"

注意:ListBox 显示所有数据。

于 2015-03-06T14:44:07.540 回答
0

上面 Albert 的解决方案将起作用,但您需要将记录集移到末尾,然后再移回来以完全填充数组,否则您将只能获得第一行。使用MoveLastMoveFirst

Dim rstData    As DAO.Recordset
Dim v          As Variant

Set rstData = CurrentDb.OpenRecordset("select FirstName from FaxBook3")
rstData.MoveLast
rstData.MoveFirst  
v = rstData.GetRows(rstData.RecordCount)
于 2017-12-05T11:15:50.653 回答