Home
DBPix
Download
Order
Testimonials
Support
Tutorials
Samples
KnowledgeBase
Links
Revision History
Documentation
Search
Contact
Site Map

Graph: DBPix image storage vs OLE Embedding and Linking in Microsoft Access


DBPix Sample Source Code: item_edit.asp
Back to sample
<HTML>
<HEAD>
<META name=VI60_defaultClientScript content=VBScript>
<TITLE>XMLHTTP Editing Sample</TITLE>
<SCRIPT ID=clientEventHandlersVBS LANGUAGE=vbscript>

'---- MarshalOptionsEnum Values ----
Const adMarshalAll = 0
Const adMarshalModifiedOnly = 1

'---- ObjectStateEnum Values ----
Const adStateClosed = &H00000000
Const adStateOpen = &H00000001
Const adStateConnecting = &H00000002
Const adStateExecuting = &H00000004
Const adStateFetching = &H00000008'

Dim rs 
Dim cn

Sub window_onload
  Set rs = CreateObject ("ADODB.Recordset")
  Set cn = CreateObject ("ADODB.Connection")

  rs.CursorLocation = adUseClient

  Dim strConnect
  strConnect = "Provider=MSPersist;"

<%
  Dim RetPage	' The page number to return to after editing, i.e. the page we came from
  if Request.QueryString("retpg")="" then
    RetPage = 1
  else
    RetPage = cint(Request.QueryString("retpg"))
  end if

' The rs.open command (and others later) needs a full url - build it here from the request variables
' (this is just so that we don't have to hard code the url).
  Dim urlpath
  Dim RequestPath
  if Request.ServerVariables("HTTPS") = "on" then
    urlpath = "https://" & Request.ServerVariables("SERVER_NAME")
  else
    urlpath = "http://" & Request.ServerVariables("SERVER_NAME")
  end if

  RequestPath = Request.ServerVariables("PATH_INFO")
  urlpath = urlpath & Left(RequestPath, InStrRev(RequestPath, "/"))
%>
  cn.Open strConnect
  ' can use get_item_xml.asp instead, but atg is more efficient
  rs.Open "<%=urlpath%>get_item_atg.asp?itemid=<%=Request("itemid")%>", cn
<%
	If (Request("itemid") = 0) Then
%>
  ' New record - set some intial values
  rs("Title").Value = "[Title]"
  rs("Description").Value = "[Description]"
  rs("ImageVersion").Value = 1
  rs.Update
  Reset.Disabled = true
  Delete.Disabled = true
<%
	End If
%>
  rs.MarshalOptions = adMarshalModifiedOnly

  if rs.state = adStateOpen then
    InitControls
    me.Focus
  else
    MsgBox "The requested record could not be found, or is unavailable"
  end if
End Sub

</SCRIPT>
</HEAD>

<SCRIPT LANGUAGE="VBScript">
Option Explicit
Const adPersistXML = 1
Const adPersistADTG = 0
Const adSaveCreateOverWrite = 2
Const adUseClient = 3
Const adRecNew = &H0000001
Const adRecModified = &H0000002
Const adRecUnmodified = &H0000008


Sub InitControls
	DBPixThm.Image  = rs("Thumbnail")
	DBPixMain.Image = rs("Image")
	Title.Value     = rs("Title")
	Description.Value = rs("Description")
End Sub

Sub Title_OnChange
  rs("Title") = Title.Value
  rs.Update
End Sub

Sub Description_OnChange
  rs("Description") = Description.Value
  rs.Update
End Sub


Sub DBPixMain_ImageModified

  ' Update the thumbnail

  If DBPixMain.ImageBytes > 0 Then	
    DBPixThm.ImageLoadBlob(DBPixMain.Image)	
  Else
    DBPixThm.ImageClear
  End If

  ' Save new images and info to the recordset

  rs("Image")     = DBPixMain.Image
  rs("Thumbnail") = DBPixThm.Image

  rs("ImgWidth")  = DBPixMain.ImageWidth
  rs("ImgHeight") = DBPixMain.ImageHeight
  rs("ImgSize")   = DBPixMain.ImageBytes

  rs("ThmWidth")  = DBPixThm.ImageWidth
  rs("ThmHeight") = DBPixThm.ImageHeight
  rs("ThmSize")   = DBPixThm.ImageBytes

  ' Increment the image Id (used to prevent old versions from being returned by caches).
  rs("ImageVersion")   = rs("ImageVersion") + 1

  rs.Update

End Sub


Sub Delete_OnClick
  rs.Delete
  rs.Update
  DoSubmit
End Sub


