类函数中的错误91

使用Access 2010,我收集信息并将其放在Excel电子表格上。 当我运行下面的代码,我越来越

运行时错误“91”:
对象variables或未设置块

在我的class上在这一行Set Cci = ChartColorItems(ColorID) Public Function GetRGB(ByRef ColorID As String) As Integer

“ChartColors”类:

 Option Compare Database Option Explicit Private pChartColorItems As Collection Public Property Get ChartColorItems() As Collection Set ChartColorItems = pChartColorItems End Property Public Property Set ChartColorItems(ByRef lChartColorItem As Collection) Set pChartColorItems = lChartColorItem End Property Public Function GetRGB(ByRef ColorID As String) As Integer Dim Cci As ChartColorItem Dim x As Integer '--------------------------------------------------- 'Error happens here: Set Cci = ChartColorItems(ColorID) '--------------------------------------------------- x = RGB(Cci.Red, Cci.Green, Cci.Blue) GetRGB = x Set Cci = Nothing End Function Private Sub Class_Initialize() Dim Cci As ChartColorItem Dim Colors As Collection Set Colors = New Collection Set Cci = New ChartColorItem Cci.Red = 149 Cci.Green = 55 Cci.Blue = 53 Cci.ColorID = "Pie1" Colors.Add Cci, Key:=Cci.ColorID Set Cci = Nothing Set Cci = New ChartColorItem Cci.Red = 148 Cci.Green = 138 Cci.Blue = 84 Cci.ColorID = "Pie2" Colors.Add Cci, Key:=Cci.ColorID Set Cci = Nothing End Sub 

和ChartColorItem类:

 Option Compare Database Option Explicit Private pColorID As String Private pRed As Integer Private pGreen As Integer Private pBlue As Integer Public Property Get ColorID() As String ColorID = pColorID End Property Public Property Let ColorID(ByRef x As String) pColorID = x End Property Public Property Get Red() As Integer Red = pRed End Property Public Property Let Red(ByRef x As Integer) pRed = x End Property Public Property Get Green() As Integer Green = pGreen End Property Public Property Let Green(ByRef x As Integer) pGreen = x End Property Public Property Get Blue() As Integer Blue = pBlue End Property Public Property Let Blue(ByRef x As Integer) pBlue = x End Property 

当我debugging时,代码步骤通过ChartColorItems() getter就好了,错误发生在End Function ,并且将我放在上面提到的行上。

这和我在本周早些时候写的一些代码非常相似,主要区别在于我使用Class_Initialize子类来填充ChartColors,因为我试图存储一组固定的颜色,而我之前的代码正在收集数据并以更“正常”的方式将其插入课堂。

您将模块级别的私有集合定义为pCharColorItems ,但是您绝不会在类的intialize方法中初始化它。 而是使用本地范围的Colors集合variables。

 Private Sub Class_Initialize() Dim Cci As ChartColorItem Dim Colors As Collection Set Colors = New Collection 

您需要使用pChartColorItems

 Private Sub Class_Initialize() Dim Cci As ChartColorItem Dim Colors As Collection Set pChartColorItems = New Collection Set Cci = New ChartColorItem Cci.Red = 149 Cci.Green = 55 Cci.Blue = 53 Cci.ColorID = "Pie1" pChartColorItems.Add Cci, Key:=Cci.ColorID Set Cci = Nothing Set Cci = New ChartColorItem Cci.Red = 148 Cci.Green = 138 Cci.Blue = 84 Cci.ColorID = "Pie2" pChartColorItems.Add Cci, Key:=Cci.ColorID Set Cci = Nothing End Sub 

但是GetRGB这行还有另外一个bug。

  x = RGB(Cci.Red, Cci.Green, Cci.Blue) 

RGB函数返回一个长整型时,将x声明为整数。 “Pie1”的值会导致溢出错误。