Search code examples
javascriptms-accessinternet-explorer-9internet-explorer-11

JavaScript Access Database SQL Data type mismatch in criteria expression


Edit: Answered/Workaround at the bottom of the question.

I have already seen all other questions regarding this same error, but i am very positive this is a unique scenario.

I am pretty much managing an Access database from a self built website using JavaScript, an I am having the following issue:

var evDate = new Date(hDate);
evDate.setFullYear(evDate.getFullYear() + 1);
evDate = evDate.toLocaleDateString().toString();
var SQL = "INSERT INTO PendingChanges([Eval Date]) VALUES ('" + evDate + "')";

This provides the following string:

"INSERT INTO PendingChanges([Eval Date]) VALUES ('‎04‎/‎19‎/‎2018')"

In IE9 this works fine, but in IE11 it gives me the error "Data type mismatch in criteria expression."

If i were to use the hDate value instead, which comes from an input with a date picker, without adding a year to it:

var SQL = "INSERT INTO PendingChanges([Eval Date]) VALUES ('" + hDate + "')";

I will end up with the following string:

"INSERT INTO PendingChanges([Eval Date]) VALUES ('‎04‎/‎19‎/‎2017')"

An this works just fine on IE9 and IE11. A workaround would be for me to add an "onchange" even on the hDate input, with this trigger take the value and convert it to a date with a year added to it, pass this new value to another input, and pass this new input to the main function. I am trying to avoid doing that.

Any ideas why IE11 is not "seeing" the string value on evDate as valid? (believe me, it looks identical as hDate, only difference is 2017/2018).

Edit: This is how i am using the SQL variable to make the queries to the database.

var pad = "Access Database File Path";
var cn = new ActiveXObject("ADODB.Connection");
var strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + pad;
cn.Open(strConn);
var rs = new ActiveXObject("ADODB.Recordset");
rs.Open(SQL, cn);
//if i close the rs after the rs.Open, the function returns an error that the change can not be completed on a closed connection, so i just close the connection.
cn.Close();

Edit 2: Answer

I found an easier workaround. instead of using the 3 lines of code to crate a Date with hDate, add a year and change it to a short date string, I used moment.js

var evDate = moment(hDate).add(1, 'year').format('MM/DD/YYYY');

This is returning the same value that I was working with before, but it allows the function to work fine in both, IE9 and IE11.

Thank you to all that looked at my question and those that tried to help me.


Solution

  • Not exactly an answer, more like a workaround.

    Since i am using the moment.js extension for my date pickers, I decided to try the following:

    var evDate = moment(hDate).add(1, 'year').format('MM/DD/YYYY');
    

    This is in the end providing the same value as my previous method, but for some random unique reason it works just fine in both IE9 and IE11. My guess is that the evDate.toLocaleDateString().toString() on IE11 is for some reason not playing nice with javascript and javascript is not taking it as a string. just my random guess.