| Programming | Software Engineering | Web Design | Database | Operating Systems

How to Pass Access Data Across the Web

Danny Lesandrini
Keywords: Access,Copy Data
From: http://www.databasejournal.com/features/msaccess/article.php/3491216

The solution described below should probably not be considered a "best practice" but it works.  It came to life as I was pondering the following conundrum:

How can I pass a little bit of data from an internal Access application behind a firewall out to a public web site in real time?

In this case, the internal application maintained product orders.  As an order matures, it passes through various stages:  quote to confirmed to production to invoiced to ... you get the idea.  Some customers had many orders and they would call several times a day to check on them.  "Wouldn't it be great," my manager said, "if our customers could get their order status information from our web page."  The problem was that we did not host our own web site internally, so there was no way to "link" the web to our internal order system, written in Microsoft Access.

Well, I thought about it for a while and decided to create a small, mirrored version of the database out at the web site.  All that would be needed were the customer, order and status table, and not even all the fields for each.  Customers were interested only in seeing the status of their current orders, so the output table would consist of only a half dozen fields.  That should be easy to pass in an ASP QueryString, right?

Right!  It is easy to pass and it works like a charm, but how does one navigate to a web page from VBA code in an Access database?  Ahhhh ... that's the real trick.  Read on.

The ASP QueryString

The QueryString object is a very simple mechanism for passing data to an ASP web page.  You simply append parameters and their values after the page address followed by a question mark (?), delimited by the ampersand character(&), like this:

    process.asp?CustomerID=1234&CustomerName=ABC Limited&OrderDate=6/15/2005

The page,  process.asp in this case,  will parse everything that follows the question mark and split it into the following:

Customer ID ... with a value of 1234
CustomerName ... with a value of ABC Limited
OrderDate ... with a value of 6/15/2005

The example in the screen shot below shows the URL that was used to generate the output page.  We see that the ID in the URL is 35 and the page is reflecting that value, along with all the other parameters that appear in the QueryString.  The ASP code to create this page, which is neither trivial nor difficult, is displayed below.

What's basically happening in the code below is the following:

  1. Parameters from the QueryString are loaded into local variables.
  2. Database is searched for an existing record
    1. If not found, a record is inserted
    2. If found, the existing record is updated
  3. For confirmation, the record is accessed again and output to the screen in a table.
<%@ Language=VBScript %>
<% Option Explicit    %>
<%
Dim sConn, sPath, objCnn, sSQL, objRst, sOut
Dim sID, sCID, sSID, sSDate, sPoNum, sCust, sStatus

' Get path to database and create connect string
sPath =  Replace(LCase(Server.MapPath("process.asp")),"process.asp","remote.mdb")
sConn = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source=" & sPath

' Collect new values from querystring that will be used for INSERT or UPDATE
sID     = Request.QueryString("ID")
sCID    = Request.QueryString("CID")
sSID    = Request.QueryString("SID")
sSDate  = Request.QueryString("SDate")
sPoNum  = Request.QueryString("PoNum")

Response.Write("<h3><font color=black>Welcome To The Order Prodess Page</font></h3>")

' Open database connection
Set objCnn = server.createobject("adodb.connection")
Set objRst = server.CreateObject("adodb.recordset")
objCnn.open(sConn)

' Only perform INSERT/UPDATE if OrderID exists   
If CLng(sID) > 0  Then
   ' Load the record:
   sSQL = " SELECT COUNT(*) As OrderExists FROM qryOrderInfo WHERE OrderID=" & sID
   Set objRst = objCnn.Execute(sSQL)
   
   If objRst.Fields("OrderExists") = 0 Then
   ' No record Exists.  Insert new one.
      sSQL = "INSERT INTO tblOrder ([OrderID], [CustomerID], [StatusID], [StatusDate], [PONumber])" & _
             " VALUES(" & sID & ",'" & sCID & "'," & sSID & ",#" & sSDate & "#,'" & sPoNum & "')"
      objCnn.Execute sSQL
      Response.Write("<h3><font color=darkgreen>Inserted new order ... ID=" & sID & "</font></h3>")
   Else
      ' Record exists, update
      sSQL =  "UPDATE tblOrder SET [CustomerID]=" & sCID & ", [StatusID]=" & sSID & _
              ", [StatusDate]=#" & sSDate & "#, [PONumber]='" & sPoNum & "' " & _
              "WHERE [OrderID]=" & sID
      objCnn.Execute sSQL
      Response.Write("<h3><font color=darkred>Updated existing order ... ID=" & sID & "</font></h3>")
   End If
