views:

640

answers:

3

Does anyone have an idea what this error means or how to solve it? I am using Access 2003 and SQL2005. It comes up when trying to add a record on a particular subform.

[Microsoft][SQL Native Client] Invalid character value for cast specification (#0)

This MS bug report describes the same message, but it is a bug in SQL Server 6.5 that has already been solved.

Solved: Apparently having no PK on the destination table was causing this, it didn't have anything to do with the subform or the query from Access. I wasn't even aware there were tables in this database without PK. Adding PK to the destination table solved it. The strange thing is the same query string that errored when executed via SQL native client, executed through SSMS with no errors. Hope this helps anyone else who has come across that strange message.

+3  A: 

Is one of your fields in the view calculated/built with the CAST function? In this case, you might not have the right to update/add a value for that field.

Can you execute your view in the MS SQL Studio interface and try to insert a record?

Philippe Grondier
Hi, I don't know what is actually being executed. Inserting a record on the form itself works fine, but trying to insert it when that form is a subform causes this error. The subform just inserts a single text value, no functions, no vba.
Dale Halliwell
in my opinion, the message you get means that the view uses the CAST function somewhere to calculate a value in a field, so the corresponding field cannot be updated. If the problem is specific to the subform, then the link between the form and subform, or the subform recordset's primary key, might be built on this calculated value.
Philippe Grondier
I am having a look at what is actually happening with the SQL Profiler now, thanks for telling me about it, I can't believe I didn't even know about this before.
Dale Halliwell
Ok, I found the statement that was being executed using SQL Profiler just a normal insert using sp_executesql, pasted it into SSMS and it ran up no problem (1 row(s) affected). Other subforms that have the same kind of insert statement execute fine inside MS Access, so I am just trying to narrow down what the difference is that is causing this one to fail. There is no CAST function anywhere.
Dale Halliwell
A: 

Based solely in the message you provided above, it appears that you are trying to set an invalid value to some field or parameter, etc... The message is telling you that it is trying to convert a value into an specific data type but the value is invalid for that data type... makes sense?

Please add more details so we can help you better.

Ricardo
+6  A: 

Hum, I would check the text box default on the access side. I would also bring up the linked table in design mode, and you want to check the data type that ms-access assumes here. For non supported data types ms-access will generally use a string, and sql server might be wanting something else.

So, check both the Primary key (PK) in main table, and then check the data type used (assumed) in the child table for the foreign key (FK) column. While we are at this, check your expressions used for the child/master link settings in the sub-form control (not the form, not the sub-form, but the sub-form control used in your form that links up these two tables).

Sub forms in access are sensitive if you don’t have a timestamp column in the sql server table. As mentioned check the PK and the FK data types and make sure they match up (just bring up the tables in design mode in ms-access -- you get an error message about the design mode being read only, but just continue on so you can check/view to ensure the data types match up).

So for the child table, you need a PK, a FK, and also a timestamp column (you don’t have to display the TS column in the sub-form, but you need it in the table).

Sub-forms in ms-access are sensitive and often fail if you don’t include a timestamp column in the sql table. (access uses these row version columns to determine if the data been changed).

Albert D. Kallal
You were right.. 2 hours later.. it was because the child table had no PK.. grr!
Dale Halliwell
To second Albert's good answer, let me just say that I've just made it a practice in SQL Server of including a timestamp field in every table as a matter of course -- it just makes things easier. I'd never have a table without a PK in any db engine, so that is not a rule I need to force myself to follow!
David-W-Fenton
nice deduction +1
Philippe Grondier