我正在开发一个程序,它可以从表中的一个字段获取数据,并将整个列放入一个数组中,甚至只是从表本身读取。代码似乎使用了一个表单或其他我想使用数组的东西。
答案 0 :(得分:7)
这将有效:
Dim rstData As DAO.Recordset
Dim v As Variant
Set rstData = CurrentDb.OpenRecordset("select FirstName from FaxBook3")
v = rstData.GetRows(rstData.RecordCount)
" V"现在将成为所有名字的数组。如果您的查询有多列,则数组将是/可以是多维的。
答案 1 :(得分:4)
这是一个简单的示例,说明如何获取表中列的内容并将其动态添加到数组中:
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
答案 2 :(得分:0)
另一种方法是使用以下语句: 西班牙语(español):Otra forma de hacerlo es de la siguiente manera:
CLIENTSLIST表:一列
Me.ListFileContent.RowSourceType =“表/查询”
Me.ListFileContent.RowSource =“CLIENTSLIST”
注意:ListBox显示所有数据。
答案 3 :(得分:0)
MoveLast
和MoveFirst
。
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)