VBScript / Javascript accessing local Microsoft Access Database
-
Saturday, November 17, 2012 8:54 AM
Hello Experts,
- I plan to make a simple web page with Javascript / VBscript embedded in it.
- Script controls what text/data is going to be displayed in text-area (no other HTML element is present) from MS-Access table. (on LOCAL MACHINE only)
- Tried a bit, but getting confused with various methods of connection (DAO, ADO, Access Object etc).
- There seems vast difference in opening a connection to a database and opening a database itself.
- With VBA it was fine, but with VBScript / Javascript I have found no success.
- Could any one share some code about :
- Passing information to and fro from Access to HTML (text area), and calling Access object like Macros, Forms, SP etc.
VBA Code : (Used for opening same database in which this module was written)
- Dim objConn As New ADODB.Connection
Dim objRec As New ADODB.Recordset
Set objConn = New ADODB.Connection
Set objConn = CurrentProject.Connection
Set objRec = New ADODB.Recordset
strSql = "tblMain_Bank"
objRec.Open strSql, objConn, adOpenDynamic, adLockOptimistic, adCmdTable
- How do I put desired database address (Access file path) in place of CurrentProject.Connection
With regards : Ajay Check
All Replies
-
Saturday, November 17, 2012 1:56 PMModerator
Hi,
Please read the following:
Bill
-
Saturday, November 17, 2012 2:46 PM
Hi, AbqBill
- My problem revolves around Ms-Access (database) and Vb-Script / Java-Script (scripting).
- That's the reason I have posted same question, in Scripting Guys and Ms Office Access forum only.
- No matter, wherever, I get answer, I will mark them both as solved with appropriate note.
- So, apart from posting question, simultaneously in two forums, what I have missed with "way of asking questions in forum".
With regards : Ajay Check
-
Saturday, November 17, 2012 2:54 PMModerator
Hi,
You cannot access local files on the local file system for security reasons. To work around this, you can write your application as an HTA instead.
Bill
- Marked As Answer by Ajay Check Saturday, November 17, 2012 3:55 PM
-
Saturday, November 17, 2012 3:10 PM
- How do I put desired database address (Access file path) in place of CurrentProject.Connection
With regards : Ajay Check
Const adLockReadOnly = 1
Const adOpenForwardOnly = 0
Dim strConnect As String, strSQL As StringstrConnect = "Provider=Microsoft.Jet.OLEDB.4.0;Data " _
& "Source=C:\Program Files\Microsoft " _
& "Office\Office12\SAMPLES\Northwind.mdb;" _
& "Persist Security Info=False"
strSQL = "SELECT Customers.ContactName " _
& "FROM Customers " _
& "WHERE (((Customers.CustomerID)='" & Sheets(1).Range("b1") & "')); "Set objConnection = CreateObject("ADODB.Connection")
Set objRecordset = CreateObject("ADODB.Recordset")objConnection.Open strConnect
objRecordset.Open strSQL, objConnection, _
adOpenForwardOnly, adLockReadOnlydo some stuff
objRecordset.Close
objConnection.CloseRegards, Dave Patrick ....
Microsoft Certified Professional
Microsoft MVP [Windows]
Disclaimer: This posting is provided "AS IS" with no warranties or guarantees , and confers no rights. -
Saturday, November 17, 2012 3:52 PM
You cannot open a database from clientside code.
You cannot open a database from serverside code without making special arrangements for proper authentication.
This question belongs in the IIS developers forum and not in this admin scripting forum.
¯\_(ツ)_/¯
-
Saturday, November 17, 2012 4:06 PM
Hello Experts,
- AbqBill : As pointed out by, him, the problem was with security feature. Things worked when I turned it into .HTA application. Say thanks AbqBill, had he not pointed out security issue, I would have been throwing stones in all wrong direction.
- Things to remember / I learned : HTA and HTML works in different way, when it comes to security.
- While opening a connection to database or database itself, pay special attention to your Ms-Access version and provider version associated with it.
- Refer : http://msdn.microsoft.com/en-us/library/ms524771(v=vs.90).aspx.
- For reference purpose, who are going to stumble upon same question, which I found a bit tricky, below is complete working code of HTA:
<html> <head> <title>Demo project</title> <HTA:APPLICATION APPLICATIONNAME="HTA Test" SCROLL="yes" SINGLEINSTANCE="yes" > <!-- Script starts here --> <script language="VBScript"> Dim objText Sub subStart ' Opening a connection to database strConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=D:\ZenCode.accdb" Set cnn = CreateObject("ADODB.Connection") cnn.Open strConnectionString ' Open a recordset using the Open method and use the connection established by the Connection object. Set rstCustomers = CreateObject("ADODB.Recordset") strSQL = "SELECT SubGroup FROM tblCode" rstCustomers.Open strSQL, cnn ' Putting value from database to hta rstCustomers.MoveFirst Set objText = document.getElementById("area") objText.value = rstCustomers("SubGroup") ' Looping through database and printing data values Do Until rstCustomers.EOF Msgbox rstCustomers("SubGroup") rstCustomers.MoveNext Loop End Sub </script> <head> <!--Body starts here--> <body onload="subStart()"> <center>You have to insert your access file path and table fields name to make it work with your code</center> <hr> <center><textarea id="area" rows="25" cols="50"></textarea></center> </body> </html>
With regards : Ajay Check
- Edited by Ajay Check Saturday, November 17, 2012 4:12 PM Adding more info

