Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using eval() for flexible recordset fieldnames 1

Status
Not open for further replies.

waldemar

Programmer
Nov 15, 2001
245
DE
and another (probably) quick one:

This function is supposed to empty a specified field in a table. The function accepts a parameters for the name of the field.... but what is correct eval() usage?

Function emptyfield(tablename As String, fieldname As String) As Boolean
Dim db As Database, rs As Recordset
Set db = CurrentDb
Set rs = db.OpenRecordset("SELECT * FROM [" & tablename & "];", dbOpenDynaset)
If rs.EOF = True And rs.BOF = True Then
emptyfield = False
rs.Close: Set rs = Nothing: db.Close: Set db = Nothing
Exit Function
End If
rs.MoveFirst
Do Until rs.EOF = True
rs.Edit
rs![eval(fieldname)] = &quot;&quot; <<<<<<< !!!!
rs.Update
rs.MoveNext
Loop

rs.Close: Set rs = Nothing: db.Close: Set db = Nothing
End Function

appreciate any ideas...
regards
waldemar
 
Eval (&quot;rs![&quot; & fieldname & &quot;] = &quot; & Chr$(34) & Chr$(34))

didnt work either...
 
Use the following syntax
rs(fieldname)=&quot;&quot;
This works even if the fieldname has spaces in it. You do not need string delimiters around fieldname because it is a variable. However, if the field does not accept zero length values the procedure will raise an error. This is a setting in the field properties. There should be error handling in the code. An update query would be faster.
 
There should also be an assignment at the end
emptyfield=true
otherwise the function will return false even when it is successful.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top