So I have been searching stackoverflow and I hit a wall. Please forgive any "poor practices" just need this logic to run once. This is the first time I'm using classes in VBA (if I had more time I would just have written this in C#)
Here is my problem. I have two custom classes. DocName and Doclib
Docname contains various information obtained per row in my sheet. I'm looking for uniqueness in the names of the "Staff". I couldn't do a simple search because the DB where I received this from had no rhyme or reason to how the names were stored. Sometimes full name, sometimes with middle initial, sometimes backwards, sometimes with credentials. It's maddening.
The abbreviated class is below:
Option Explicit
Public staff As Boolean
Public docID As Long
Public namesCount As Integer
Private names(5) As String
Public CNumber As String
Private fakeCnumber As Long
Public rowIndex As Integer
Public hasCnumber As Boolean
Private rawString As String
Private Sub class_initialize()
namesCount = 0
hasCnumber = False
rowIndex = 1
End Sub
Public Sub resetClass()
docID = 0
rowIndex = 1
resSize = 0
namesCount = 0
End Sub
Public Function match(comp As DocName) As Boolean
Dim goodMatch As Integer
goodMatch = 0
Dim myNames As Integer
Dim compNames As Integer
myNames = 0
compNames = 0
While (nameValid(myNames))
While (comp.nameValid(compNames))
If names(myNames) = comp.getName(compNames) Then
goodMatch = goodMatch + 1
End If
compNames = compNames + 1
Wend
myNames = myNames + 1
Wend
If goodMatch > 1 Then
match = True
Else
match = False
End If
End Function
Public Function nameValid(i As Integer) As Boolean
If i < namesCount Then nameValid = True
If i >= namesCount Then nameValid = False
End Function
Public Property Let savestate(orig As DocName)
Dim x As Integer
x = 0
docID = orig.getdocID
hasCnumber = orig.hasCnumber
If hasCnumber Then CNumber = orig.getCNumber
While orig.nameValid(x)
names(x) = orig.getName(x)
x = x + 1
Wend
End Property
I also have a class "DocLib". The sole goal of that class is to house each instance of DocName I want to save. I want it all in memory to do some post processing on it before I dump it back out to the excel sheet. Again abbreviated class below:
Option Explicit
Private res(500) As DocName
Private resSize As Integer
Private Sub class_initialize()
resSize = 0
End Sub
Public Function addDoc(n As DocName)
Dim x As Integer
Dim m As Boolean
m = True
x = 0
While x < resSize & resSize > 0
If res(x).match(n) Then
res(x).addOrder
If res(x).hasCnumber = False & n.hasCnumber = True Then
res(x).setCNumber = n.getCNumber
End If
m = False
End If
Wend
If Not m Or resSize = 0 Then
res(resSize) = New DocName
res(resSize).savestate = n 'This is where it breaks *****
'res(resSize) = n
resSize = resSize + 1
End If
End Function
When I run this I get error 91. Object variable or with block variable not set. I get this at the line above "Res(resSize).savestate = n"
I tried just simple assignment first but that gave me the same error. So I created the savestate function with no change. I also tried assigning a new doc to each position (line before the crash). I didn't do that initially and had the same error.
Any ideas? Thanks in advance. I'm sure this is a simple fix. I just need this bloody code to work once on a test set and then again on a 20k set and I'll never use it again.
- I tried changing the index from 0 to 1 at the start that didn't work. When I follow it through the debugger it gives me that error at the end of the class_initialize function for docname after the "new" statement. The docname class works everywhere else I need it. I tried a simple test where I manually gave it row numbers and it worked. As soon as I tried to store it myself, it broke.
Thanks guys! ~Chad
&where you should probably be usingAnd...