By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,101 Members | 1,463 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,101 IT Pros & Developers. It's quick & easy.

connect to an access database using javascript

P: 2
hi friends,

can any one out here help me by giving me the code to connect to an MSACCESS database using javascript.
Sep 6 '06 #1
Share this Question
Share on Google+
17 Replies


P: 27
Is there a way to do it?
Sep 7 '06 #2

iam_clint
Expert 100+
P: 1,208
it would be a task to do such a thing seeming how the database isn't going to be on every users computer, javascript is client side, you would need a serverside script to do this for you, ASP and PHP can both efficiently do this.
Sep 7 '06 #3

P: 2
hi friends,

can any one out here help me by giving me the code to connect to an MSACCESS database using ASP(javascript)
Sep 8 '06 #4

P: 1
I am also interesting in using JS to access an MS-Access file. I know this is CLIENT SIDE. The access file will be locally hosted so JS seems the way to go.

I am very new to JS, but looking use it to aid in IE pages that will be dynamically created from a DVD that is distributed (not from the web).

The only snip-it that I am starting to review is as follows:

<% @LANGUAGE="JScript" %>


var strConnect = "DSN=YourDSNHere";
oConn=Server.CreateObject("ADODB.Connection");
oConn.Open(strConnect);

var strSQL = "SELECT SomeField FROM myTable;";
var rsAd=oConn.Execute(strSQL);



Response.Write("First row is, "+ rsAd.Fields("SomeField") )

oConn.Close();
Sep 17 '06 #5

P: 1
hi friends,

can any one out here help me by giving me the code to connect to an MSACCESS database using javascript.
If you are using asp.net, there is no way to connect to database using standard javascript. You have to install server side javascript and you can do data connection with any data souce and delete,modify,view the records of the table.
That is the only way.Bye

Sanju John
Feb 15 '07 #6

dorinbogdan
Expert 100+
P: 839
You can connect to database from client-side script, if MDAC is installed on client's computer.
Just Update strConn with the correct Data Source, and SQL query as desired.
Expand|Select|Wrap|Line Numbers
  1. <html>
  2.     <head>
  3.         <title>Entitled Document</title>
  4.         <script language="JavaScript">
  5.         function getCount(){
  6.             var cn = new ActiveXObject("ADODB.Connection");
  7.             var strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = C:\clientDB.mdb;Persist Security Info=False";
  8.             cn.Open(strConn);
  9.             var rs = new ActiveXObject("ADODB.Recordset");
  10.             var SQL = "select count(*) from Customers";
  11.             rs.Open(SQL, cn);
  12.             alert(rs(0));
  13.             rs.Close();
  14.             cn.Close(); 
  15.  
  16.         }
  17.         </script>
  18.     </head>
  19.     <body>
  20.         <input type="button" value="Get count" onclick="getCount()">
  21.     </body>
  22. </html>
  23.  
  24.  
Did it help?
Feb 15 '07 #7

P: 4
Hi, thanks for posting this solution! I'm working on a local application which uses a local DB as well, so this is a great snip.

I do have a question about extending this. My DB is not Access and uses an ActiveX DLL for communication, but I've never worked with ActiveX in this manner. Would you happen to know how to modify this script to work with an ActiveX COM object?

I'm guessing it would be something like this to start, but obviously I'm clueless, so please correct as necessary.
6.
var cn = new ActiveXObject("ActiveXDB.com");

but then I'm clueless as to how to open a connection, send data to it or get the data back?

Sorry if this is a newbie question, but I've been searching for a day now and most answers I find are just "No, Javascript has no business talking to a database". If this method works out, it would be an awesome decrease in system requirements for my application, so I very much appreciate any help available.

Thanks!
D
May 22 '07 #8

dorinbogdan
Expert 100+
P: 839
Yes it should work.
I suppose that know the methods of your DLL, then just follow the previous ADODB sample and update it as needed.

Please note that only IExplorer allows ActiveX objects, and FireFox, Opera... do not allow.
May 23 '07 #9

P: 4
Blarg, getting "Automation server can't create object" when running with an ActiveX friendly platform (HTA for testing purposes). Can anyone see what I'm doing wrong here that causes the error?

