?
Solved

CDaoRecordset: "Item not found in this collection"

Posted on 2001-08-07
9
Medium Priority
?
520 Views
Last Modified: 2007-12-19
(From Visual C++ I use DAO (data access object) to work with a Access database.)

I'm going mad! Why do I get this message?

1) I open the recordset with:
m_rsTitle.Open (dbOpenDynaset, "SELECT TitleID, Title FROM Title");

2) Everytime I want to read or write to the set I do:
m_rsTitle.SetFieldValue ("Title", szBuffer);

With each read or write I get "Item not found in this collection"
I've tried using SetFieldValue (1, szBuffer) but it gives the same error.
The weird thing is that just yesterday it worked for the first record that I add,
but then after that it only gave the error. And it is not just with either GetFieldValue() or
SetFieldValue(), but with both.

Any suggestions?
0
Comment
Question by:Claude050897
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 4

Expert Comment

by:MacRena
ID: 6359364
m_rsTitle.Open (dbOpenDynaset, "SELECT TitleID, Title FROM Title");

To Open the Recordset:

m_rsTitle = CurrentDb.OpenRecordset("SELECT TitleID, Title FROM Title;", dbOpenDynaset)

To Add a Record:

CurrentDb.Execute "INSERT INTO Title (TitleID, Title) VALUES ("SMIDAV", "Dave Smith");"

To Read a Record:

MyString = DLookup("Title", "Title", "TitleID = 'SMIDAV'")


0
 
LVL 4

Expert Comment

by:MacRena
ID: 6359376
Oops, always hit Submit before reading what I typed...
Try that again...

****************


To Open the Recordset:

Set m_rsTitle = CurrentDb.OpenRecordset("SELECT TitleID, Title FROM Title", dbOpenDynaset)


To Add a Record:

CurrentDb.Execute "INSERT INTO Title (TitleID, Title) VALUES ("SMIDAV", "Dave Smith");"


To Read a Record:

MyString = DLookup("Title", "Title", "TitleID = 'SMIDAV'")
0
 
LVL 4

Expert Comment

by:MacRena
ID: 6359421
That would be for individual records...

If you are doing a loop, then it would be better to open and read the recordset like this...

*****************************
Dim db As Database
Dim m_rsTitle As DAO.Recordset
Dim strID As String
Dim strName As String

Set db = CurrentDb
Set m_rsTitle = db.OpenRecordset("SELECT Title.* FROM Title, dbOpenDynaset)

Do Until m_rsTitle.EOF
          strID = m_rsTitle!TitleID
          strName = m_rsTitle!Title
          MsgBox "ID = " & strID & " And Name = " & strName
          m_rsTitle.MoveNext
Loop
m_rsTitle.Close
Set m_rsTitle = Nothing
Set db = Nothing


******************************

Or if you want to add a Record...

******************************

Dim db As Database
Dim m_rsTitle As DAO.Recordset

Set db = CurrentDb
Set m_rsTitle = db.OpenRecordset("SELECT Title.* FROM Title, dbOpenDynaset)

m_rsTitle.AddNew
m_rsTitle!TitleID = InputBox("Enter the ID of the new person")
m_rsTitle!Title = InputBox("Enter the Name of the new person")
m_rsTitle.Update

m_rsTitle.Close
Set m_rsTitle = Nothing
Set db = Nothing

0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
LVL 4

Expert Comment

by:MacRena
ID: 6359439
BTW...

Having field names the same as table names is confusing.

MyString = DLookup("Title", "Title", "TitleID = 'SMIDAV'")



I recommend using prefixes to differentiate Table names from Field names...

MyString = DLookup("fldTitle", "tblTitle", "fldTitleID = 'SMIDAV'")

0
 
LVL 8

Expert Comment

by:dovholuk
ID: 6360824
MacRena,

i think claude is looking for c++ code, not access vba, but i could be mistaken...

claude,
i've never used the CDAORecordSet class, but it seems straightforward. i do have some questions though...

1.) do you have dbOpenDynaset defined somewhere as a constant?

2.) m_rsTitle has been declared as a new CDAORecordSet, right?

3.) the only info on the web i could find quickly about CDAORecordSet is from ms. they use a slightly different syntax than you do. such as:
rs.SetFieldValue(_T("Customer_ID"), varFieldValue);
vs what you have (similiar, but slightly different)
m_rsTitle.SetFieldValue ("Title", szBuffer);
admittedly, i don't quite know what the _T() does in the MS version. i would guess it's a method that determines what the integer value is of the field in question...

4.) are you calling m_rsTitle.AddNew(); with m_rsTitle.Update()???

just a few questions that might spark you...

dovholuk
0
 

Author Comment

by:Claude050897
ID: 6362814
Yip, I want c++ code.

1) dbOpenDynaset is a MFC constant
2) m_rsTitle is declared as a CDaoRecordset
3) I think the _T() just constructs a string
4) Yip
0
 
LVL 8

Accepted Solution

by:
dovholuk earned 600 total points
ID: 6363608
i can see why you are going "mad"!!! lol...

have you posted this question in the c++ area? i'm no MFC expert, maybe someone there has had this issue in the past.

after further exploration, i agree that _T() returns a string but i still don't have a clue as to why it'll work one time.

are you using CDaoDatabase db; or some other call as such? maybe your not closing down the db properly and it is left in a read-only mode???

geesh... all i could think of is to create a copy of your db and try it with a "clean" copy.  do this by creating a blank db (not just copying/pasting) and importing all the objects from one db to another...

*shrug*

good luck.

dovholuk
0
 
LVL 7

Expert Comment

by:Nosterdamus
ID: 6364815
Hi Claude,

If you are using VB 4.0, then this link (FIX: "Item not found in this collection" with MFC DAO Join) might have the solution for your problem (another M$ bug):
http://support.microsoft.com/support/kb/articles/Q139/9/97.asp

This link (FIX: "Item not found in this collection" Message Box) might be interesting as well:
http://support.microsoft.com/support/kb/articles/Q148/8/48.asp

Hope this helps,

Nosterdamus
0
 
LVL 7

Expert Comment

by:Nosterdamus
ID: 6364829
Sorry...

If you are using VB 4.0

Should be:
If you are using C++ 4.0 ofcourse....
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question