Recordset not returning record
am 04.09.2006 02:44:51 von Iain BishopI 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
%>
type="text/css" />