Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Validation of a data base field

Status
Not open for further replies.

mal54

Programmer
Mar 11, 2002
13
AU
Im having troble validating a data base field the field is a key field in MS Access This is the function I have highlighted the offending code can anyone help?

function insertEntry( byref conn )
dim InstrID
dim TypeId
dim txtTitle
dim txtEntryDate
dim txtPassword
dim FirstName
dim LastName
dim txtStreetAddress
dim txtSuburb
dim txtState
dim txtBusinessPhone
dim txtEmail
dim iLength
dim iRecordId
dim rs
dim aInstrID
dim Sql

' read parameters.

InstrID = request("InstrID")
TypeID = request("TypeID")
txtTitle = request("txtTitle")
txtPassword = request("txtPassword")
FirstName = request("FirstName")
LastName = request("LastName")
txtStreetAddress = request("txtStreetAddress")
txtSuburb = request("txtSuburb")
txtState = request("txtState")
txtBusinessPhone = request("txtBusinessPhone")
txtEmail = request("txtEmail")


' open recordset.

Sql = "select from Instructors InstrID"
Set conn = Session("Learner_Connection")
set rs = Server.CreateObject("ADODB.Recordset")
rs.Open "Instructors", conn, 2, 3

aInstrID = rs.fields("InstrID").value

'check parameters.
if not rs.eof then
If aInstrID = InstrID Then
insertEntry = false
exit function
end if
end if



' create new recordset.

rs.AddNew
rs("InstrID") = InstrID
rs("TypeId") = TypeID
rs("txtTitle") = txtTitle
rs("txtEntryDate") =Now
rs("txtPassword") = txtPassword
rs("FirstName") = FirstName
rs("LastName") = LastName
rs("txtStreetAddress") = txtStreetAddress
rs("txtSuburb") = txtSuburb
rs("txtState") = txtState
rs("txtBusinessPhone") = txtBusinessPhone
rs("txtEmail") = txtEmail
rs.Update

' succeeded.
insertEntry = true

rs.Close
Set rs = Nothing
end function

 
What you are doing here will eventually bring the server to it's knees, as you don't close and destroy the connection to the db, when you call exit function.

I don't see why you pass the connection to the function, as you later get it from a sessionvariable.

But to address your question:
You should be able to do something like the following:
Code:
strSQL = "SELECT * FROM Instructors I WHERE InstrId = " & InstrId
Set conn = Session("Learner_Connection") 
set rs = Server.CreateObject("ADODB.Recordset") 
rs.Open strSQL, conn, 1, 3 
If (rs.bof AND rs.eof) then 'No such ID
	rs.AddNew
	[...]
	rs.Update
	InsertEntry = TRUE
Else
	InsertEntry = FALSE
End if
rs.close
set rs = Nothing
conn.close
set conn = Nothing

By the way:
If you are getting the contents from formfields, you should use request.FORM as this is less heavy on the server + it closes a possible hole, enabling users to pass values into your code from querystring-parametres. This is not a bug - it's an undocumented feature...
;-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top