[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 531
  • Last Modified:

CDaoRecordset: "Item not found in this collection"

(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
Claude050897
Asked:
Claude050897
  • 4
  • 2
  • 2
  • +1
1 Solution
 
MacRenaCommented:
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
 
MacRenaCommented:
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
 
MacRenaCommented:
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
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 
MacRenaCommented:
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
 
dovholukCommented:
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
 
Claude050897Author Commented:
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
 
dovholukCommented:
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
 
NosterdamusCommented:
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
 
NosterdamusCommented:
Sorry...

If you are using VB 4.0

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

Featured Post

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

  • 4
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now