Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read and write to an access database using Javascript

first I want to mention that I am aware that in web interaction with databases should
always be with server side languages due to security reasons and for the fact that javascript
as is doesn't offer and compatibility with the windows file system.

that said I am facing a though situation and I am trying to think creatively.
I am not allowed any access to a server sided scripting and SQL.

and I need to create a client based application for an intranet that will be able to store data as time progress.

I have found 2 solutions so far but none of them has enough documentation for me to use correctly.

one is a javascript library called ACCESSdb which can be found here:ACCESSdb
unfortunately I couldn't understand how to use it to write or read data from the DB...

and the other is those 3 pieces of code:

Adding a Record:

function AddRecord() {
var adoConn = new ActiveXObject("ADODB.Connection");
var adoRS = new ActiveXObject("ADODB.Recordset");

adoConn.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source='/\dbName.mdb'");
adoRS.Open("Select * From tblName", adoConn, 1, 3);

adoRS.AddNew;
adoRS.Fields("FieldName").value = "Quentin";
adoRS.Update;

adoRS.Close();
adoConn.Close();
}  

Removing a Record:

function DeleteRecord() {
var adoConn = new ActiveXObject("ADODB.Connection");
var adoRS = new ActiveXObject("ADODB.Recordset");

adoConn.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source='\\dbName.mdb'");
adoRS.Open("Select * From tblName Where FieldName = 'Quentin'", adoConn, 1, 3);
adoRS.Delete;
adoRS.Delete;

adoRS.Close();
adoConn.Close();
}  

Editing a Record:

function EditRecord() {
var adoConn = new ActiveXObject("ADODB.Connection");
var adoRS = new ActiveXObject("ADODB.Recordset");

adoConn.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source='\\dbName.mdb'");
adoRS.Open("Select * From tblName Where FieldName = 'Quentin'", adoConn, 1, 3);

adoRS.Edit;
adoRS.Fields("FieldName").value = "New Name";
adoRS.Update;

adoRS.Close();
adoConn.Close();
}  

out of them only the add new record one worked for me for some reason...
also I found that to read the value of any cell in the first row all I had to do was to write:

alert(adoRS(cellNum));  

but how do I get the value of cells in the later rows? lets say (row 3,cell 5).

Thank you for reading this far! I'll appreciate your help a lot!

Jake

like image 717
Jake Avatar asked Mar 19 '12 20:03

Jake


1 Answers

First, make sure that '/\' and '\' (in connection string) is just a typo in SO.

Second, here is a version of Delete command:

function DeleteRecord() {
var adoConn = new ActiveXObject("ADODB.Connection");
var adoCmd = new ActiveXObject("ADODB.Command");

adoConn.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source='\\dbName.mdb'");
adoCmd.ActiveConnection = adoConn;
adoCmd.CommandText = "Delete * From tblName Where FieldName = 'Quentin'";
adoCmd.Execute();

adoConn.Close();
}

And, Edit command (without looping -> updates all [matching] records):

function EditRecord() {
var adoConn = new ActiveXObject("ADODB.Connection");
var adoCmd = new ActiveXObject("ADODB.Command");

adoConn.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source='\\dbName.mdb'");
adoCmd.ActiveConnection = adoConn;
adoCmd.CommandText = "Update tblName Set FieldName = 'New Value' Where FieldName = 'Quentin'";
adoCmd.Execute();

adoConn.Close();
}  

Please note, I have not tested this (do not have Access right now), so there might be some syntax bugs...

Hope it works and helps.

like image 93
Igor Turman Avatar answered Sep 23 '22 01:09

Igor Turman