Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

TTable.FindKey blow up

Status
Not open for further replies.

mwitwer

Programmer
May 14, 2002
13
US
I am getting an exception on a FindKey call. The error states that the name of my primary key must have a value ('Field "ID" must have a value'). This seems to only happen when the dataset is empty. Now, this dataset is in a shared data module. The error occurs after a form with a dataset component (that points to the same physical table as the shared one) has posted. The FindKey is done to reposition the record pointer in the shared dataset; this is when the exception occurs. Interestingly, a lookup dataset in the shared data module (again pointing to the same table) does a FindKey as well, but does not error. Any one have any ideas about what may be going on here? Or a possible solution/workaround?

This is a tricky problem. This does not happen to me on my dev machine. I've stepped through the code, seen that the primary key value is indeed empty, but the FindKey works properly for me. It only happens on other machines after the app has been installed.

 
That error message sounds like you are posting a record to a table that has a field that requires a value, and you're not supplying said value.

You might want to put a little bit of logic just before you Post, saying something like, if ID is empty, then do something about it.

If it's only occuring on client boxes and not on your dev box, the version of the table on your dev box may be different. Use Database Desktop or something to check the one on your dev box, and go Info Structure. Under Validity checks, see if for the ID field there's a check mark in the "Required Field" check box. Then do the same on a dev box, and compare. The key file here is the .VAL, as that's where the validity info is stored.

BTW, take a look at Locate as an alternative to FindKey. Locate (at least allegedly) works even if you have a set of fields listed that aren't in the current index. -- Doug Burbidge mailto:dougburbidge@yahoo.com
 
I've tried all those things, except the Locate. I will definitely try the Locate.
 
I tried to use Locate instead of FindKey, and I got the same error. It actually worked fine on my dev machine; again, it did not work on test machines.
 
Would the {$I+}, {$I-} compiler switch have any affect on TTable.FindKey? I noticed this in the code of my project in a routine where there was no other file I/O going on, but there was a FindKey, in fact the FindKey came immediately after it.

(I didn't put it there; I inherited this project :=)
 
No, $I shouldn't make any difference; that's for direct file I/O stuff, rather than database stuff.

I'm really surprised that putting logic just before the Post, to check if ID is empty, isn't working for you.

Another possibility is that it's not the explicit Post that's causing the problem, but rather an implicit post. Quoth the help:

"Dataset methods that change the dataset state, such as Edit, Insert, or Append, or that move from one record to another, such as First, Last, Next, and Prior automatically call Post."

So it could be one of those that is popping the error.

You could try adding an exception handler to the app, to trap this exception, and maybe ShowMessage() a bunch of likely debugging information. -- Doug Burbidge mailto:dougburbidge@yahoo.com
 
Finally found the problem with my FindKey exception. There was nothing wrong with the code at all. Someone who had worked on the same project years before said that they had encountered a similar problem, and he finally decided to remove the dataset component from the form and re-add it. That was it. The component must have gotten "corrupted" somehow. I did the same thing, and my problem was solved. So, if you are ever using any VCL database components and you can't understand why it is not working, try this method before you start trying all kinds of strange workarounds! I was I had.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top