SQLiteDb.DLL is registered on the machine, I even took the classID out of the registry to make sure it was right.Thanks for any help!

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <object>
  4. id = "SQLiteDb"
  5. CLASSID = "CLSID:1B562564-1BB6-4970-B0FB-31B24AA9C1D0"
  6. </object>
  7.  
  8. <script language="JavaScript">
  9.  
  10. var cn = new ActiveXObject("SQLiteDb.Connection");
  11. var strConn = "Database=db/test.db";
  12. </script>
  13.  
  14. </head>
  15. <body>
  16.  
  17. <script language="javascript">
  18. function getdata(){
  19. cn.Open(strConn);
  20. var rs = new ActiveXObject("SQLiteDb.Recordset");
  21. var SQL = "SELECT CoName from Companies WHERE CoID=1";
  22. rs.Open(SQL, cn);
  23. alert(rs(0));
  24. rs.Close();
  25. cn.Close();
  26. }
  27. </script>
  28. <input type="button" value="Get data"
  29. onclick="getdata()">
  30. </body>
May 30 '07 #10

dorinbogdan
Expert 100+
P: 839
Run this code and see if connection is created:
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3.  
  4. <script language="JavaScript">
  5.  
  6. var cn = new ActiveXObject("SQLiteDb.Connection");
  7. if (typeof(cn)=="undefined" || cn==null) 
  8.  alert("Unable to create the connection object!");
  9. else
  10.  alert("Connection object was successfully created!");
  11.  
  12. var strConn = "Database=db/test.db";
  13. function getdata(){
  14.  cn.Open(strConn);
  15.  var rs = new ActiveXObject("SQLiteDb.Recordset");
  16.  var SQL = "SELECT CoName from Companies WHERE CoID=1";
  17.  rs.Open(SQL, cn);
  18.  alert(rs(0));
  19.  rs.Close();
  20.  cn.Close();
  21. }
  22. </script>
  23.  
  24. </head>
  25. <body>
  26.  
  27. <input type="button" value="Get data"
  28. onclick="getdata()">
  29. </body>
  30. </html>
May 31 '07 #11

P: 4
Run this code and see if connection is created:
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3.  
  4. <script language="JavaScript">
  5.  
  6. var cn = new ActiveXObject("SQLiteDb.Connection");
  7. if (typeof(cn)=="undefined" || cn==null) 
  8.  alert("Unable to create the connection object!");
  9. else
  10.  alert("Connection object was successfully created!");
  11.  
  12. var strConn = "Database=db/test.db";
  13. function getdata(){
  14.  cn.Open(strConn);
  15.  var rs = new ActiveXObject("SQLiteDb.Recordset");
  16.  var SQL = "SELECT CoName from Companies WHERE CoID=1";
  17.  rs.Open(SQL, cn);
  18.  alert(rs(0));
  19.  rs.Close();
  20.  cn.Close();
  21. }
  22. </script>
  23.  
  24. </head>
  25. <body>
  26.  
  27. <input type="button" value="Get data"
  28. onclick="getdata()">
  29. </body>
  30. </html>
Hi, thanks for the help! And the survey says... Nope! Still has the "Automation Server" error. Interesting that I didn't need to put in the Object CLSID? I thought it was necessary for the registry to look up this object?

Anyways, this has got to be about creating and using the ActiveX object. I've posted on the manufacturer forums and gotten no helpful responses, but this code was in their help:
Expand|Select|Wrap|Line Numbers
  1. Dim cn As SQLiteDb.Connection
  2. Set cn = New SQLiteDb.Connection
  3.  
  4. cn.ConnectionString = "Data Source=C:\MyData\MyDatabase.db;"
  5. cn.Open
  6.  
So if this is supposed to work in VBscript, I can't see why the Javascript doesn't work, unless it has something to do with the "As SQLiteDb.Connection" line?

I have a different setup to the same DB with an ODBC driver and using an ADODB connection object, and it works fine. Unfortunately, that won't work for the distribution because it still requires installation of the driver and ADO to be installed on the client machine.

