Recordset not returning record

Recordset not returning record

am 04.09.2006 02:44:51 von Iain Bishop

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:

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 = "
An item of content
with the same Element and Content ID already exists.
"
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:


"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@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
%>


<%

' *** 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 = "
Element and ContentID
must not be blank.
"
Element = "text"
Else
If IsNumeric(Request.Form("ContentID")) =False then
strErrorMessage = "
ContentID must be a
number.
"
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 = "
An item of content
with the same Element and Content ID already exists.
"
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
%>




Admin








type="text/css" />



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')">

width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">
















class="clsHead1">
class="clsHead1">

Iain
Bishop


cellpadding="0" cellspacing="0" summary="Software Development and Website
Design Heading">



Software Development, Web Design


class="clsHead1">
class="clsHead1">

















href="Introduction.asp" onmouseout="MM_swapImgRestore()"
onmouseover="MM_swapImage('imgIntroduction','','Images/Intro ductionHover.gif',1)"> src="Images/Introduction.gif" alt="Introduction" name="imgIntroduction"
width="76" height="76" border="0" id="imgIntroduction" />
href="SoftwareDatabases.asp" onmouseout="MM_swapImgRestore()"
onmouseover="MM_swapImage('imgSoftwareDatabases','','Images/ SoftwareDatabasesHover.gif',1)"> src="Images/SoftwareDatabases.gif" alt="Database Design and Software
Development" name="imgSoftwareDatabases" width="76" height="76" border="0"
id="imgSoftwareDatabases" />
href="WebDesign.asp" onmouseout="MM_swapImgRestore()"
onmouseover="MM_swapImage('imgWebDesign','','Images/WebDesig nHover.gif',1)"> src="Images/WebDesign.gif" alt="Web Design" name="imgWebDesign" width="76"
height="76" border="0" id="imgWebDesign" />
href="Portfolio.asp" onmouseout="MM_swapImgRestore()"
onmouseover="MM_swapImage('imgPortfolio','','Images/Portfoli oHover.gif',1)"> src="Images/Portfolio.gif" alt="Portfolio" name="imgPortfolio" width="76"
height="76" border="0" id="imgPortfolio" />
href="ContactInfo.asp" onmouseout="MM_swapImgRestore()"
onmouseover="MM_swapImage('imgContact','','Images/ContactHov er.gif',1)"> src="Images/Contact.gif" alt="Contact" name="imgContact" width="76"
height="76" border="0" id="imgContact" />

 


 


 


 


align="center">
cellpadding="0" cellspacing="0" summary="Software Development and Website
Design Main Content">







class="clsHead2Dark">Add
New Content


name="frmCreateContent" id="frmCreateContent">





























class="clsHead5Dark">Element: class="clsTextInput" value="<%=Request.Form("Element")%>" size="32"
maxlength="50">
class="clsHead5Dark">ContentID: class="clsTextInput" value="<%=Request.Form("ContentID")%>" size="32">
class="clsHead5Dark">Title: class="clsTextInput" value="<%=Request.Form("Title")%>" size="32"
maxlength="50">
class="clsHead5Dark">ContentImage: class="clsTextInput" value="<%=Request.Form("ContentImage")%>" readonly
size="32">
target="_blank"
onclick="MM_openBrWindow('AdminFileUpload.asp','','width=400 ,height=150');return
false;">Attach/Remove
Image
class="clsHead5Dark">ContentText:
  class="clsCommandButton" id="cmdCreate" value="Create Content">
class="clsCommandButton" onClick="document.location.href='
AdminContent.asp';" value="Cancel" />
align="left"><%= strErrorMessage %>




 




 




cellpadding="0">




align="center">Terms
Of Service
align="center">




Re: Recordset not returning record

am 04.09.2006 13:47:55 von Mike Brind

Iain Bishop wrote:
> 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:
>
> 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


http://classicasp.aspfaq.com/general/why-does-recordcount-re turn-as-1.html

--
Mike Brind