【问题标题】:Using an array as a property in Access VBA在 Access VBA 中使用数组作为属性
【发布时间】:2020-07-13 19:30:39
【问题描述】:

伙计们,

我正在尝试在 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 类型,因为我不确定如何定义返回或设置数组的属性。

【问题讨论】:

  • 我很好奇 i:=3425。我以前没见过。对不起,我不能提供更多帮助。
  • 该子程序在签名中有 5 或 6 个可选参数。您使用:= 表示法只传递一个参数,以便编译器知道它是哪个参数。

标签: ms-access vba


【解决方案1】:

我已经有一段时间没有检查这个了,但 fp1.AddImportHistory (ih1) 应该是 fp1.AddImportHistory ih1,没有括号吗?

【讨论】:

    【解决方案2】:

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

    fp1.AddImportHistory (ih1)
    

    Call fp1.AddImportHistory (ih1)
    

    问题是我知道这一点,只是掩饰了它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-10
      • 2011-07-17
      • 1970-01-01
      • 2018-10-20
      • 1970-01-01
      • 1970-01-01
      • 2017-07-14
      • 1970-01-01
      相关资源
      最近更新 更多