Sub Reset_OnClick
  rs.CancelUpdate
  InitControls
End Sub


Sub Submit_OnClick

	if ((rs.Status = adRecModified) OR (rs.Status = adRecNew)) then
		DoSubmit
	else
		MsgBox "The record has not been changed"
	end if ' rs.Status = adRecModified

End Sub

Sub DoSubmit

	Dim stm
	Dim objHTTP
	Dim ResponseTxt

	' Create a stream and save the recordset to it
	Set stm = CreateObject("MSXML2.DOMDocument")
	rs.save stm, adPersistXML

	' Create an HTTP object and call the item_submit.asp script on the server, passing the updated recordset in the request headers
	Set objHTTP = CreateObject ("Microsoft.XMLHTTP")   
	objHTTP.open "POST", "<%=urlpath%>item_submit.asp", False
	objHTTP.send stm.xml

	'Wait until the request has been processed
	Do Until objHTTP.ReadyState = 4
		DoEvents
	Loop

	ResponseTxt = objHTTP.responseText

	'Cleanup objects
	Set objHTTP = Nothing  

	if ResponseTxt = "OK" then
	  'Navigate back to main page
	  Window.Navigate "default.asp?pg=<%=RetPage%>"
	else
	  MsgBox "An error occured saving your changes." & vbcrlf & vbcrlf & "Another user may be editing this record, " & vbcrlf & "or you may not have completed all required fields."
	end if

End Sub

' Map button clicks to DBPix methods

Sub LoadImage_OnClick
    DBPixMain.ImageLoad
End Sub

Sub PasteImage_OnClick
    DBPixMain.ImagePaste
End Sub

Sub TWAINImage_OnClick
    DBPixMain.TWAINAcquire
End Sub

Sub SaveImage_OnClick
    DBPixMain.ImageSave 
End Sub

Sub ClearImage_OnClick
    DBPixMain.ImageClear
End Sub

Sub CopyImage_OnClick
    DBPixMain.ImageCopy
End Sub
</SCRIPT>

<SCRIPT language="VBScript" FOR="window" EVENT="onbeforeunload">
'  if rs.state = adStateOpen then
'    if rs.Status = adRecModified then
'          window.event.returnValue = "Warning: Modified data has not been saved."
'    end if
'  end if
</SCRIPT>
<SCRIPT language="VBScript" FOR="window" EVENT="onunload" >
  if rs.state = adStateOpen then
    rs.Close
  end if
  Set rs = Nothing
</SCRIPT>
<body topmargin="10" leftmargin="0">
<center>
<p align="center"><font face="Arial" size="4" color="#003366"><strong><u>ASP Image Database Samples - Remote Editing with XMLHTTP</u></strong></font></p>
<!--LPK Example
<OBJECT CLASSID = "clsid:5220cb21-c88d-11cf-b347-00aa00a28331" VIEWASTEXT>
   <param NAME="LPKPath" VALUE="./dbpix/dbpix20.lpk">
