Access-VBA - Name field parameter

October 2016


I am calling a function that will search for the value of a specific field for precise registration. To do this I pass a parameter to my office on behalf of my table, the record ID and field name.

FindLinkedString Function (ByVal Table As String, ByVal ID As Integer, ByVal Field As String) As String   

Sun As Database mydb2   
Sun LinkedTable As Recordset   
Sun linkedTableCount As Integer   

Set mydb2 = CurrentDb ()   
Set LinkedTable = mydb2.OpenRecordset (Table)   

For linkedTableCount = 1 To LinkedTable.RecordCount   
If LinkedTable! ID = ID Then   
FindLinkedString = table & "! " Field &   
GoTo Fin_FindLinkedString   
End If   


End Function 

The problem is that FindLinkedString = table & "!" & Field, is obviously fair game since it will not find value in the db, it saves the string by turning "Table" and "Field" by the parameters (eg "reviews" Name "then I want that it appears "Smith")


You can use this:
... LinkedTable.MoveFirst While Not LinkedTable.EOF If LinkedTable.Fields ( "ID") = ID Then FindLinkedString = LinkedTable.Fields ( "Name") LinkedTable.Close mydb2.Close Set LinKedTable = Nothing Set mydb2 = Nothing Exit Function LinkedTable.MoveNext Wend End If '...

But it may be simpler:

... Set LinkedTable = mydb2.OpenRecordset ( "SELECT Name FROM" & Table & "WHERE ID =" & ID) FindLinkedString = LinkedTable.Fields (0) ...

Related :

This document entitled « Access-VBA - Name field parameter » from CCM ( is made available under the Creative Commons license. You can copy, modify copies of this page, under the conditions stipulated by the license, as this note appears clearly.