View Full Version : DAO trying to assing a value to a txtbox from rs


kawi6rr
05-11-2011, 10:32 AM
Run-time error 2113
The value you entered isn’t valid for this field.

The txtbox is unbound so I don’t think that’s the problem. The field value CFS in the table is a date and I’m setting CFS as an object so this is where I’m guessing things are going wrong. I can't set CFS as a date the way I have it set up. Can anyone help me get this either formatted correctly or figured out?


Sub addCFSDate()

Dim rs As DAO.Recordset
Set rs = CurrentDb.OpenRecordset("SELECT CFS FROM LoadDateCFS", dbOpenDynaset)

Dim CFS As Object

Set CFS = rs("CFS")

Do While Not rs.EOF
'rs.MoveFirst
Debug.Print rs![CFS] ' compare this syntax to the usual syntax in the example below
rs.MoveNext

Loop

Me.txtCFS = rs!CFS

rs.Close
Set rs = Nothing

End Sub


Thanks!!

kawi6rr
05-11-2011, 11:04 AM
I figured out the problem. I had my txtbox call in the wrong place, the code works below.

Do While Not rs.EOF
Me.txtCFS.value = rs![CFS]
rs.MoveNext
Loop

How do I get it to call the most recent record?

pbaldy
05-11-2011, 11:28 AM
You'd replace

"SELECT CFS FROM LoadDateCFS"

with an SQL statement that only pulled the last record. Don't know your data structure, but that might be with a totals query and Max(), TOP 1, or ?