MS Access VBA 在需要时使用 ReDim Preserve 增加数组的大小(如在按钮单击事件处理程序方法或循环中)
MS Access VBA using ReDim Preserve to increase the size of an array when needed( as in within a button click event handler method or within a loop )
我是 Microsoft Office Professional Plus 2013 Access 新手。
我正在使用以下方法开发应用程序:
-Microsoft Office Professional Plus 2013 Access
我是我的 VBA 编辑器,我有以下 Class 模块:
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' 对象变量或 With 块变量未设置
在下一行
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
我是 Microsoft Office Professional Plus 2013 Access 新手。
我正在使用以下方法开发应用程序:
-Microsoft Office Professional Plus 2013 Access
我是我的 VBA 编辑器,我有以下 Class 模块:
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' 对象变量或 With 块变量未设置
在下一行
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