Referencing Fields with a variable

thejad

New member
Local time
Today, 12:07
Joined
Oct 25, 2007
Messages
6
Hi,

I am trying to create vba application which compares two records across specified fields. The results are then recorded to a different set of fields in a different table.

I have racked my brain, and for the life of me can not figure out how to do this.

Does anyone know how this task can be undertaken?

I had been experiencing a 3265 error, which i resolved rather inefficiently, and am now experiencing a 91 error.

I can post some code if that will help, but I just wanted to understand if it can be done, and how.

Thanks
 
What code have you been trying to use? A 91 is object variable not set.
 
Code

Please be warned that this is likely ugly and inefficient, but I have been trying to use some work arounds to make it work.

Sub UpdateGaps()

Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Dim updatetable As DAO.Recordset
Dim ident As String
Dim length As Integer
Dim xp(8) As Variant, coa(8) As Variant
Dim e(8) As Integer
Dim field(8) As String
Dim xpvalue As Integer, coavalue As Integer
Dim xpcoatype As String, edesc As String
Dim x As Integer
Dim xstr As String
Dim strfield As String
Dim fieldname As Object




'define field names
field(0) = "Price"
field(1) = "Amount(G)"
field(2) = "Mty Dt"
field(3) = "Commission"
field(4) = "Principal"
field(5) = "Broker ID"
field(6) = "Short Note4"

'define array lengths for looping
length = 6
Set dbs = CurrentDb()
Set rst = dbs.OpenRecordset("XPCOA", dbOpenDynaset)
Set updatetable = dbs.OpenRecordset("CancelCorrectErrorField", dbOpenDynaset)
rst.MoveFirst
' loop through records
Do Until rst.EOF
'reset variables
x = 0
xpvalue = 0
coavalue = 0
edesc = ""
'reset comparative variables
For x = 0 To length
xp(x) = 0
coa(x) = 0
e(x) = 0
Next x
With rst
'get the identifier to compare
ident = !Identifier
'pass values to function to record different lines for two specific record types
xpcoacheck xp(), coa(), rst, ident, edesc, xpvalue, coavalue
End With
x = 0
'check that both of the comperative entries are present
If xpvalue = 0 Or coavalue = 0 Then
'update table
With updatetable
.AddNew
!Identifier = ident
!ErrorResponse = edesc
.Update
End With
Else
'compare and enter errors into array
For x = 0 To length
If xp(x) <> coa(x) Then
e(x) = 1
End If
Next x
'update listing of error codes
With updatetable
.MoveLast
If ident = !Identifier Then
.Edit
!ErrorResponse = edesc
.Update
x = 0
For x = 0 To length
If e(x) > 0 Then
xstr = CStr(x)
'here's where it all starts to go wrong,
'I can't seem to update data to go into each column
'by position in the array. Every syntax either comes
' back as error 3265 or in it's current iteration
' error 91. I use the same code again, so If I can
' solve it once, applying it again shouldn't be a problem

fieldname = "ErrorField" & xstr
.Edit
!["fieldname"] = True

.Update
End If
Next x
Else
.AddNew
!Identifier = ident
!ErrorResponse = edesc
.Update
x = 0
For x = 0 To length
If e(x) > 0 Then
xstr = CStr(x)
fieldname = "ErrorField" & xstr
.Edit
![fieldname] = True
.Update
End If
Next x
End If
End With
End If
rst.MoveNext
Loop

End Sub

Public Function xpcoacheck(ByRef xp(), ByRef coa(), ByRef rst, ByRef ident, ByRef edesc, ByRef xpvalue, ByRef coavalue)
' this is a work around because i couldn't pull the
'field names directly from the field array

Dim xpcoatype As String
Dim length As Integer
length = 8
With rst

Do Until rst.EOF
Nextitemcheck:
xpcoatype = !Type
Select Case xpcoatype
Case "XP"
If xpvalue = 0 Then
xpvalue = rst.RecordCount
x = 0
For x = 0 To length
Select Case x
Case 0
xp(x) = ![Price]
Case 1
xp(x) = ![Amount(G)]
Case 2
xp(x) = ![Mty Dt]
Case 3
xp(x) = ![Commission]
Case 4
xp(x) = ![Principal]
Case 5
xp(x) = ![Broker ID]
Case 6
xp(x) = ![Short Note4]
Case Else
End Select
Next x
Else
edesc = edesc & "Multiple XP values " & rst.RecordCount
End If
Case "COA"
If coavalue = 0 Then
coavalue = rst.RecordCount
x = 0
For x = 0 To length
Select Case x
Case 0
coa(x) = ![Price]
Case 1
coa(x) = ![Amount(G)]
Case 2
coa(x) = ![Mty Dt]
Case 3
coa(x) = ![Commission]
Case 4
coa(x) = ![Principal]
Case 5
coa(x) = ![Broker ID]
Case 6
coa(x) = ![Short Note4]
Case Else
End Select
Next x
Else
edesc = edesc & "Multiple COA values " & rst.RecordCount
End If

Case Else
edesc = edesc & "Not an XP or COA Code " & rst.RecordCount
End Select

rst.FindNext ![Identifier] = ident
If .NoMatch Then
If coavalue = 0 Or xpvalue = 0 Then
edesc = edesc & "No Matches Found"
Exit Function
Else
Exit Function
End If
Else
GoTo Nextitemcheck
End If

rst.MoveNext

Loop
End With


End Function



thanks for your help, I know this is a lot of code.... I have hilighted the problem area in red. Thanks
 
Your problem is (at least the most obvious one) that you never instantiate the variable fieldname(which I would not use as it is likely a reserved word). You declare it as an object, but nowhere do you use the SET keyword to instantiate it. That's your error 91.
 
Thanks, now as I am sure you are aware, I am newbie, and unsure how I would go about "instantiating the variable fieldname" (which I will change thanks). Once I have instantiated the variable, will I be able to use the variable to call multiple field names in my loop to input the data as required?
 
Try using this instead:

Dim fld As DAO.Field

Set fld=updatetable(0)

etc.
 
Thanks.. that solved the 91 error, now I am back to the good ol' 3265.

Dim fld As DAO.field
Dim strfldnm As String
.
.
Set fld = updatetable(0)
.
.

For x = 0 To length
If e(x) > 0 Then
xstr = CStr(x)
.Edit
strfldnm = "ErrorField" & xstr
fld = strfldnm
!["fld"] = True ' this is the error stop point
.Update
End If
Next x

Any ideas why this might be happening? This is very similar to the error I got when i started trying to pass the field value using a string. The value is moving, the field name matches to the table field name.

Any ideas??

Thanks
 
I tried that as well and get the same Error in the same spot...
 
I found the answer and a syntax for doing what I wanted to do

updatetable(fld) = True

rather than

![fld] = True

I can't say that I totally understand, but it works!

Thanks
 
Glad you got it. I'm not as good with DAO as I am with ADO, so I didn't think of that.
 
I found the answer

updatetable(fld) = True

rather than

![fld] = True

I can't say that I totally understand
I can't say that any of us can! The only thing I understand is that we use "DOT Notation" :rolleyes:
 

Users who are viewing this thread

Back
Top Bottom