/    Sign up×
Community /Pin to ProfileBookmark

Reading/Writing data in MS-Access database using javascript

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

to post a comment
JavaScript

19 Comments(s)

Copy linkTweet thisAlerts:
@swonJan 07.2003 — 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.
Copy linkTweet thisAlerts:
@QuentinJun 30.2004 — 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
<i>
</i>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
<i>
</i>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
<i>
</i>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.
Copy linkTweet thisAlerts:
@faulkjApr 21.2009 — [SIZE="3"]You [I]can[/I] 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 [I]JSON[/I], [I]XML[/I], and [I]HTML[/I].



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 [B][U][I]ACCESSdb [/I][/U][/B]and you can get it here![/SIZE]


?
Copy linkTweet thisAlerts:
@Flippervan65Feb 28.2012 — 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:


[I][COLOR="Blue"]<!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:FilipDocumentenBNPPFortisCCB.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>

[/COLOR]
[/I]


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 !!!
Copy linkTweet thisAlerts:
@ddepuemdMar 22.2012 — I'm having the same issue and desperately need to be able to read an access database locally from a web page. HELP!!
Copy linkTweet thisAlerts:
@Flippervan65Mar 24.2012 — This code does the trick:

<script type="text/javascript">

var pad = "D:FlipperDocumentendata.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.
Copy linkTweet thisAlerts:
@dennyclarkMar 25.2012 — 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.

.
Copy linkTweet thisAlerts:
@ddepuemdMar 26.2012 — 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!
Copy linkTweet thisAlerts:
@Flippervan65Mar 26.2012 — No, I did not make any changed to my IE-settings. Of course you have to allow javascript to run in your browser.
Copy linkTweet thisAlerts:
@lakshmikNov 14.2012 — hi im a beginner in javascript

i have used the script given above in this thread. but im not able to connect to database. pls help me.. i m using acess 2007. pls find the below script

<HTML>

<head>

<script>

function AddRecord()

{

var adoConn = new ActiveXObject("ADODB.Connection");

var adoRS = new ActiveXObject("ADODB.Recordset");

adoConn.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source='C:Documents and SettingsuserDesktopv.mdb'");

adoRS.Open("Select * From u", adoConn, 1, 2);

document.write("c");

adoRS.Close();

adoConn.Close();

}

</script>

</head>

<body>

<input type="button" value="sub" onclick="AddRecord()">

</body>

</html>

thanks in advance
Copy linkTweet thisAlerts:
@laaviewApr 03.2013 — Hi Flippervan65,

thank you, you're code really did the trick.

I've been trying this for really long...

I've done little modification to connect access'2007 database and that also worked.


<html>
<head>
<title>Access db Connection</title>



<script type="text/javascript">

//var pad = "C:UsersAdminDocumentsTestDB.mdb";

[B]var pad = "C:UsersAdminDocumentsTestDB.accdb";[/B]

//var strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + pad;

[B]var strConn = "Provider=microsoft.ace.oledb.12.0;Data Source=" + pad;[/B]

var cn = new ActiveXObject("ADODB.Connection");

cn.Open(strConn);

var rs = new ActiveXObject("ADODB.Recordset");

var SQL = "SELECT * FROM customer_mas WHERE ID='512225'";

rs.Open(SQL, cn);

if(!rs.bof) {

rs.MoveFirst();

if(!rs.eof) {

document.write("<p><br>" + rs.fields(1).value + ", ");

document.write("<br>" + rs.fields(2).value + ", ");

document.write("<br>" + rs.fields(3).value + ".</p>");

}

}

else {

document.write("No data found");

};

rs.Close();

cn.Close();

</script>

</head>

</html>
Copy linkTweet thisAlerts:
@Rich071Jun 11.2013 — Curious, using accessdb.js, has anyone connected to an Access 2007/2010, accdb, using encryption and a mdw file? I can use the 2007/2010 DB without encryption, but I get the error below when accessing the DB with the encryption turned on and a mdw file.

Error!Error

[B]Cannot start your application. The workgroup information file is missing or opened exclusively by another user.[/B]

I know I am missing something, but not sure what. I have created the MDW file and joined it to the DB with no success.

Appreciate it, anyone has had success using a Access 2007/2010 with the encryption turned on.

Thanks,
Copy linkTweet thisAlerts:
@gary90Sep 10.2013 — hi ,,

thanks but i cant get all data rows from my db ,.,please help me thanks in advance
Copy linkTweet thisAlerts:
@MRALEXMay 24.2014 — Hello,

This is my code for insert data to acess database I can't get it to work Need some help. Thanks.

function insert(){

var today= new Date().toLocaleDateString();

var pad = "C:Usersaxmar18Documentsfeedback.accdb";

var cn = new ActiveXObject("ADODB.Connection");

var rs = new ActiveXObject("ADODB.Recordset");

var strConn = "Provider=microsoft.ace.oledb.12.0;Data Source=" + pad;

cn.Open(strConn);

rs.Open("SELECT * FROM feedback", cn);

rs.AddNew;

rs.Fields(field(1).value = "100";

rs.Update;

rs.Close();

cn.Close();

}
Copy linkTweet thisAlerts:
@MRALEXMay 24.2014 — hi ,,

thanks but i cant get all data rows from my db ,.,please help me thanks in advance[/QUOTE]


This work for me here:

function query(){

var pad = "C:UsersXXXXXXDocumentsDATABASENAME.accdb";

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");

var SQL = "SELECT * FROM tblName";

var i = 100;

rs.Open(SQL, cn);

if(!rs.bof) {

rs.MoveFirst();

while(!rs.eof) {

document.write();

document.write("<p>" + rs.fields(1).value + " ");

document.write(rs.fields(2).value + " ");

document.write(rs.fields(3).value + " ");

document.write(rs.fields(4).value + ".</p>");

rs.MoveNext();

}

}

else {

document.write("No data found");

};

rs.Close();

cn.Close();

}
Copy linkTweet thisAlerts:
@bobbypittay9385Mar 13.2015 — Echinacea Until Pregnant Saw Palmetto Does Information Technology Work Terramycin Good Dogs [url=https://archive.org/details/Antibiotics_236 ]https://archive.org/details/Antibiotics_236[/url]. Men Weight Loss Movies Diabetes By Population Too Fat For Metformin Risperdal For Bipolar M Tab . Levi Oxycontin Controlled Substance Gemfibrozil 600 Cholesterol And Triglycerides . Most Time While Taking Clomiphene Citrate Timeline Of Day Clomid [url=http://ampicillinpills.net/ ]buy betta ampicillin[/url] Xanax With Pregnancy Emedtv Buy Levitra In Costa Rica Xanax Online Overnight Shipping Burnaby Bupropion Hcl Prescription
Copy linkTweet thisAlerts:
@antsureJan 21.2016 — This code does the trick:

<script type="text/javascript">

var pad = "D:FlipperDocumentendata.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.[/QUOTE]


How can we get spesific field value via textbox for this one?
Copy linkTweet thisAlerts:
@rgreenberMay 30.2016 — Wow!!

I have been looking for code like this literally for *many* *months*(!!!)

Now to get it to work with a database sitting on the server. How can I accomplish that?

Thanks.
×

Success!

Help @christog spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.24,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...