Else
   Response.Write("<h3><font color=navy>Nothing to do ... no order ID passed.</font></h3>")
End If

' Now, go look up the updated record and output the field values
sSQL = " SELECT * FROM qryOrderInfo WHERE OrderID=" & sID
Set objRst = objCnn.Execute(sSQL)

sOut = "<table border=1 cellspacing=0 cellpadding=0 ID="Table1">"
sOut = sOut & "<tr><td colspan=4 align=center><h3>Edited Order</h3></td><tr>"
sOut = sOut & "<tr><th>Customer</th><th>Status</th><th>Status Date</th><th>PO Number</th><tr>"

If Not objRst.BOF and Not objRst.EOF Then
   sCust   = objRst.Fields("Customer")
   sStatus = objRst.Fields("Status") 
   sSDate  = objRst.Fields("StatusDate") 
   sPoNum  = objRst.Fields("PONumber") 

   sOut = sOut & "<tr><td>" & sCust & "</td><td>" & sStatus & "</td>"
   sOut = sOut & "<td>" & sSDate & "</td><td>" & sPoNum & "</td><tr>"
End If 
sOut = sOut & "</table>"
Response.Write sOut

Set objRst = Nothing
Set objCnn = Nothing
%>


Well, that's it for the receiving part.  Now on to the trick ... how to send the data from VBA in Microsoft Access.

Send Data to URL

The majority of the VBA code performs the string concatenation, forming the actual URL that will be hit.  The code is shown below and should be pretty straightforward to understand.  Start out with a constant that identifies the base URL for the page and add parameters and their values.  Once the URL is complete, it takes only one line of code to launch it, the one highlighted in yellow below.  However, there are a couple of tricks to make this work.

Public Function 
  UpdateOrderInfoWebSite() As Long
On Error Resume Next  
   

   Dim objDoc As SHDocVw.InternetExplorer
   Dim sURL As String
   Dim lOrderID As Long
   
   lOrderID = Forms!frmOrder!txtOrderID

   Const cURL As String = 
     "http://localhost/Orders/Process.asp"
   sURL = cURL & "?ID=" & lOrderID
   sURL = sURL & "&SDate = 
     " & Forms!frmOrder!txtStatusDate
   sURL = sURL & "&SID   =   
     "  & Forms!frmOrder!cboStatusID 
   sURL = sURL & "&PONum = 
     "  & Forms!frmOrder!txtPONumber 
   sURL = sURL & "&CID   = 
     " & Forms!frmOrder!cboCustomerID
   
   Set  objDoc = New SHDocVw.InternetExplorer
   objDoc.Navigate sURL, , , True

   ' Need something to pause execution 
     while URL is hit.
   Dim i As Integer, j As Integer
   For i = 0 To 1000
      j = DCount("*", "MsysObjects")
   Next

   UpdateOrderInfoWebSite = Err.Number
    
End Function


Notice above that objDoc was declared as type   SHDocVw.InternetExplorer?  This declaration will fail to compile unless you set a reference to the Microsoft Internet Controls.  Open any module and select References from the Tools menu.  You will see the screen below.  Scroll down the list until you find the Internet Controls reference and select it.  Click OK to save your selection.
 

The other trick is not so clear, and quite frankly, I do not have a great solution for it, or even a great explanation for why it exists.  It seems that the process needs to pause for a few seconds to let the Navigate to URL command work before the object goes out of scope.  There are some great API calls that will pause execution, but in this case I just elected to execute the DCount() function a thousand times.  That is enough of a pause to allow the process to complete and it does not require any complex explanations of API declares.

