Solved

CDaoRecordset: "Item not found in this collection"

Posted on 2001-08-07
9
497 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
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
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 
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 200 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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

777 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