Click to See Complete Forum and Search --> : Reading/Writing data in MS-Access database using javascript
christog
01-07-2003, 02:12 PM
I have a database with one table and I am trying to write data to it using only javascript. I have the connection string set up but do not know how to use javascript to access the DB and read/write to it. any help would be greatly appreciated. Thanks
You can't write into a database with javascript, cause javascript works on the client-side not on the server-side, for this you need a server side language, in your case probably one like ASP, JSP, Perl etc.
Quentin
06-30-2004, 12:32 PM
I know this post is old but I wanted to post this incase anyone else has this same question. Yes you can access a MS Access Database Client-Side with JavaScript here are some examples:
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();
}
I hope this helps anyone who was trying to do this.
faulkj
04-21-2009, 02:32 AM
You can indeed connect to a Microsoft Access database from JavaScript.
I had a situation where this functionality was actually useful, so I wrote a JavaScript library to simplify the process. It allows you to execute full SQL queries in a single command directly from JavaScript - with the option to select the desired format of the result-set with formats including JSON, XML, and HTML.
I had a great deal of difficulty finding information or examples of how to connect to do this, so I decided to share my code with the world...
The library is called ACCESSdb (http://www.accessdb.org/)and you can get it here (http://www.accessdb.org/)!
:D
Flippervan65
02-28-2012, 05:11 PM
Hi faulkj,
I accidently found your javascript library "accessdb" on the internet because i was looking for a way to get data out of a MSAccess-database and put it on a simple HTML-page.
Maybe I am overlooking something, but as the result of my query, I get "[object Object] " per element in the returned javascript array.
Here's my HTML/javascript I am trying to get to work:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>test javascript</title>
<script type="text/javascript" src="accessdb.js"></script>
</head>
<body>
<script type="text/javascript">
document.write('<p>Resultaat van de AccessDB-query:</p>');
var myDB = new ACCESSdb("D:\\Filip\\Documenten\\BNPPFortis\\CCB.mdb", {showErrors:true});
var SQL = "SELECT * FROM Agent";
var SQLresultSet = myDB.query(SQL);
if(myDB.query(SQL)) {
document.write(SQLresultSet[1]);
}
</script>
<p>Einde resultaat van de AccessDB-query</p>
</body>
</html>
Can you help me out ?
Anyway, I need a simple way to create a dynamic HTML-page, not using any server side scripting tool/language. And that's what your script/library is just doing !!!
ddepuemd
03-22-2012, 07:02 AM
I'm having the same issue and desperately need to be able to read an access database locally from a web page. HELP!!
Flippervan65
03-24-2012, 03:08 PM
This code does the trick:
<script type="text/javascript">
var pad = "D:\\Flipper\\Documenten\\data.mdb";
var cn = new ActiveXObject("ADODB.Connection");
var strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + pad;
cn.Open(strConn);
var rs = new ActiveXObject("ADODB.Recordset");
var SQL = "SELECT * FROM datatable";
rs.Open(SQL, cn);
if(!rs.bof) {
rs.MoveFirst();
if(!rs.eof) {
document.write("<p>" + rs.fields(1).value + ", ");
document.write(rs.fields(2).value + ", ");
document.write(rs.fields(3).value + ".</p>");
}
}
else {
document.write("No data found");
};
rs.Close();
cn.Close();
</script>
It only works with InternetExplorer or in Firefox with the IEtab set to Internet Explorer.
dennyclark
03-25-2012, 05:33 PM
Java refers to several computer software products and specifications from Sun Microsystems (which has since merged with Oracle Corporation), that together provide a system for developing application software and deploying it in a cross-platform environment. Java is used in a wide variety of computing platforms from embedded devices and mobile phones on the low end, to enterprise servers and supercomputers on the high end.
.
ddepuemd
03-26-2012, 06:49 AM
This does not work on my PC. Are there some settings I need to make in IE8 that will allow the object to be created and work? I keep getting an undefined error!
Flippervan65
03-26-2012, 04:13 PM
No, I did not make any changed to my IE-settings. Of course you have to allow javascript to run in your browser.