Using variable in SELECT statement
am 31.08.2005 20:19:15 von ShasI'm pretty new to asp but have coded before (many years ago) and
understand databases. I am trying to use a variable in a SELECT
statement, which I have done elsewhere in the same script, but can't
get this particular bit to work.
I'm retrieving a property record and then picking up a photo for each
property record I find. This code works:
'Set query string to extract just records with appropriate property
type
propstring1 = "'" & Session("lsproptype") & "'"
propstring2 = "SELECT * FROM property WHERE PropType = " & propstring1
proprs.Open propstring2, conn
' Loop through records found and display
DO UNTIL proprs.EOF
Set photors = conn.Execute ("SELECT Photopath1 FROM photopath WHERE
PropID = 16")
proprs.MoveNext
LOOP
But if I try to amend the SELECT in the loop to use a variable instead
of the constant "16", I have problems. I've also tried other ways of
writing the statement with the constant in to help get the syntax right
but no luck. Here's what I've tried so far:
'This statement does not work
photostring = "SELECT Photopath1 FROM photopath WHERE PropID = 16"
Set photors = conn.Execute photostring
'Neither does this using either of the photostring2 options
photostring1 = proprs("PropertyID")
photostring2 = "SELECT Photopath1 FROM photopath WHERE PropID = " &
(photostring1)
photostring2 = "SELECT Photopath1 FROM photopath WHERE PropID = 16"
photors.Open photostring2, conn
'Neither does this
photostring = "(" & "SELECT Photopath1 FROM photopath WHERE PropID =
16" & ")"
Set photors = conn.Execute photostring
'Neither does this
Set photors = conn.Execute _
('SELECT Photopath1 FROM photopath WHERE PropID = 16')
'Neither does this
Set photors = conn.Execute ("SELECT Photopath1 FROM photopath WHERE
PropID = '" & proprs("PropertyID") & "'")
'Neither does this
Set photors = conn.Execute ("SELECT Photopath1 FROM photopath WHERE
PropID = " & PropertyID)
'Neither does this
photoid = proprs("PropertyID")
Set photors = conn.Execute ("SELECT Photopath1 FROM photopath WHERE
PropID = " & photoid)
Any help very welcome - don't know what else to try!