I have an Access database with a table called "Content" in it, with
keyfields "Element" (Text) and "ContentID" (Number).
I am trying to access a record I have in there which has the following
values: Element = 'Contact', ContentID = 1.
Relevant code looks like this:
<!--#include file="Connections/conIainBishopSWService.asp" -->
Dim recContent
Dim recContentRecordCount
Set recContent = Server.CreateObject("ADODB.Recordset")
recContent.ActiveConnection = MM_conIainBishopSWService_STRING
recContent.Source = "SELECT Element, ContentID, Title FROM Content
WHERE Element = '" & Request.Form("Element") & "' AND ContentID = " &
Request.Form("ContentID")
recContent.CursorType = 0
recContent.CursorLocation = 2
recContent.LockType = 1
recContent.Open()
recContentRecordCount=recContent.RecordCount
recContent.Close()
Set recContent = Nothing
If recContentRecordCount > 0 Then
strErrorMessage = "<span class=""clsHead4Dark""><br>An item of content
with the same Element and Content ID already exists.</span>"
Else ......
The problem is that my recordset returns a recordcount of -1.
My ActiveConnection & Source properties both look ok:
Provider=Microsoft.Jet.OLEDB.4.0;Password="";User ID=admin;Data
Source=\\Piii-1300\Store (E)\Websites\Databases\Iain Bishop SW
Service.mdb;Mode=Share Deny None;Extended Properties="";Jet OLEDB:System
database="";Jet OLEDB:Registry Path="";Jet OLEDB:Database
Password=4roo5ster16;Jet OLEDB:Engine Type=5;Jet OLEDB:Database Locking
Mode=1;Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Global Bulk
Transactions=1;Jet OLEDB:New Database Password="";Jet OLEDB:Create System
Database=False;Jet OLEDB:Encrypt Database=False;Jet OLEDB:Don't Copy Locale
on Compact=False;Jet OLEDB:Compact Without Replica Repair=False;Jet
OLEDB:SFP=False
SELECT Element, ContentID, Title FROM Content WHERE Element = 'Contact' AND
ContentID = 1
I just don't understand why the Recordset doesn't return the record. Can
anyone please help?
The record is definitely present in the table since the following SQL
returns a duplicate keyfield error:
insert into Content (Element,ContentID,Title,ContentImage,ContentText)
values ('Contact',1,'','','')
Many Thanks
Iain
Full page code if required is as follows:
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<% [at] LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
<%
' *** Restrict Access To Page: Grant or deny access to this page
MM_authorizedUsers=""
MM_authFailedURL="AdminLogin.asp"
MM_grantAccess=false
If Session("MM_Username") <> "" Then
If (true Or CStr(Session("MM_UserAuthorization"))="") Or _
(InStr(1,MM_authorizedUsers,Session("MM_UserAuthorization")) >=1)
Then
MM_grantAccess = true
End If
End If
If Not MM_grantAccess Then
MM_qsChar = "?"
If (InStr(1,MM_authFailedURL,"?") >= 1) Then MM_qsChar = "&"
MM_referrer = Request.ServerVariables("URL")
if (Len(Request.QueryString()) > 0) Then MM_referrer = MM_referrer & "?" &
Request.QueryString()
MM_authFailedURL = MM_authFailedURL & MM_qsChar & "accessdenied=" &
Server.URLEncode(MM_referrer)
Response.Redirect(MM_authFailedURL)
End If
%>
<!--#include file="Connections/conIainBishopSWService.asp" -->
<!--#include file="Tools/freeASPUpload.asp" -->
<%
' *** Edit Operations: declare variables
dim debug1
Dim MM_editAction
Dim MM_abortEdit
Dim MM_editQuery
Dim MM_editCmd
Dim MM_editConnection
Dim MM_editTable
Dim MM_editRedirectUrl
Dim MM_editColumn
Dim MM_recordId
Dim MM_fieldsStr
Dim MM_columnsStr
Dim MM_fields
Dim MM_columns
Dim MM_typeArray
Dim MM_formVal
Dim MM_delim
Dim MM_altVal
Dim MM_emptyVal
Dim MM_i
MM_editAction = CStr(Request.ServerVariables("SCRIPT_NAME"))
If (Request.QueryString <> "") Then
MM_editAction = MM_editAction & "?" & Request.QueryString
End If
' boolean to abort record edit
MM_abortEdit = false
' query string to execute
MM_editQuery = ""
%>
<%
' *** Insert Record: set variables
Dim recContent
Dim recContentRecordCount
If (CStr(Request("MM_insert")) = "form1") Then
' Validate Form Fields
If Request.Form("Element") = "" or Request.Form("ContentID") = "" Then
strErrorMessage = "<span class=""clsHead4Dark""><br>Element and ContentID
must not be blank.</span>"
Element = "text"
Else
If IsNumeric(Request.Form("ContentID")) =False then
strErrorMessage = "<span class=""clsHead4Dark""><br>ContentID must be a
number.</span>"
Else
'Check that this piece of content doesn't already exist - Element and
ContentID combo must be unique
Set recContent = Server.CreateObject("ADODB.Recordset")
recContent.ActiveConnection = MM_conIainBishopSWService_STRING
recContent.Source = "SELECT Element, ContentID, Title FROM Content
WHERE Element = '" & Request.Form("Element") & "' AND ContentID = " &
Request.Form("ContentID")
recContent.CursorType = 0
recContent.CursorLocation = 2
recContent.LockType = 1
recContent.Open()
recContentRecordCount=recContent.RecordCount
recContent.Close()
Set recContent = Nothing
If recContentRecordCount > 0 Then
strErrorMessage = "<span class=""clsHead4Dark""><br>An item of content
with the same Element and Content ID already exists.</span>"
Else
'Validation successful so insert record
strErrorMessage = ""
MM_editConnection = MM_conIainBishopSWService_STRING
MM_editTable = "Content"
MM_editRedirectUrl = "AdminContent.asp"
MM_fieldsStr =
"Element|value|ContentID|value|Title|value|ContentImage|valu e|ContentText|value"
MM_columnsStr =
"Element|',none,''|ContentID|none,none,NULL|Title|',none,''| ContentImage|',none,''|ContentText|',none,''"
' create the MM_fields and MM_columns arrays
MM_fields = Split(MM_fieldsStr, "|")
MM_columns = Split(MM_columnsStr, "|")
' set the form values
For MM_i = LBound(MM_fields) To UBound(MM_fields) Step 2
MM_fields(MM_i+1) = CStr(Request.Form(MM_fields(MM_i)))
Next
' append the query string to the redirect URL
If (MM_editRedirectUrl <> "" And Request.QueryString <> "") Then
If (InStr(1, MM_editRedirectUrl, "?", vbTextCompare) = 0 And
Request.QueryString <> "") Then
MM_editRedirectUrl = MM_editRedirectUrl & "?" & Request.QueryString
Else
MM_editRedirectUrl = MM_editRedirectUrl & "&" & Request.QueryString
End If
End If
' *** Insert Record: construct a sql insert statement and execute it
Dim MM_tableValues
Dim MM_dbValues
If (CStr(Request("MM_insert")) <> "") Then
' create the sql insert statement
MM_tableValues = ""
MM_dbValues = ""
For MM_i = LBound(MM_fields) To UBound(MM_fields) Step 2
MM_formVal = MM_fields(MM_i+1)
MM_typeArray = Split(MM_columns(MM_i+1),",")
MM_delim = MM_typeArray(0)
If (MM_delim = "none") Then MM_delim = ""
MM_altVal = MM_typeArray(1)
If (MM_altVal = "none") Then MM_altVal = ""
MM_emptyVal = MM_typeArray(2)
If (MM_emptyVal = "none") Then MM_emptyVal = ""
If (MM_formVal = "") Then
MM_formVal = MM_emptyVal
Else
If (MM_altVal <> "") Then
MM_formVal = MM_altVal
ElseIf (MM_delim = "'") Then ' escape quotes
MM_formVal = "'" & Replace(MM_formVal,"'","''") & "'"
Else
MM_formVal = MM_delim + MM_formVal + MM_delim
End If
End If
If (MM_i <> LBound(MM_fields)) Then
MM_tableValues = MM_tableValues & ","
MM_dbValues = MM_dbValues & ","
End If
MM_tableValues = MM_tableValues & MM_columns(MM_i)
MM_dbValues = MM_dbValues & MM_formVal
Next
MM_editQuery = "insert into " & MM_editTable & " (" & MM_tableValues &
") values (" & MM_dbValues & ")"
If (Not MM_abortEdit) Then
' execute the insert
Set MM_editCmd = Server.CreateObject("ADODB.Command")
MM_editCmd.ActiveConnection = MM_editConnection
MM_editCmd.CommandText = MM_editQuery
debug1=MM_editCmd.CommandText
MM_editCmd.Execute
MM_editCmd.ActiveConnection.Close
If (MM_editRedirectUrl <> "") Then
Response.Redirect(MM_editRedirectUrl)
End If
End If
End If
End If
End If
End If
Else
'first time into this page so clear the CurrentAttachedImage session
variable as this is a Create Content page
'so there is no currently attached image
session("CurrentAttachedImage")=""
End If
%>
<html xmlns="http://www.w3.org/1999/xhtml"><!-- InstanceBegin
template="/Templates/Standard Template.dwt.asp"
codeOutsideHTMLIsLocked="false" -->
<head>
<!-- InstanceBeginEditable name="doctitle" -->
<title>Admin</title>
<!-- InstanceEndEditable -->
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<!-- InstanceBeginEditable name="head" -->
<meta name="Robots" content="noindex">
<script language="JavaScript" type="text/JavaScript">
<!--
function MM_openBrWindow(theURL,winName,features) { //v2.0
window.open(theURL,winName,features);
}
//-->
</script>
<!-- InstanceEndEditable -->
<link href="Stylesheets/Standard%20Styles.css" rel="stylesheet"
type="text/css" />
<script language="JavaScript" type="text/JavaScript">
<!--
function MM_preloadImages() { //v3.0
var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length;
i++)
if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
function MM_swapImgRestore() { //v3.0
var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++)
x.src=x.oSrc;
}
function MM_findObj(n, d) { //v4.01
var p,i,x; if(!d) d=document;
if((p=n.indexOf("?"))>0&&parent.frames.length) {
d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++)
x=d.forms[i][n];
for(i=0;!x&&d.layers&&i<d.layers.length;i++)
x=MM_findObj(n,d.layers[i].document);
if(!x && d.getElementById) x=d.getElementById(n); return x;
}
function MM_swapImage() { //v3.0
var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array;
for(i=0;i<(a.length-2);i+=3)
if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc)
x.oSrc=x.src; x.src=a[i+2];}
}
//-->
</script>
</head>
<body leftmargin="30" rightmargin="30" topmargin="30" bottommargin="30"
marginwidth="0" marginheight="0" padding="0"
onload="MM_preloadImages('Images/Introduction-Hover.gif','Im ages/IntroductionHover.gif','Images/SoftwareDatabasesHover.g if','Images/WebDesignHover.gif','Images/PortfolioHover.gif', 'Images/ContactHover.gif')">
<table summary="Software Development and Website Design Page Content"
width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="250" height="15" align="left" valign="top" bgcolor="#8F9C79"
class="clsHead1"></td>
<td align="left" valign="top" bgcolor="#C8CEBF" class="clsHead1"></td>
</tr>
<tr>
<td width="250" align="center" valign="top" bgcolor="#8F9C79"
class="clsHead1"><p class="clsHead1Light">Iain
Bishop</p></td>
<td align="left" valign="top" bgcolor="#C8CEBF" class="clsHead1">
<table width="95%" height="100%" border="0" align="center"
cellpadding="0" cellspacing="0" summary="Software Development and Website
Design Heading">
<tr>
<td><p class="clsHead1Dark">Software Development, Web Design</p></td>
</tr>
</table>
</td>
</tr>
<tr>
<td width="250" height="15" align="left" valign="top" bgcolor="#8F9C79"
class="clsHead1"></td>
<td align="left" valign="top" bgcolor="#C8CEBF" class="clsHead1"></td>
</tr>
<tr>
<td width="250" align="center" valign="top" bgcolor="#8F9C79"
class="clsHead1"><div align="center">
<table summary="Software Development and Website Design Links
Content" width="240" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="80" height="80"><div align="center"></div></td>
<td width="80" height="80"><div align="center"><a
href="Introduction.asp" onmouseout="MM_swapImgRestore()"
onmouseover="MM_swapImage('imgIntroduction','','Images/Intro ductionHover.gif',1)"><img
src="Images/Introduction.gif" alt="Introduction" name="imgIntroduction"
width="76" height="76" border="0" id="imgIntroduction" /></a></div></td>
<td width="80" height="80"><div align="center"></div></td>
</tr>
<tr>
<td width="80" height="80"><div align="center"><a
href="SoftwareDatabases.asp" onmouseout="MM_swapImgRestore()"
onmouseover="MM_swapImage('imgSoftwareDatabases','','Images/ SoftwareDatabasesHover.gif',1)"><img
src="Images/SoftwareDatabases.gif" alt="Database Design and Software
Development" name="imgSoftwareDatabases" width="76" height="76" border="0"
id="imgSoftwareDatabases" /></a></div></td>
<td width="80" height="80"><div align="center"><a
href="WebDesign.asp" onmouseout="MM_swapImgRestore()"
onmouseover="MM_swapImage('imgWebDesign','','Images/WebDesig nHover.gif',1)"><img
src="Images/WebDesign.gif" alt="Web Design" name="imgWebDesign" width="76"
height="76" border="0" id="imgWebDesign" /></a></div></td>
<td width="80" height="80"><div align="center"></div></td>
</tr>
<tr>
<td width="80" height="80"><div align="center"></div></td>
<td width="80" height="80"><div align="center"><a
href="Portfolio.asp" onmouseout="MM_swapImgRestore()"
onmouseover="MM_swapImage('imgPortfolio','','Images/Portfoli oHover.gif',1)"><img
src="Images/Portfolio.gif" alt="Portfolio" name="imgPortfolio" width="76"
height="76" border="0" id="imgPortfolio" /></a></div></td>
<td width="80" height="80"><div align="center"><a
href="ContactInfo.asp" onmouseout="MM_swapImgRestore()"
onmouseover="MM_swapImage('imgContact','','Images/ContactHov er.gif',1)"><img
src="Images/Contact.gif" alt="Contact" name="imgContact" width="76"
height="76" border="0" id="imgContact" /></a></div></td>
</tr>
</table>
<p align="center"> </p>
<p align="center"> </p>
<p align="center"> </p>
<p align="center"> </p>
</div></td>
<td align="left" valign="top" bgcolor="#C8CEBF" class="clsHead1"><div
align="center">
<table width="95%" height="100%" border="0" align="center"
cellpadding="0" cellspacing="0" summary="Software Development and Website
Design Main Content">
<tr>
<td valign="top">
<!-- InstanceBeginEditable name="rgnMain" --><span
class="clsHead2Dark">Add
New Content</span>
<form action="<%=MM_editAction%>" method="post"
name="frmCreateContent" id="frmCreateContent">
<table align="left">
<tr valign="baseline">
<td align="right" nowrap
class="clsHead5Dark">Element:</td>
<td> <input name="Element" type="text"
class="clsTextInput" value="<%=Request.Form("Element")%>" size="32"
maxlength="50">
</td>
</tr>
<tr valign="baseline">
<td height="25" align="right" nowrap
class="clsHead5Dark">ContentID:</td>
<td> <input name="ContentID" type="text"
class="clsTextInput" value="<%=Request.Form("ContentID")%>" size="32">
</td>
</tr>
<tr>
<td align="right" valign="top" nowrap
class="clsHead5Dark">Title:</td>
<td valign="baseline"> <input name="Title" type="text"
class="clsTextInput" value="<%=Request.Form("Title")%>" size="32"
maxlength="50">
</td>
</tr>
<tr valign="baseline">
<td align="right" nowrap
class="clsHead5Dark">ContentImage:</td>
<td> <input name="ContentImage" type="text"
class="clsTextInput" value="<%=Request.Form("ContentImage")%>" readonly
size="32">
<span class="clsTextDark"> <a href="AdminFileUpload.asp"
target="_blank"
onclick="MM_openBrWindow('AdminFileUpload.asp','','width=400 ,height=150');return
false;">Attach/Remove
Image</a></span></td>
</td>
</tr>
<tr valign="baseline">
<td align="right" valign="top" nowrap
class="clsHead5Dark">ContentText:</td>
<td> <textarea name="ContentText" cols="34" rows="15"
class="clsTextInput"><%=Request.Form("ContentText")%></textarea></td>
</tr>
<tr valign="baseline">
<td nowrap align="right"> </td>
<td> <input name="cmdCreate" type="submit"
class="clsCommandButton" id="cmdCreate" value="Create Content">
<input name="cmdCancel" type="button"
class="clsCommandButton" onClick="document.location.href='
AdminContent.asp';" value="Cancel" />
</td>
</tr>
<tr valign="baseline">
<td colspan="2" align="right" nowrap><div
align="left"><%= strErrorMessage %></div></td>
</tr>
</table>
<input type="hidden" name="MM_insert" value="form1">
</form>
<p> </p>
<!-- InstanceEndEditable --> </td>
</tr>
<tr>
<td height="60" valign="top"><div align="center">
<hr align="center" width="300" />
<table width="200" border="0" cellspacing="0"
cellpadding="0">
<tr>
<td width="50%" class="clsSmallTextDark"><div
align="center"><a href="Terms.asp">Terms
Of Service</a></div></td>
<td width="50%" class="clsSmallTextDark"><div
align="center">Administration</div></td>
</tr>
</table>
</div></td>
</tr>
</table>
</div></td>
</tr>
</table>
</body>
<!-- InstanceEnd --></html>