So, is there an ActiveX forum that I should move over to for this? Or are we ok to keep working here?
Thanks again for the help!
May 31 '07 #12

dorinbogdan
Expert 100+
P: 839
I guess that your ActiveX does not expose a COM interface for any application.
Perhaps it's a VB6 DLL that works in VB only (maybe .Net too).

At this point just the manufacturer should offer more support.

God bless you,
Dorin.
Jun 1 '07 #13

P: 4
Thanks for all of your help Dorin. At least I'm up and running with the ODBC method in Javascript, so I'm not at a total work stoppage :)

I agree about the manufacturer. They advertise that this is usable by any COM-aware language, including VBscript like the example I posted, but they surely ought to give full information on using it as a web object. I don't know how to make this work only because every other ActiveX object I've ever used has come with a code snippet from the manufacturer, and all I did was paste that in and get to work with it.

So, guess I'll keep looking for a solution instead of buying theirs. Too bad, theirs supported ADO-style requests, and that would let me use a lot of standard methods to get the job done quicker.

Thanks again for your help, you've been great!
Jun 1 '07 #14

P: 1
Dorin,

Thank you for your post; it helped me alot.

Bobby

You can connect to database from client-side script, if MDAC is installed on client's computer.
Just Update strConn with the correct Data Source, and SQL query as desired.
Expand|Select|Wrap|Line Numbers
  1. <html>
  2.     <head>
  3.         <title>Entitled Document</title>
  4.         <script language="JavaScript">
  5.         function getCount(){
  6.             var cn = new ActiveXObject("ADODB.Connection");
  7.             var strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = C:\clientDB.mdb;Persist Security Info=False";
  8.             cn.Open(strConn);
  9.             var rs = new ActiveXObject("ADODB.Recordset");
  10.             var SQL = "select count(*) from Customers";
  11.             rs.Open(SQL, cn);
  12.             alert(rs(0));
  13.             rs.Close();
  14.             cn.Close(); 
  15.  
  16.         }
  17.         </script>
  18.     </head>
  19.     <body>
  20.         <input type="button" value="Get count" onclick="getCount()">
  21.     </body>
  22. </html>
  23.  
  24.  
Did it help?
Aug 20 '07 #15

P: 2
Hi Dorin,

Thanks a lot for this post. It helped me in my application.

One thing I am still searching is that the data is retrieved if SQL is pointing to a table and not a query object which has some functions called.

Thanks anyways

regards
mustang123

You can connect to database from client-side script, if MDAC is installed on client's computer.
Just Update strConn with the correct Data Source, and SQL query as desired.
Expand|Select|Wrap|Line Numbers
  1. <html>
  2.     <head>
  3.         <title>Entitled Document</title>
  4.         <script language="JavaScript">
  5.         function getCount(){
  6.             var cn = new ActiveXObject("ADODB.Connection");
  7.             var strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = C:\clientDB.mdb;Persist Security Info=False";
  8.             cn.Open(strConn);
  9.             var rs = new ActiveXObject("ADODB.Recordset");
  10.             var SQL = "select count(*) from Customers";
  11.             rs.Open(SQL, cn);
  12.             alert(rs(0));
  13.             rs.Close();
  14.             cn.Close(); 
  15.  
  16.         }
  17.         </script>
  18.     </head>
  19.     <body>
  20.         <input type="button" value="Get count" onclick="getCount()">
  21.     </body>
  22. </html>
Did it help?
Nov 7 '07 #16

dorinbogdan
Expert 100+
P: 839
See this link.
Expand|Select|Wrap|Line Numbers
  1. ...
  2. rs.Open("yourTableName",cn, 1, 1, 2);
  3. ...
  4.  
Be blessed.
Nov 8 '07 #17

P: 1
I found a JavaScript library that allows you to execute SQL queries in a single command with the option of selecting the output format of the result-set from choices including JSON, XML, and HTML.

The library is called ACCESSdb and you can get it here: http://www.accessdb.org/
Apr 21 '09 #18

Post your reply

Sign in to post your reply or Sign up for a free account.