Solved

CDaoRecordset: "Item not found in this collection"

Posted on 2001-08-07
9
495 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
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 4

Expert Comment

by:MacRena
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 4

Expert Comment

by:MacRena
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 8

Expert Comment

by:dovholuk
Comment Utility
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
Comment Utility
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 200 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Sorry...

If you are using VB 4.0

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

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Today's users almost expect this to happen in all search boxes. After all, if their favourite search engine juggles with tens of thousand keywords while they type, and suggests matching phrases on the fly, why shouldn't they expect the same from you…
In the article entitled Working with Objects – Part 1 (http://www.experts-exchange.com/Microsoft/Development/MS_Access/A_4942-Working-with-Objects-Part-1.html), you learned the basics of working with objects, properties, methods, and events. In Work…
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…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

772 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now