N
Niklas Östergren
Hi!
I´m trying to write a simple function to lookup a string value in a linked
table (NOT local). And If I don´t remember wrong I can´t use SEEK-method for
this so I try to use *FindFirst* method but can´t get it to work.
I´m sure I´m making it more difficult than it is and I have tried for some
time but can´t get it right. Any help are apreciated!
TIA!
// Niklas
Her´s my code:
============================================
Public Function FindFirstStringRecord(strTableName As String, strFieldName
As String, intRecordID As Integer) As String
Dim db As Database
Dim rec As Recordset
Dim SQL As String
Dim strSearchString As String
Dim strMsg As String
strSQL = "SELECT * FROM " & strTableName
strSearchString = " WHERE " & strFieldName & " = " & intRecordID
'Check validity of passed parameters.
' Exit sub presenting Msg if not valid
If strSQL = "" Or strSearchString = "" Then
strMsg = "SQL-string and/or string to search for is missing."
MsgBox strMsg
Exit Function
Else
Set db = Currentdb()
Set rec = db.OpenRecordset(strSQL, dbOpenSnapshot)
With rec
' rec.Index = "PrimaryKey"
.FindFirst strSearchString
' If found, get value
If Not .NoMatch Then
' If value <> 0 send value to calling sub else exit function
If .Fields(strFieldName) <> "" Then
FindFirstStringRecord = .Fields(strFieldName)
End If
End If
' Close the recordset
.Close
End With
End If
Set rec = Nothing
Set db = Nothing
End Function
I´m trying to write a simple function to lookup a string value in a linked
table (NOT local). And If I don´t remember wrong I can´t use SEEK-method for
this so I try to use *FindFirst* method but can´t get it to work.
I´m sure I´m making it more difficult than it is and I have tried for some
time but can´t get it right. Any help are apreciated!
TIA!
// Niklas
Her´s my code:
============================================
Public Function FindFirstStringRecord(strTableName As String, strFieldName
As String, intRecordID As Integer) As String
Dim db As Database
Dim rec As Recordset
Dim SQL As String
Dim strSearchString As String
Dim strMsg As String
strSQL = "SELECT * FROM " & strTableName
strSearchString = " WHERE " & strFieldName & " = " & intRecordID
'Check validity of passed parameters.
' Exit sub presenting Msg if not valid
If strSQL = "" Or strSearchString = "" Then
strMsg = "SQL-string and/or string to search for is missing."
MsgBox strMsg
Exit Function
Else
Set db = Currentdb()
Set rec = db.OpenRecordset(strSQL, dbOpenSnapshot)
With rec
' rec.Index = "PrimaryKey"
.FindFirst strSearchString
' If found, get value
If Not .NoMatch Then
' If value <> 0 send value to calling sub else exit function
If .Fields(strFieldName) <> "" Then
FindFirstStringRecord = .Fields(strFieldName)
End If
End If
' Close the recordset
.Close
End With
End If
Set rec = Nothing
Set db = Nothing
End Function