You should be able to copy and paste the above code into any VBA module and run it, so long as you set the reference mentioned above.  All the code is available in this month's download, but if you try to test it, you will need to set up the remote.mdb and asp pages on a web server.  The Local.mdb has a form for testing the process and can be run from any computer with access to your web page.  In this case, I have set the AfterUpdate Event property for all the controls to = UpdateOrderInfoWebSite() which calls the above code.  Each time something is updated, our web database will, in short order, become synchronized with it.

What Could Go Wrong? 

As I mentioned, this works, but it is not necessarily a great idea.  Just imagine the local network traffic it would generate if you had 100 employees editing records all day long.  We ran into these kinds of problems at first, but decided that, with a little better planning, we could reduce the number of calls to the UpdateOrderInfoWebSite() function.  We continued to use the method, but reduced the number of times it was called by sending an update only after a record was saved as a whole, not each time someone changed a single control, as is the case with this demo application.

There are other issues to consider as well.  What happens when you add new Customers?  New Statuses?  The same method can be replicated to update all the tables in this remote database, but again, all this adds overhead.  As you can see, the process could mushroom to the point where you are replicating the entire database through QueryString variables to an offsite database.  Yes, this solution works, but is not very scalable.

On the other hand, it is a pretty nifty trick for dumping small pieces of data through your firewall to a web site.  As usual, if anyone has a better solution, I would love to hear it.


