473,383 Members | 1,822 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Instr problems

347 100+
I have a page that uses a session variable that stores items like a shopping cart, all this works fine but i am trying to code a button that removes an item, i have a page that i have set up for the remove function and have tried the following code

Expand|Select|Wrap|Line Numbers
  1. dim firstHalf, secondHalf  
  2. firstHalf = left(session("recordsInCart"), instr(request.form("recordNum")))  
  3. secondHalf = right(session("recordsInCart"), len(session("recordsInCart")) - instr(request.form("recordNum")) - len (request.form("recordNum"))) 
  4. session("recordsInCart")=firstHalf&secondHalf 
this returns an error like this

this is returning an error

Microsoft VBScript runtime error '800a01c2'

Wrong number of arguments or invalid property assignment: 'instr'

not sure where to go from here as i know nothing about this

all i want to do is click on a button on the page that will send the variable recordNum from a form on the page back to the page and remove the numbers contained in this variable

e.g.
session variable = 1,2,3,4,5,6,7,8,9

recordNum = 4

click the button and remove 4 so that the session variable = 1,2,3,5,6,7,8,9

hope i have explained this properly
May 7 '09 #1
13 3278
GazMathias
228 Expert 128KB
As a minimum amount of arguments, you need to provide instr() with the string to search and the string to find.

So:

Expand|Select|Wrap|Line Numbers
  1. left(session("recordsInCart"), instr(session("recordsInCart"),request.form("recordNum")))
  2.  
Looking at it though I think replace() or split() may be better options.

For example using split you could add each recordNum to an array then loop through it adding each one back to your session variable except the one you don't want.

For example

Expand|Select|Wrap|Line Numbers
  1. If Not Request.Form("RecordNum") ="" Then
  2. CartItems = split(session("recordsInCart"),",")
  3. For Each Item in CartItems
  4. If Not Item = Request.Form("RecordNum") Then newitems = newitems & Item & ","
  5. Next
  6. session("recordsInCart") = newitems
  7. End If
  8.  
Gaz
May 7 '09 #2
colinod
347 100+
I understand this but it is returning an error with the sql statement that uses the session variable??

Expand|Select|Wrap|Line Numbers
  1. "SELECT * FROM mp3 INNER JOIN celebs ON mp3.celebid = celebs.idnumber WHERE idnumbermp3 in (" & session("recordsInCart") & ") ORDER BY celebs.surname"
i have added a response.write on the last line and it is removing the item numbers but leaving the commas? how can i get rid of them as they must be causing the problem

any ideas it worked with the old code
May 7 '09 #3
GazMathias
228 Expert 128KB
Can you post here the session variable before and after the items have been removed so I can take a look see?

