在Access VBA中使用数组作为属性

时间:2017-05-23 20:58:42

标签: access-vba

民间,

我正在尝试在VBA中编写一个类模块,该模块从Access数据库中读取一些文件名,以便将它们导入到同一个数据库中。我已经为这个数据库编写了几个类模块,它们似乎有效。但是,此模块具有类型clsImportFile的数组,我尝试将其用作属性。存在的实际错误是

Run-Time error '438': Object doesn't support this property or method

以下是clsFileProcessor定义:

Option Compare Database
Option Explicit

Private pFiles As Variant
Private pIndex As Long
Private pSize As Long

Property Get Files() As Variant
    Files = pFiles
End Property

Property Let Files(f As Variant)
    pFiles = f
End Property

Property Get Index() As Long
    Index = pIndex
End Property

Property Let Index(i As Long)
    pIndex = i
End Property

Property Get Size() As Long
    Size = pSize
End Property

Property Let Size(s As Long)
    pSize = s
End Property

Public Sub Initialize()
    Dim FileArray() As Variant
    ReDim FileArray(Size) As Variant
    Files = FileArray()
End Sub

Public Sub IncrementIndex()
    pIndex = pIndex + 1
End Sub

Public Sub IncrementSize()
    Dim newSize As Integer
    newSize = pSize + 10
    Dim newFiles() As Variant
    ReDim newFiles(newSize) As Variant
    Dim i As Integer
    For i = 0 To Index
        newFiles(i) = pFiles(i)
    Next i
    Files = newFiles()
End Sub

Public Sub AddImportHistory(ih As clsImportHistory)
    If Index + 1 = Size Then
        IncrementSize
    Else
        Files(Index) = ih
        IncrementIndex
    End If
End Sub

这是我工厂的Create sub:

Public Function CreateFileProcessor() As clsFileProcessor
    Set CreateFileProcessor = New clsFileProcessor
    CreateFileProcessor.Index = 0
    CreateFileProcessor.Size = 10
    CreateFileProcessor.Initialize
End Function

最后,这是测试子:

Public Sub TestFileProcessor()
    Dim fp1 As clsFileProcessor
    Set fp1 = CreateFileProcessor()
    Debug.Assert fp1.Index = 0
    Debug.Assert fp1.Size = 10
    Dim ih1 As clsImportHistory
    Set ih1 = CreateImportHistory(i:=3425)
    Dim ih2 As clsImportHistory
    Set ih2 = CreateImportHistory(i:=3426)
    fp1.AddImportHistory (ih1)
    Debug.Assert False
End Sub

fp1.AddImportHistory (ih1)是错误所指向的,但我怀疑它与数组有关。

修改 我通过评论clsFileProcessor.AddImportHistory中的所有内容,然后系统地取消评论问题与Files属性的分配相关的行。我Dimming Files属性为Variant。这是我的问题吗?我使用的是Variant类型,因为我不确定如何定义返回或设置数组的属性。

2 个答案:

答案 0 :(得分:0)

我检查过这段时间已经有一段时间了,但是fp1.AddImportHistory (ih1)应该是fp1.AddImportHistory ih1,而不是parens?

答案 1 :(得分:0)

我被跟踪了几天,但当我回到这里时,我发现我需要改变

fp1.AddImportHistory (ih1)

Call fp1.AddImportHistory (ih1)

麻烦的是我知道这一点而且只是掩饰它。