简单的函数记录到数组中 [英] simple function record into array

查看:59
本文介绍了简单的函数记录到数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我是vba的新手,因此遇到了问题。

i确实有一些记录

FG

---------------

2 3

2 5

6 7

2 3

和那些我真的想插入一个阵列,但不记得怎么做了这个在VBA中的这个


可以帮助我吗? plzz。

Hi,
Im a rookie in vba, and therefor got a problem.
i do have folwing records
F G
---------------
2 3
2 5
6 7
2 3
and those i realy wanto plug into a array, but cant remember how to do
this in VBA
can any one help me ? plzz.

推荐答案

Geagleeye写道:
Geagleeye wrote:
FG
--- ------------
2 3
2 5
6 7
2 $

和那些我真正想要的插件成阵,但不记得怎么办这个在VBA中
任何人都能帮帮我吗? plzz。
F G
---------------
2 3
2 5
6 7
2 3
and those i realy wanto plug into a array, but cant remember how to do
this in VBA
can any one help me ? plzz.




DAO和ADO都有记录集对象的GetRows方法。

GetRows从记录集中检索指定的数量

并将它们放入一个二维数组中。



Both DAO and ADO have a GetRows method of the recordset object.
GetRows retrieves a specified number or rows from a recordset
and places them into an 2-D array.


" Geagleeye" <乌尔******** @ hotmail.com>在消息中写道

news:11 ********************** @ o13g2000cwo.googlegr oups.com ...
"Geagleeye" <Ur********@hotmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...


我是vba的新手,因此遇到了问题。
我确实有啰嗦记录
FG
------ ---------
2 3
2 5
6 7
2 $

和那些我真的想插入阵列,但不记得怎么办这个在VBA中
任何人都可以帮助我吗? plzz。
Hi,
Im a rookie in vba, and therefor got a problem.
i do have folwing records
F G
---------------
2 3
2 5
6 7
2 3
and those i realy wanto plug into a array, but cant remember how to do
this in VBA
can any one help me ? plzz.




我假设你想从数据库中的表中获取这些数据并且你要求b $ b不要求关于如何将这些''硬编码''值转换成

二维数组。

作为rkc,指出你可以使用DAO或ADO编码。这里显示的是ADO版本

,因为我没有指定返回的行数 - 默认情况下

检索所有记录:

Public Sub TestArray()


On Error GoTo Err_Handler


Dim avar As Variant

Dim lngCount As Long


avar = CreateArray(" SELECT F,G FROM MyTable")

for lngCount = LBound(avar)To UBound(avar)

MsgBox CStr(avar(0,lngCount))

下一个lngCount


Exit_Handler:

退出Sub


Err_Handler:

MsgBox Err.Description,vbExclamation,"错误号:" &安培; Err.Number

恢复Exit_Handler


结束子

公共函数CreateArray(strSQL As String)As Variant


错误GoTo Err_Handler


Dim rst作为ADODB.Recordset


设置rst =新ADODB.Recordset


rst.Open strSQL,CurrentProject.Connection


CreateArray = rst.GetRows()


Exit_Handler :


如果不是第一个那么

如果rst.State<> adStateClosed然后

rst.Close

结束如果

设置rst = Nothing

结束如果


退出函数


Err_Handler:

MsgBox Err.Description,vbExclamation,"错误号:" &安培; Err.Number

恢复Exit_Handler


结束功能




I assume you want to get this data from a table in the database and that you
are not asking about how to get those ''hard-coded'' values into a
2-dimensional array.
As rkc, points out you can use DAO or ADO coding. The ADO version is shown
here since I do not have to specify the number of rows returned - by default
all records are retrieved:
Public Sub TestArray()

On Error GoTo Err_Handler

Dim avar As Variant
Dim lngCount As Long

avar = CreateArray("SELECT F, G FROM MyTable")

For lngCount = LBound(avar) To UBound(avar)
MsgBox CStr(avar(0, lngCount))
Next lngCount

Exit_Handler:
Exit Sub

Err_Handler:
MsgBox Err.Description, vbExclamation, "Error No: " & Err.Number
Resume Exit_Handler

End Sub
Public Function CreateArray(strSQL As String) As Variant

On Error GoTo Err_Handler

Dim rst As ADODB.Recordset

Set rst = New ADODB.Recordset

rst.Open strSQL, CurrentProject.Connection

CreateArray = rst.GetRows()

Exit_Handler:

If Not rst Is Nothing Then
If rst.State <> adStateClosed Then
rst.Close
End If
Set rst = Nothing
End If

Exit Function

Err_Handler:
MsgBox Err.Description, vbExclamation, "Error No: " & Err.Number
Resume Exit_Handler

End Function


对于lngCount = LBound(avar,2 )到UBound(avar,2)




For lngCount = LBound(avar, 2) To UBound(avar, 2)

?


这篇关于简单的函数记录到数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