Connect to MS Access database using javascript

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • srivijayaragavan
    New Member
    • Jun 2007
    • 2

    Connect to MS Access database using javascript

    Can any one help me how to connect MS Access from Javascript with example?
    Is any component required or that is to be installed in client side?
    I have read that some component needs to be installed.
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by srivijayaragava n
    can any one help me how to connect msaccess from javascript with example?
    is any component required or that is to be installed in client side?
    in same forum i have seen that some component needs to be installed. so how to get that plz..give me
    Java != Javascript.
    Moved to Javascript forum.

    To connect to a database, you need a server side script. Javascript is client side.

    Comment

    • dmjpro
      Top Contributor
      • Jan 2007
      • 2476

      #3
      do u know a new technology ..... AJAX.
      try to know it ... this ll help u to do this.
      best of luck.

      kind regards.
      dmjpro.

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        Changed thread title.

        Comment

        • acoder
          Recognized Expert MVP
          • Nov 2006
          • 16032

          #5
          Javascript cannot connect to a database, but you can make a request to a server-side page which does so using javascript (known as AJAX). Read up on Ajax in the Offsite Links thread at the top of this forum.

          Comment

          • OutSider1234
            New Member
            • Jun 2007
            • 1

            #6
            I think I've seen this done before somewhere, but of course I can't remember where exactly.

            It is possible to connect to a client side database (Access) via Javascript.
            Below is a sample connecting to a database on C.

            It is running a query on a table called SampleTable, and returning a recordset of 4 fields.
            The sysntax may be a little odd, it very like classic ASP in how it pages through the recordset.

            Hope this helps

            Code:
            <html>
              <head>
            
              <script type="text/javascript">
              <!--
              var adOpenDynamic = 2;
              var adLockOptimistic = 3;
            
              /* Path of database.
              */
              var strDbPath = "C:\\FolderName\\Sample.mdb";
            
              /*
                Here is the ConnectionString for Microsoft Access.
                If you want to 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 ActiveXObject(strAdoType);
            	}
              }
            
              function showReports(){
                try{
            		var strHtml ="";
            		strHtml += "<table cellpadding=0 cellspacing=0 border=1 width= '100%' align=center>";
            		strHtml += "<tr ><td align=center colspan=4><b>Sample Database Records</b></td></tr>";
            
            		    //Database Connection
                   var conn = getAdoDb("ADODB.Connection");
                   conn.open(conn_str, "",   "");
            
                   //Recordset
                   var rs = new ActiveXObject("ADODB.Recordset");
                   //strQuery = "SELECT * FROM SampleTable";
                   strQuery = "SELECT SampleTable.Date, SampleTable.Name, SampleTable.Group, SampleTable.Details FROM SampleTable";
                   rs.open(strQuery, conn, adOpenDynamic, adLockOptimistic);
            
                   if(!rs.bof){
                      rs.MoveFirst();
                      while(!rs.eof) {
                         strHtml += "<tr>";
                         strHtml += " <td><Font face ='tahoma'>" + rs.fields(0).value + "</font></td>";
                         strHtml += " <td><Font face ='tahoma'>" + rs.fields(1).value + "</font></td>";
                         strHtml += " <td><Font face ='tahoma'>" + rs.fields(2).value + "</font></td>";
                         strHtml += " <td><Font face ='tahoma'>" + rs.fields(3).value + "</font></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>Call Log Details</title>
              </head>
            
              <!--<body onload="show_menu()">
                <div id="main" />-->
              <body>
                <script language="JavaScript">
                  showReports();
                </script>
              </body>
              </html>

            Comment

            • acoder
              Recognized Expert MVP
              • Nov 2006
              • 16032

              #7
              Originally posted by OutSider1234
              It is possible to connect to a client side database (Access) via Javascript.
              That is not Javascript, it is JScript (Microsoft's version). It would only work in IE with security restrictions relaxed.

              Comment

              • Sunlis
                New Member
                • Jun 2007
                • 2

                #8
                I realize that this thread has been quiet for a while now, but this is the closest thing I can find to a solution to my problem.

                In a project of mine, users need to be able to register, and I need it to be done using client-side script, I need it to write to a MS Access database.
                There are 6 fields, firstname, lastname, email, countryorregion ,stateorprovinc e, and password.
                They also need to be able to log-in, using their first & last names, and a password, all of which they provide upon registration.

                I've been trying to get this to work for days now, but I know almost no javascript or php.
                Any help would be GREATLY appreciated.

                -Sunlis

                Comment

                • acoder
                  Recognized Expert MVP
                  • Nov 2006
                  • 16032

                  #9
                  Login should always be done server-side. In any case, you can't connect to a database using Javascript, but you can make a http request to a server-side script (e.g. in PHP) which will make the necessary database transactions. This would be done using something called Ajax.

                  Comment

                  • Boodaleh
                    New Member
                    • Mar 2010
                    • 2

                    #10
                    Originally posted by acoder
                    Javascript cannot connect to a database, but you can make a request to a server-side page which does so using javascript (known as AJAX). Read up on Ajax in the Offsite Links thread at the top of this forum.
                    Not exactly, this is server-side code. You can also accomplish this as you would in VBscript

                    Comment

                    • Boodaleh
                      New Member
                      • Mar 2010
                      • 2

                      #11
                      Originally posted by acoder
                      That is not Javascript, it is JScript (Microsoft's version). It would only work in IE with security restrictions relaxed.
                      Not quite accurate, this is server-side code, so it's not intended to be intepreted by the browser. So it will work regardless of the client's browser.

                      Comment

                      • acoder
                        Recognized Expert MVP
                        • Nov 2006
                        • 16032

                        #12
                        Heh, try that on a non-IE browser. It looks like ASP, but it isn't. This is client-side code connecting to an Access database on the client.

                        Comment

                        Working...