Related Article
  • "Access Hacks": Wrangle Data Like Never Before
  • Execute SQL Stored Procedures from Microsoft Access
  • ODBC DSN-Less Connection Tutorial
  • ODBC DSN-Less Connection Tutorial
  • Convert Access Tables Into ASP Web Pages

  • Comment
    Britney Post At: 2007-1-6 12:44:54
    Hello, nice site look this:
    http://fastppc.info http://fastppc.info http://fastppc.info http://fastppc.info http://fastppc.info http://fastppc.info http://fastppc.info
    End ^) See you
    PB5CX8vZQ5 Post At: 2007-1-13 17:02:24
    Hi! Very nice site! Thanks you very much! r3O2fB4JYm2x
    X16FWWGOVD Post At: 2007-1-18 16:45:43
    http://www.seun.ru/criea/purchase-viagra.html purchase viagra http://www.seun.ru/criea/cialis-online.html cialis online http://www.seun.ru/criea/viagra-purchase.html viagra purchase http://www.seun.ru/criea/cialis-price.html cialis price http://www.seun.ru/criea/cialis-cheap.html cialis cheap http://www.seun.ru/criea/buy-tramadol.html buy tramadol
    c5ITp4c8aE Post At: 2007-1-19 8:26:32
    [url=http://scandius.com/deegi/viagra.html]viagra[/url] [url=http://scandius.com/deegi/generic-cialis.html]generic cialis[/url] [url=http://scandius.com/deegi/viagra-buy.html]viagra buy[/url] [url=http://scandius.com/deegi/cialis-canada.html]cialis canada[/url] [url=http://scandius.com/deegi/canada-viagra.html]canada viagra[/url] [url=http://scandius.com/deegi/cialis-cheap.html]cialis cheap[/url]
    OUvOKJdNgr Post At: 2007-1-23 15:38:51
    [url=http://www.avl.lu/kpwrv/generic-cialis.html]generic cialis[/url] [url=http://www.avl.lu/kpwrv/cialis.html]cialis[/url] [url=http://www.avl.lu/kpwrv/cialis-online.html]cialis online[/url] [url=http://www.avl.lu/kpwrv/buy-cialis.html]buy cialis[/url] [url=http://www.avl.lu/kpwrv/cheap-cialis.html]cheap cialis[/url] [url=http://www.avl.lu/kpwrv/buy-cialis-online.html]buy cialis online[/url]
    mxifkxusny Post At: 2007-6-23 12:35:17
    Hello! Good Site! Thanks you! mybwypxmjzj
    qofaeesjpj Post At: 2007-6-27 18:48:35
    Thanks for this site! [url=http://qea.com/resources/phentermine_online.html]phentermine online[/url]
    Tramadol use Post At: 2007-7-6 10:48:22
    FedEx overnight shipping, free prescription with your order and 24/7 customer service, http://BuyTramadolOnline.ws
    babah2 Post At: 2008-1-17 19:29:25
    Your site is great! Very useful. Good resources here. Thanks much!
    http://geo.ya.com/mqk3/spyware-doctor-register.html
    http://geo.ya.com/mqk3/doctor-spyware.html
    http://geo.ya.com/mqk3/index.html
    http://geo.ya.com/mqk3/spyware-doctor-series.html
    http://geo.ya.com/mqk3/pc-tools-spyware-doctorpc.html
    m3svcs Post At: 2008-1-18 16:40:09
    Excellent website. This will be my first time visiting
    [url=http://geo.ya.com/spytqe/free-adware-spyware-removal-software.html]free adware spyware removal software[/url]
    [url=http://geo.ya.com/spytqe/free-spyware-adware-removal-tool.html]free spyware adware removal tool[/url]
    [url=http://geo.ya.com/spytqe/free-scan-spyware-adware-removal.html]free scan spyware adware removal[/url]
    [url=http://geo.ya.com/spytqe/anti-free-removal-spyware-virus.html]anti free removal spyware virus[/url]
    [url=http://geo.ya.com/spytqe/free-spyware-removal-downloads.html]free spyware removal downloads[/url]
    b3myfr3n Post At: 2008-1-18 21:06:58
    Wow! Well done, keep up the great work. I found lots of intresting things here. Thanks!
    [link http://geo.ya.com/spytqe/free-anti-spyware-removal.html]free anti spyware removal[/link]
    [link http://geo.ya.com/spytqe/free-spyware-removal-utility.html]free spyware removal utility[/link]
    [link http://geo.ya.com/spytqe/adware-download-free-removal-spyware.html]adware download free removal spyware[/link]
    [link http://geo.ya.com/spytqe/free-full-spyware-removal-software.html]free full spyware removal software[/link]
    [link http://geo.ya.com/spytqe/win-95-spyware-adware-removal-free-download.html]win 95 spyware adware removal free download[/link]
    accgio Post At: 2008-1-19 1:48:02
    Looks good! Many thanks, Thanks much!
    2r2xju2viw Post At: 2008-8-17 0:29:33
    c35ji3le36e [URL=http://www.531809.com/1057504.html] imac9crx [/URL] 89oa2tryzk4tu
    Madelyn Contreras Post At: 2009-1-9 12:30:30
    hi
    fa2fcuwexdsduchr
    good luck
    Reynaldo Dawson Post At: 2009-1-11 1:27:49
    hi
    fa2fcuwexdsduchr
    good luck
    ydqhlb6ub8 Post At: 2009-2-1 7:51:36
    Hi! Ebanij vrot! 83gcut35rt ww148svaaw!
    bgcfli1xxn Post At: 2009-2-1 7:51:53
    Hi! Ebanij vrot! 843sl6pv3c 5jnxgi86ii! http://my.opera.com/arnoldoalbrecht/blog/2009/01/30/group-airfares?viewmobile
    w4uta4gwk6 Post At: 2009-2-1 7:52:41
    Hi! Ebanij vrot! z2bmdj5b4d snmyg7d1jr! [url=http://my.opera.com/tameraaguilera/blog/2009/01/30/airfare-saintjohn-to-halifax?viewmobile]Airfare Saintjohn To Halifax[/url] ; Bye! =)
    pEbmJotr Post At: 2010-3-22 22:46:32
    ZUBUTG
    Add Your Comment:
    Your Name:      
    Your Comment:
    Note: After you post comment,please refresh the browser to show you comment.
    Search In YeYan.CN:
     

    Home | Privacy Policy | Copyright Policy | Contact Us | Site Map
    Copyright © 2006 YeYan.CN, All Rights Reserved.