I probably just need to modify the loop slightly (I didn't test it!)

Never mind that.

I copied the code from here and modified it.

Expand|Select|Wrap|Line Numbers
  1. <%
  2.  
  3. mytest= "1,2,3,4,5,6,7,8,9,10,"
  4.  
  5. remove = "4"
  6.  
  7. If Not remove ="" Then
  8. CartItems = split(mytest,",")
  9. For Each Item in CartItems
  10. If Not Item = remove Then
  11. If IsNnumeric(Item) then newitems = newitems & Item & ","
  12. End If
  13. Next
  14. mytest = left(newitems, len(newitems)-1) 'remove trailing comma from each loop iteration
  15. End If
  16.  
  17. Response.Write mytest
  18.  
  19. %>
  20.  
This prduces 1,2,3,5,6,7,8,9,10 either with or without a trailing comma in the initial variable.

Just change my variables to yours.

Gaz
May 7 '09 #4
colinod
347 100+
This is the session variable from the page previous to the delete page

0,1634,2089,1638,1633,

this is the error i get when i try and delete an item on the delete page

[Microsoft][ODBC Microsoft Access Driver] Syntax error (missing operator) in query expression 'idnumbermp3 in (0,1634,2089,1633,,)'.

the can be seen if you follow one of the artists on the following link
www.yaketyyakallmouth.com/pages/boys.asp?id=all

not sure why the 0 is at the front either but thats always been there
May 7 '09 #5
GazMathias
228 Expert 128KB
That now produces 0,1634,2089,1638,1633 using the modified loop in my prior post.

Can you test that now?
May 7 '09 #6
colinod
347 100+
I now have

Expand|Select|Wrap|Line Numbers
  1. <% 
  2.  
  3. If Not request.form("recordNum") ="" Then 
  4. CartItems = split(session("recordsInCart") ,",") 
  5. For Each Item in CartItems 
  6. If Not Item = request.form("recordNum") Then 
  7. If IsNumeric(Item) then newitems = newitems & Item & "," 
  8. End If 
  9. Next 
  10. session("recordsInCart") = left(newitems, len(newitems)-1) 'remove trailing comma from each loop iteration 
  11. End If 
  12.  
  13. Response.Write session("recordsInCart") 
  14.  
  15. %> 
this works fine, but if i go back to another artist after deleting something it does not have a comma in the middle of the old number and the new one?
May 7 '09 #7
colinod
347 100+
hi it now works i removed the -1

its now

Expand|Select|Wrap|Line Numbers
  1. <%  
  2.  
  3. If Not request.form("recordNum") ="" Then  
  4. CartItems = split(session("recordsInCart") ,",")  
  5. For Each Item in CartItems  
  6. If Not Item = request.form("recordNum") Then  
  7. If IsNumeric(Item) then newitems = newitems & Item & ","  
  8. End If  
  9. Next  
  10. session("recordsInCart") = left(newitems, len(newitems)) 'remove trailing comma from each loop iteration  
  11. End If  
  12.  
  13. Response.Write session("recordsInCart")  
  14.  
  15. %>  
May 7 '09 #8
colinod
347 100+
just wondering if anyone could help with something else, i want to change the image of the button used to add something to the cart if it exists in the cart already

the code used to add to the cart is the following

<%

if InStr(session("recordsInCart"), ","&request.form("recordNum")) = 0 then
session("recordsInCart") = session("recordsInCart") + request.form("recordNum") &","
else
'do nothing
end if
%>

and the form that contains the button is

Expand|Select|Wrap|Line Numbers
  1. Response.write("<form action=""boys2.asp?id=" & yaketyRecordset("idnumber") & "&amp;voicetype=" &voiceid &""" method=""post"">" & vbNewline)%>
  2.                                       <TD>
  3.                                     <input type="hidden" name="recordNum" value="<%=dlRecordset("idnumbermp3")%>">
  4.                                     </TD>
  5.                                       <TD><INPUT name="submit" type="image" src="../images/makeup/shortlistpink.gif" alt="Add to shortlist" align="bottom" border="0"></TD>
  6.                                     </form>
i know this must be really simple just to ask if the id number is in the session variable and then say the image is......

just not that good with asp
May 7 '09 #9
colinod
347 100+
hi

Using my session("recordsInCart") could i find if my number stored in recordNum is in it and then change a picture depending on it, its so i can have the button that adds to the cart show a different picture if its already in the session variable

sorry ive already asked you this!!!!!
May 8 '09 #10
jhardman
3,406 Expert 2GB
@colinod
Expand|Select|Wrap|Line Numbers
  1. if instr(...) > 0 then
May 11 '09 #11
colinod
347 100+
Hi
I think i understand what you mean, i have tried to code it so that depending on the Instr result it writes a different line of code for the submit button, only problem is that no button appears, im sure ive done something wrong in my code

Expand|Select|Wrap|Line Numbers
  1. <%
  2. if InStr(session("recordsInCart"), ","&dlRecordset("idnumbermp3")) > 0 then 
  3. Response.Write("<INPUT name=""submit"" type=""image"" src=""../images/makeup/shortlistpink.gif"" alt=""Add to shortlist"" align=""bottom"" border=""0"">") & vbCRLF
  4. else 
  5. Response.Write("<INPUT name=""submit"" type=""image"" src=""../images/makeup/shortlistyellow.gif"" alt=""Add to shortlist"" align=""bottom"" border=""0"">") & vbCRLF
  6. end if 
  7. %>
May 12 '09 #12
jhardman
3,406 Expert 2GB
For troubleshooting purposes and readability, take it out of the response.write. Usually this type of error is just a result of too many quote marks in the response.write. Remember that HTML is much more straight-forward, so if you can write it all int plain HTML do it.

Expand|Select|Wrap|Line Numbers
  1. <%
  2. if InStr(session("recordsInCart"), ","&dlRecordset("idnumbermp3")) > 0 then 
  3.  %>
  4. <INPUT name="submit" type="image" src="../images/makeup/shortlistpink.gif" alt="Add to shortlist" align="bottom" border="0">
  5. <%
  6. else %>
  7. <INPUT name="submit" type="image" src="../images/makeup/shortlistyellow.gif" alt="Add to shortlist" align="bottom" border="0">
  8. <%
  9. end if 
  10. %>
May 12 '09 #13
colinod
347 100+
changed it to straight HTML and its works fine thanks for your help
May 15 '09 #14

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: DTB | last post by:
I am trying to convert a complex function from Oracle to SQL Server and have come across Oracle's Instr() function. I see SQL Server has CHARINDEX() which is similar, however it does not provide...
6
by: Chris Calzaretta | last post by:
Hello Everybody, Question instr function will give you the first instance of the finding so EX: so your string looks like string1 = "testing>This is > just a test > testtesttest"...
4
by: Gordon | last post by:
Hi; I am trying to extract a substring using a combination of the mid() and Instr() i.e. aString = "Jones, Thomas R, Dr." hold = InStr(1, aString, " ," , 1) newString = Mid(aString, 1,...
3
by: Mary | last post by:
MemberID VID 1002 1001 1003 1002 1004 1003 1005 1003 1007 1001...
4
by: fischerspooner | last post by:
Hi, I'm banging my head against the desk because I can't find a solution for the following simple problem. Case: There is a column in a table that has FamilyName and FirstName(s) in one field....
12
by: rodchar | last post by:
hey all, i'm getting a result that i don't understand i have a string "test1, test2" If InStr("test1") and InStr("test2") Then 'Inside EndIf The inside is not running for some reason. Any...
3
by: lstrudeman | last post by:
Hello; A friend gave me this syntax and they are unavailable at the moment and I need this asap. I am trying to figure out how SQL figures this out. Below the syntax takes a field in a file and...
3
by: Kubie | last post by:
I'm having problems finding the syntax/comands that do the this correctly. I'm must not be doing the join right or something.. List one AAC1234 CDA2w31 12ZZC12 12TRE1 23234 243
3
by: Alex Pavluck | last post by:
I have a date stored like this '2004 - 2006' and I use this code to get startyear and stopyear StartYear: Trim(Left(,InStr(,"-")-1)) StopYear: Trim(Right(,InStr(,"-")-1)) Is there a way to...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.