DAO Update

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi There,

Is there any way to dynamically change a field name in the update code? I
have the following code: The String UpdStr contains the name of a field in my
table that I want to update. I keep getting the error that no such name
exists in the table.
Say the contents of UpdStr was "Aspiration" which is a fieldname in my
table. How do I code it to a field to be updated?

Public Sub WriteConfig()
Dim SizeVar As Variant
Dim UpdStr As String
SizeVar = Len(Me.TagName) - 3
UpdStr = Mid(Me.TagName, 1, SizeVar)
Dim dbAces As DAO.Database
Dim rst As DAO.Recordset
Dim NewDate As Field
Set dbAces = CurrentDb
Set rst = dbAces.OpenRecordset("AcesData")
rst.MoveLast
With rst
.Edit
![UpdStr] = Me.TagID
!ChangeDate = Now
.Update
.Close
End With
dbAces.Close
End Sub
 
Hi John,

I tried that also. For some reason it doesn't like the parentheses. In
Debugger the value is okay but the message is always error 3265 "Item not
found in this collection"

The syntax is as the reference suggests, however I even tried applying the
same syntax to the valid !ChangeDate field as !Fields("ChangeDate") and get
the same message there!
 
Hurray!

Finally got it! It doesn't like the "with" New Code as follows:

Public Sub WriteConfig()
Dim SizeVar As Variant
Dim UpdStr As String
SizeVar = Len(Me.TagName) - 3
UpdStr = Mid(Me.TagName, 1, SizeVar)
Dim dbAces As DAO.Database
Dim rst As DAO.Recordset
Dim NewDate As Field
Set dbAces = CurrentDb
Set rst = dbAces.OpenRecordset("AcesData")
rst.MoveLast
rst.Edit
rst.Fields(UpdStr) = Me.TagID
rst.Fields("ChangeDate") = Now
rst.Update
rst.Close
dbAces.Close
End Sub
--
Marc


John Spencer said:
Try referring to the field as follows

!Fields(UpdStr )= Me.TagID


Marc said:
Hi There,

Is there any way to dynamically change a field name in the update code? I
have the following code: The String UpdStr contains the name of a field in
my
table that I want to update. I keep getting the error that no such name
exists in the table.
Say the contents of UpdStr was "Aspiration" which is a fieldname in my
table. How do I code it to a field to be updated?

Public Sub WriteConfig()
Dim SizeVar As Variant
Dim UpdStr As String
SizeVar = Len(Me.TagName) - 3
UpdStr = Mid(Me.TagName, 1, SizeVar)
Dim dbAces As DAO.Database
Dim rst As DAO.Recordset
Dim NewDate As Field
Set dbAces = CurrentDb
Set rst = dbAces.OpenRecordset("AcesData")
rst.MoveLast
With rst
.Edit
![UpdStr] = Me.TagID
!ChangeDate = Now
.Update
.Close
End With
dbAces.Close
End Sub
 
John Spencer said:
Try referring to the field as follows

!Fields(UpdStr )= Me.TagID

Should have been

.Fields(UpdStr )= Me.TagID

That was probably the only problem.
 
Back
Top