Solved Seek issue (1 Viewer)

Dumferling

Member
Local time
Today, 03:46
Joined
Apr 28, 2020
Messages
102
I am trying to get the database to check if a record exists. I am using Seek to do this where the code will pick up the record number from the primary table and search on the related table for that index number (IDContract).

The code is:
Dim db As DAO.Database
Dim rst As DAO.Recordset
Dim vblIndex As Integer
vblIndex = Forms!frmCContractBrowser!ID

'On Error GoTo ErrorHandler

Set db = CurrentDb()
Set rst = db.OpenRecordset("tblCPOPIA", dbOpenDynaset)

' The index name for Employee ID.
rst.Index = "IDContract"
rst.Seek "=", vblIndex

If rst.NoMatch Then
Dim msg As String
msg = MsgBox("Record not found", vbOKOnly, "LMD Notification")
Else

End If

rst.Close
db.Close

Set rst = Nothing
Set db = Nothing

I am getting an error on the rstIndex = "IDContract" line - Operation is not supported for this kind of object.

I cannot see what I am doing wrong. It is something simple, I am sure but looking at examples, I can't see it.
 

Ranman256

Well-known member
Local time
Yesterday, 21:46
Joined
Apr 9, 2015
Messages
4,339
use dlookup, only 1 line:

Code:
vRet = Dlookup("[IDContract]", "tblCPOPIA","[IDContract]=" & Forms!frmCContractBrowser!ID

if isnull(vRet) then
   msgbox "Not Found"
else
   msgbox "found"
endif
 

jdraw

Super Moderator
Staff member
Local time
Yesterday, 21:46
Joined
Jan 23, 2006
Messages
15,379
Or use DCount

If Dcount("*", "tblCPOPIA","[IDContract]=" & Forms!frmCContractBrowser!ID) > 0 Then
..record exists
else
....record does not exist
end if
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 09:46
Joined
May 7, 2009
Messages
19,231
I am getting an error on the rstIndex = "IDContract" line - Operation is not supported for this kind of object.
you need to Open it using the option dbOpenTable:

Set rst = db.OpenRecordset("tblCPOPIA", dbOpenTable)
' The index name for Employee ID.
rst.Index = "IDContract"
rst.Seek "=", vblIndex
 

Pat Hartman

Super Moderator
Staff member
Local time
Yesterday, 21:46
Joined
Feb 19, 2002
Messages
43,223
Some operations do NOT work on linked tables. I don't know if Seek is one of them. Either DLookup() or DCount() as have already been suggested are simple and work. I use DCount() for this task when I need to find out if a record exists..
 

pbaldy

Wino Moderator
Staff member
Local time
Yesterday, 18:46
Joined
Aug 30, 2003
Messages
36,124
The domain aggregate functions already suggested are simplest to do what you're doing. In other situations, I would never open a recordset on the entire table and then find the desired record with Seek. It would be very inefficient and slow on larger tables. I'd open the recordset on the desired record:

Set rst = db.OpenRecordset("SELECT * FROM tblCPOPIA WHERE IDContract = " & vblIndex, dbOpenDynaset)

Testing for rst.EOF would tell you if there were no matching records.
 

pbaldy

Wino Moderator
Staff member
Local time
Yesterday, 18:46
Joined
Aug 30, 2003
Messages
36,124
remember, we are using Index.

Well, maybe I don't understand the inner workings, but it seems like you're pulling the entire table over the wire before you use your index. I'm letting the database engine do the work and pulling a single record over the wire. @Pat Hartman certainly knows more than I do, perhaps she can weigh in.
 

Dumferling

Member
Local time
Today, 03:46
Joined
Apr 28, 2020
Messages
102
Thanks all. I though DLookup/DCount would be slower but quite possibly the Seek function is not working because it is a linked table, as suggested. There won't be thousands of records, more like a few hundred so I will use DLookup/ DCount. Thanks for the help
 

isladogs

MVP / VIP
Local time
Today, 02:46
Joined
Jan 14, 2017
Messages
18,209
See my article Check Record Exists which compares the speed (efficiency) of 4 different methods of doing this, both with indexed and non-indexed fields. Using indexed fields each of the methods should be almost instantaneous, even with large datasets
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 09:46
Joined
May 7, 2009
Messages
19,231
Seek function is not working because it is a linked table
of course it will work.
paste in a Module:
Code:
Public Function fnOpenLinkedTable(ByVal sTable As String, Optional ByVal IdxName As String) As Object
    ' arnelgp
    ' Note:
    '
    ' sTable must be a Linked (Access database Only) table.
    '
    Dim db As DAO.Database
    Dim td As DAO.TableDef
    Dim rs As DAO.Recordset
    Dim sDbPath As String
On Error GoTo Err_Handler
    Set db = CurrentDb
    Set td = db.TableDefs(sTable)
    sDbPath = Replace$(td.Connect, ";DATABASE=", "")
    Set db = DBEngine(0).OpenDatabase(sDbPath, False, False)
    Set rs = db.OpenRecordset(sTable, dbOpenTable)
    If Len(IdxName) <> 0 Then
        rs.Index = IdxName
    End If
    Set fnOpenLinkedTable = rs
exit_sub:
    Set td = Nothing
    Set db = Nothing
    Exit Function
Err_Handler:
    MsgBox err.Number & ": " & err.Description
    Resume exit_sub
End Function

your code:
Code:
Dim rst As DAO.Recordset
Dim vblIndex As Long
vblIndex = Forms!frmCContractBrowser!ID

'On Error GoTo ErrorHandler


Set rst = fnOpenLinkedTable("tblCPOPIA", "IDContact")

rst.Seek "=", vblIndex

If rst.NoMatch Then
    Dim msg As String
    msg = MsgBox("Record not found", vbOKOnly, "LMD Notification")
Else

End If

rst.Close
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 09:46
Joined
May 7, 2009
Messages
19,231
You can't use indexed seek on a linked table
that's a myth? i already showed you the code.
Try it first. I have tried it.

Look also at the last post (the one with check marked, meaning acceptable answer to the OP) of that link.
 
Last edited:

Users who are viewing this thread

Top Bottom