After reading this coding, you will know the following facts.
- How to connect the database in JavaScript
- How to use the ADODB.Recordset in JavaScript
As I already put the details comments in coding, I hope you will find it useful.
You can download the sourcecode and MSAccess database from this link.
http://michaelsync.net/demo/JavaScriptDatabaseSrc.zip
****
<!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" xml:lang="en" lang="en">
<head>
<script type="text/javascript">
<!–
var adOpenDynamic = 2;
var adLockOptimistic = 3;
/* Path of database.
If you put the database "Inventory.mdb" in different location,
you need to specify the correct path to this variable.
But When you are running the webpage from Browsers only,
you should use this keyword "window.location.pathname" for getting
the current location. But If you are using FrontPage,
you hav to specity the static path.
*/
var strDbPath = "C:\\JavaScript Database\\Inventory.mdb";
/*
Here is the ConnectionString for Microsoft Access.
If you wanna use SQL or other databases, you hav
to change the connection string..
eg: SQL => var conn_str = "Provider=sqloledb; Data Source=itdev;" +
"Initial Catalog=pubs; User ID=sa;Password=yourpassword";
*/
var conn_str = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + strDbPath;
function getAdoDb(strAdoType){
if (window.ActiveXObject){
return new ActiveXObject(strAdoType);
}
else{
return new ActiveXObject(strAdoType);
}
}
function showReports(){
try{
var strHtml ="";
strHtml += "<table cellpadding=0 cellspacing=0 border=1 width=500px align=center>";
strHtml += "<tr ><td align=center colspan=4><b>Stock List</b></td></tr>";
//Database Connection
var conn = getAdoDb("ADODB.Connection");
conn.open(conn_str, "", "");
//Recordset
var rs = getAdoDb("ADODB.Recordset");
strQuery = "SELECT StockID,StockName,ReOrderLevel,IsActive FROM Stocks";
rs.open(strQuery, conn, adOpenDynamic, adLockOptimistic);
if(!rs.bof){
rs.MoveFirst();
while(!rs.eof) {
strHtml += "<tr>";
strHtml += " <td width=\"10px\">" + rs.fields(0).value + "</td>";
strHtml += " <td width=\"50px\">" + rs.fields(1).value + "</td>";
strHtml += " <td width=\"10px\">" + rs.fields(2).value + "</td>";
strHtml += " <td width=\"5px\">" + rs.fields(3).value + "</td>";
strHtml += "</tr>";
rs.MoveNext();
}
}
else{
//No Records.
strHtml += "<tr colspan=4><td align=center><font color=red>No Records.</font></td></tr>";
}
conn.close();
strHtml += "</table>";
document.write(strHtml);
}catch(ex){
alert(ex.message);
}
}
//–>
</script>
<title>Stock List</title>
</head>
<!–<body onload="show_menu()">
<div id="main" />–>
<body>
<script language="JavaScript">
showReports();
</script>
</body>
</html>
i hav solved my prob now..
thanks for ur help!!
hi
i am getting an error while opening the internet explorer page as no values givan for required parameters.i have connected html and access using java script.where i am doing wrong i could not get it.
pls help me out
Hi Alice, Sorry for very very late response.. I was so busy these days and couldn’t have that much time to take a look it.. actually, using ActiveX is not good one..
Hi basanthi, using activex is not very good idea. So, Could you please do it with Ajax?
Hi Michael, thanks again for your kindness~
hai
I wrote the same code as above. Frontend is HTML, Backend is MS-access. we are having a group of linking forms,in first form data is storing in access and from second form data is not storing in access. Where am i going wrong Please help me.
You should probably tell me what error you got or etc.. With your question, I’m not sure how I can help you.. The most of problems are already discussed in old comments so if you read each and every comments in this post, you may probably solve the problem… one more thing… I suggest not to use ActiveX object as much as possible.. Aax can do better..
i wrote the code in the same format as above.
when i enter values in form it is storing in database.And after completion of that file entry it is closingi.e.., the browser is collapsed.
MY requirement is it should find the existing record and should enter the fields which are kept empty
hi i got this error “ActiveX object not defined”…….
How to rectify this error????????
Hello,
I want to create a very simple web application that will display the rows of a particular database table. However I also want the application to display new entries into the table, as they are inserted. Is there an easy way to determine when a new row has been inserted?
Thanks.
thx
To Michael Sync,
I have a program that I’ve created completely in javascript (html wrapped in an ebook exe program) that people love except for the fact that they need to complete all the tasks within my program to create their project; many of them have said they would like the ability to input some data and then return to the program and have the program automatically fill-in what data they inputted previously.
I know cookies will not work, because they will not hold enough data; is it possible to have some kind of javascript database within the program that the software could auto write to and auto retrieve from.
I’ve never worked with databases, but if this is something that can be done, I’m willing to pay for it.
Thanks
Hi,
My Problem is that I would like to display all the data which are corresponding to the radio button values, but its not getting, I’m able to access it through the id’s but not through the values.
Is there a way to populate this?
Hi Michael,
I know there is a long time to this artical , but realy I need your example
In my application, I’m using js to connect a database. I tested your first code , but when I debugged the source code. A message poped up and said : ActiveXObject is not defined, also for my bad luck the files you have attached to reply Peifang don’t work.
So please if you could send me this files.
Thank you
Rose
hi Michael,
You are really great.I was actually struggling for the last two weeks..in other forums ..i was told that we need to use server side scripting even when db is located locally..great effort..and effort appreciated.
i want to check onblur of a text box.when i click outside the text box.that time it will check in database filled value is existing in database or not…for that what can i do?
Hi Mike,
I was able to run this code locally on windows platform, but when I put it into the server, It didn’t work. I am getting the following message
“the connection cannot be used to perform this operation. It is either closed on invalid in this context”.
Does the connection string need to be specified differently ? I updated the path for the var strDbPath (used /folder/file.mdb), but kept everything else same for var conn_str.
var conn_str = “Provider=Microsoft.Jet.OLEDB.4.0;Data Source=” + strDbPath.
Would you know how to resolve this error. I can be reached at yiu51car@yahoo.com
Pingback: Schwarzes Brett im Intranet - Coders Talk @ tutorials.de: Forum, Tutorial, Anleitung, Schulung & Hilfe
Hi,
I found this code and it is working great, when the path to file is specified on an Harddisk. But how can I get this code working with a database placed on the server, e.g. http://myserver/mydatabase/database.mdb?
Anyone who got this working?
Any help would be appreciated.
Best Regards,
Klaus.
Hi,
I tried the code and it is working fine as long as the .mdb is specified with an absolute path. But when I try to use only the Database name, because it is located in the same folder as the .htm or when I try to use the Intranetlink it does not work. Does this not work at all or am I doing something wrong?
Best Regards,
Klaus.
Sorry, for posting again. I checked this site again and did not find my comment. So I thought maybe something went wrong.
I am really sorry.
Hi Michael Sync,
I saw your code and its working fine. Is it possible to fetch a stored image from ms-access using this ajax script. Does i have to include any addition for displaying image because i tried but i was unable to get it. Help me.
Regards
V.Sethu
i want more example for connect the database in the java script
how to load a video using javascript & how to get the x,y pion when mouse move
thanx , prasanna
i run this code but there’s an error occur when i open the page. the error is “ActiveXObject not defined”. what should be the problem of this?
can u fix it?
hi michael, i dont get output from the code above, can you make it step by step, i need it for my project. thank you for helping . more powers
hi michael
i dont understand, can you explain it more simple?
Hi Michael,
I tried ur code its really working fine .Justed wanted to say thanks for sharing ur knowledge with whole world.
U ROCK :)
Why are Microsoft programmers so dumb? This is such a trivial task, and yet many of you are struggling.
Thanks Michael, that worked for me ……..
Hi,
How can I use this code in my jsp+MySQL environment???
hi i just need to do a mini project using html javascript and also connect with database.
plz send me some source code with outputs screenshots so that it will help me
aha. Are you looking for some outsource work, lissy?