arrays - MS Access VBA 使用 ReDim Preserve 在需要时增加数组的大小(如在按钮单击事件处理程序方法中或在循环中)
问题描述
我是 Microsoft Office Professional Plus 2013 Access 的新手。
我正在使用以下方法开发应用程序:
-Microsoft Office Professional Plus 2013 Access
我是我的 VBA 编辑器,我有以下类模块:
Option Explicit
Option Compare Database
Private cntrollingPersonFullNameProp As String
Private cntrollingPersonIsNameAddressProvidedProp As String
Private cntrollingPersonIsDOBProvidedProp As String
Private cntrollingPersonIsTaxResidenceProvidedProp As String
Private cntrollingPersonIsControllingPersonTypeProvidedProp As String
Private cntrollingPersonIsSignedAndDatedProp As String
Public Property Get CntrollingPersonFullName() As String
CntrollingPersonFullName = cntrollingPersonFullNameProp
End Property
Public Property Let CntrollingPersonFullName(lCntrollingPersonFullName As String)
cntrollingPersonFullNameProp = lCntrollingPersonFullName
End Property
Public Property Get CntrollingPersonIsNameAddressProvided() As String
CntrollingPersonIsNameAddressProvided = cntrollingPersonIsNameAddressProvidedProp
End Property
Public Property Let CntrollingPersonIsNameAddressProvided(lCntrollingPersonIsNameAddressProvided As String)
cntrollingPersonIsNameAddressProvidedProp = lCntrollingPersonIsNameAddressProvided
End Property
Public Property Get CntrollingPersonIsDOBProvided() As String
CntrollingPersonIsDOBProvided = cntrollingPersonIsDOBProvidedProp
End Property
Public Property Let CntrollingPersonIsDOBProvided(lCntrollingPersonIsDOBProvided As String)
cntrollingPersonIsDOBProvidedProp = lCntrollingPersonIsDOBProvided
End Property
Public Property Get CntrollingPersonIsTaxResidenceProvided() As String
CntrollingPersonIsTaxResidenceProvided = cntrollingPersonIsTaxResidenceProvidedProp
End Property
Public Property Let CntrollingPersonIsTaxResidenceProvided(lCntrollingPersonIsTaxResidenceProvided As String)
cntrollingPersonIsTaxResidenceProvidedProp = lCntrollingPersonIsTaxResidenceProvided
End Property
Public Property Get CntrollingPersonIsControllingPersonTypeProvided() As String
CntrollingPersonIsControllingPersonTypeProvided = cntrollingPersonIsControllingPersonTypeProvidedProp
End Property
Public Property Let CntrollingPersonIsControllingPersonTypeProvided(lCntrollingPersonIsControllingPersonTypeProvided As String)
cntrollingPersonIsControllingPersonTypeProvidedProp = lCntrollingPersonIsControllingPersonTypeProvided
End Property
Public Property Get CntrollingPersonIsSignedAndDated() As String
CntrollingPersonIsSignedAndDated = cntrollingPersonIsSignedAndDatedProp
End Property
Public Property Let CntrollingPersonIsSignedAndDated(lCntrollingPersonIsSignedAndDated As String)
cntrollingPersonIsSignedAndDatedProp = lCntrollingPersonIsSignedAndDated
End Property
在表单代码文件中,
Dim cntrollingPersonsArray() As CntrollingPerson
Private Sub AddControllingPersonBtn_Click()
Dim cntrlPerson As New CntrollingPerson
cntrlPerson.CntrollingPersonFullName = …….
cntrlPerson.CntrollingPersonIsNameAddressProvided = …..
ReDim Preserve cntrollingPersonsArray(UBound(cntrollingPersonsArray)+ 1)
cntrollingPersonsArray(UBound(cntrollingPersonsArray)) = cntrlPerson
End Sub
该应用程序抛出:
'91' 对象变量或未设置块变量
在以下行
cntrollingPersonsArray(UBound(cntrollingPersonsArray)) = cntrlPerson
我尝试了一堆不同的代码修改
ReDim Preserve cntrollingPersonsArray(UBound(cntrollingPersonsArray))
或者
ReDim Preserve cntrollingPersonsArray(0 to UBound(cntrollingPersonsArray))
或者
ReDim Preserve cntrollingPersonsArray(1 to UBound(cntrollingPersonsArray))
有人可以告诉我要采取哪些步骤来纠正上述问题吗?
解决方案
使用集合对象而不是数组。你所有的问题都解决了!
例子:
Option Explicit
Private cntrollingPersons As New Collection
Private Sub AddControllingPersonBtn_Click()
Dim cntrlPerson As New CntrollingPerson
cntrlPerson.CntrollingPersonFullName = ""
cntrlPerson.CntrollingPersonIsNameAddressProvided = ""
cntrollingPersons.Add cntrlPerson
End Sub
推荐阅读
- regex - Ruby:通过大写字母和首字母缩略词将字符串分解为单词
- javascript - [Discord.Js]如何在另一台服务器上获取新的存档(数组)
- python-3.x - 获取 ec2 信息的 boto3 代码 - 但需要知道卷加密状态
- encryption - 如何安全地存储我需要检索的信息?
- scala - 哪个版本的 SBT ?scala2.13.3
- google-cloud-dataflow - 订购消息发布/订阅 GCP
- java - spring @Async :只标记异步执行的候选者,不保证?
- javascript - 结合 SetInterval 调用多个 Ajax 函数
- python - 关于列表中的声明列表和赋值值的python错误
- html - tokenize2 下拉菜单未以模式显示