</OBJECT>
-->
<table border="0" cellpadding="4" bgcolor="#FFFFFF">
  <tr>
    <td><font face="Arial" size="4" color="#003366"><strong><u>Edit Item</u></strong></font>
    </td>
    <td align="right"><div align="right"><input type="button" name="Submit" value="Submit" style="WIDTH: 60px"><input type="button" name="Reset" value="Reset" style="WIDTH: 60px"><input type="button" name="Delete" value="Delete" style="WIDTH: 60px"></div>
    </td>
  </tr>
  <tr>
	<td colspan="2"><hr></td>
  </tr>
  <tr>
    <td colspan="2">
	    <table border="0" cellpadding="0" cellspacing="2" width="100%">
        <tr>
          <td colspan="2" align="left" valign="top"><table border="0" cellpadding="0" cellspacing="0" width="100%">
              <tr>
                <td align="left" valign="top"><font face="Arial" size="2">Title:</font></td>
                <td align="left" valign="top"><!--<INPUT ID="Title" TYPE="TEXT" SIZE="80" NAME="Title">-->
				<TEXTAREA STYLE="overflow:hidden" NAME="Title" ROWS="1" COLS="60"></TEXTAREA>
				</td>
              </tr>
              <tr>
                <td align="left" valign="top"><font face="Arial" size="2">Description:&nbsp;&nbsp;</font></td>
                <td align="left" valign="top"><!--<INPUT ID="Description" TYPE="TEXT" SIZE="80" NAME="Description">-->
				<TEXTAREA NAME="Description" ROWS="6" COLS="60"></TEXTAREA>
				</td>
              </tr>
            </table>
          </td>
        </tr>
        <tr>
          <td colspan="2"><hr></td>
        </tr>
        <tr>
          <td align="left" valign="top"><font face="Arial" size="3" color="#003366"><strong><u>Image</u></strong></font>
		  <font face="Arial" size="2">&nbsp;Right-click for more tools and info.  Use &lt;Ctrl&gt; and &lt;Shift&gt; to zoom.</font>
					</td>
          <td align="left" valign="top"><font face="Arial" size="3" color="#003366"><strong><u>Thumbnail</u></strong></font>
					</td>
				</tr>
        <tr>
          <td colspan="2"><hr></td>
        </tr>
        <tr><!-- here are the DBPix controls.  Note that the control and image dimensions are chosen to support lossless rotation-->
          <td align="left" valign="top"><object classid="clsid:58444091-851A-46BC-BA63-904886070C0D" id="DBPixMain" height="512" width="512" style="HEIGHT: 512px; WIDTH: 512px"
		  codebase="./dbpix/DBPix20.ocx#version=2,0,3,2">
              <param name="_Version" value="65536">
              <param name="_ExtentX" value="11924">
              <param name="_ExtentY" value="11924">
              <param name="_StockProps" value="1">
              <param name="BackColor" value="12632256">
              <param name="ImageResampleWidth" value="512">
              <param name="ImageResampleHeight" value="512">
              <param name="ImageResampleMode" value="2">
              <param name="ImageSaveFormat" value="0">
              <param name="ViewInitialZoom" value="0">
              <param name="ViewHAlign" value="0">
              <param name="ViewVAlign" value="0">
              <param name="ViewMenuMode" value="0">
              <param name="JPEGQuality" value="80">
              <param name="PNGColorDepth" value="0">
              <param name="PNGCompression" value="0">
              <param name="PNGFilter" value="0">
              <param name="PNGInterlace" value="1">
              <param name="ImageDitherMethod" value="3">
              <param name="ImagePaletteMethod" value="4">
              <param name="JPEGEncoding" value="1">
              <param name="UseAmbientBackcolor" value="0">
              <param name="JPEGNoRecompress" value="0">
            </object>
					</td>
          <td align="left" valign="top"><object classid="clsid:58444091-851A-46BC-BA63-904886070C0D" id="DBPixThm" height="175" width="175" style="HEIGHT: 175px; WIDTH: 175px">
							<param name="_Version" value="65536">
							<param name="_ExtentX" value="11924">
							<param name="_ExtentY" value="11924">
							<param name="_StockProps" value="1">
							<param name="BackColor" value="12632256">
							<param name="ImageResampleWidth" value="175">
							<param name="ImageResampleHeight" value="175">
							<param name="ImageResampleMode" value="2">
							<param name="ImageSaveFormat" value="0">
							<param name="ViewInitialZoom" value="1">
							<param name="ViewHAlign" value="0">
							<param name="ViewVAlign" value="0">
							<param name="ViewMenuMode" value="1">
							<param name="JPEGQuality" value="70">
							<param name="PNGColorDepth" value="0">
							<param name="PNGCompression" value="0">
							<param name="PNGFilter" value="0">
							<param name="PNGInterlace" value="1">
							<param name="ImageDitherMethod" value="3">
							<param name="ImagePaletteMethod" value="4">
							<param name="JPEGEncoding" value="1">
							<param name="UseAmbientBackcolor" value="0">
							<param name="JPEGNoRecompress" value="0">
							<param name="ImageKeepMetaData" value="0">
						</object>
						<br><br>
						<table border="0" cellpadding="0" cellspacing="0" width="100%">
              <tr>
                <td valign="top">
                </td>
                <td align="left" valign="top" width="10"></td>
                <td align="right" valign="top">
                   <input type="button" name="LoadImage" value="Load" style="WIDTH: 55px"><br>
                   <input type="button" name="SaveImage" value="Save" style="WIDTH: 55px"><br>
                   <input type="button" name="ClearImage" value="Clear" style="WIDTH: 55px"><br>
                   <input type="button" name="CopyImage" value="Copy" style="WIDTH: 55px"><br>
                   <input type="button" name="PasteImage" value="Paste" style="WIDTH: 55px"><br>
                   <input type="button" name="TWAINImage" value="TWAIN" style="WIDTH: 55px"><br>
                </td>
              </tr>
            </table>
          </td>
        </tr>
        <tr>
          <td colspan="2"><hr></td>
        </tr>
      </table>
    </td>
  </tr>
</table>
</center>
</BODY>
</HTML> 

Back to sample