• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 281
  • Last Modified:

Problem with Instr

Hello, Experts.

I'm using "if Instr(var1, var2)", to check if var2 it exists in var1
var1 = its a group of unique numbers separated by commas (Ex: 1, 3, 9, 21, 45, 53)
var2 = a simple number (the primary key of a table)

The problem is: when the var2 has an unique char as 2, for example, and the var1 is compose by 21, 22, 23... 29, it shows as if 2 was in the string, but in fact it is not.

Is there any way to compare exactly the numbers? How can I solve this problem?

Many thanks,
Drix
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td class="menu_admin">&nbsp;</td>
    <td class="menu_admin"><span style="font-weight: bold">Data</span></td>
    <td class="menu_admin"><span style="font-weight: bold">Titulo</span></td>
  </tr>
  <% 
     comando = "SELECT * FROM tbl_noticias ORDER by id_noticia ASC"
     call abrers(comando, "listar", RSNews)
     
     comando = "SELECT conteudo FROM tbl_Emails"
     call abrers(comando, "listar", RSCont)
     ' conteudo retuns numbers separated by commas. Example: 1, 3, 9, 27, 48
     
     DO WHILE NOT RSCont.EOF
         conteudo = conteudo & RSCont("conteudo") & ", "
     RSCont.MoveNext
     loop
  %>
  <%
    DO WHILE NOT RSNews.EOF
    id_new = RSNews("id_noticia")
  %>
  <tr <%If Instr(conteudo,id_new) Then Response.Write " bgcolor='ffffff'"%>>
    <td nowrap><%=id_new%> - <input name="conteudo" type="checkbox" id="checkbox" value="<%=id_new%>" <%if Instr(conteudo,id_new) Then Response.Write " checked"%>></td>
    <td <%If Instr(conteudo,id_new) Then Response.Write " class='num_pos'"%>><%=RSNews("dt_noticia")%>&nbsp;</td>
    <td <%If Instr(conteudo,id_new) Then Response.Write " class='num_pos'"%>><%=RSNews("titulo")%>&nbsp;</td>
  </tr>
<%
RSNews.MoveNext
loop
%>
</table>

Open in new window

0
drix76
Asked:
drix76
  • 7
  • 6
  • 4
1 Solution
 
sdstuberCommented:
append a comma to both and do the instr

instr(var1+',' , var2+',')
0
 
drix76Author Commented:
Thank you for the fast answer.

It worked, but now there's another issue:

When I put the - &"," - now whe I have in var conteudo a number as 32 or 42 it still showing checked the number 2.

Please, help!!!
0
 
sdstuberCommented:
same idea, put a comma in front of it

instr(',' & var1 &',' , ',' & var2 & ',')

this assumes there is no whitespace though.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
Bobaran98Commented:
I suggest converting your string to an array instead, then looping through the values to see if any of them match.  I've created a function for you below called inArray that does this loop.

Notes about my code:
  • before I use Split() to break your string into array elements, I do a replace that takes all spaces and replaces them with nothing
  • for the search term, I add an empty string to your id_new variable... that's because the elements of your conteudoArr() are by default strings and won't match against integers; by adding the empty string to the search term, I force it to be a string
Hope this helps!


'declare function that will search through array looking for value:
 
Function inArray(ByRef arrName() As String, ByVal srchTerm As String)
	
	For i as Integer = 0 To Ubound(arrName)
		If arrName(i) = srchTerm Then
			inArray = True
			Exit Function
		End If
	Next
 
	inArray = False
 
End Function
 
 
 
'later on in your code:
 
Dim conteudoArr() As String
conteudoArr = Split(Replace(conteudo," ",""),",")
 
 
'now you just call our inArray() function as part of your conditional:
If inArray(conteudo, id_new & "") Then
 
	'stuff
	'more stuff
	'etc...
 
End If

Open in new window

0
 
drix76Author Commented:
Thanks for your replay, but it retunrs an erro

Tipo de erro:
Erro de compilaĆ§Ć£o do Microsoft VBScript (0x800A03EE)
')' esperado
/esbr/admin/pop_emails.asp, line 67, column 33
Function inArray(ByRef arrName() As String, ByVal srchTerm As String)
--------------------------------^
0
 
Bobaran98Commented:
Hmm... I didn't have a chance to test this, just going by memory.  Try replacing the function declaration with either:

Function inArray(ByRef arrName As String, ByVal srchTerm As String)
or

Function inArray(ByRef arrName As String(), ByVal srchTerm As String)

or if all else fails:

Function inArray(arrName, srchTerm)

Sorry I can't be more definitive at the moment! :-)

0
 
drix76Author Commented:
It dindt work...
0
 
sdstuberCommented:
how about...


if ubound(filter(split(replace(replace(searchstr," ","")+",",",","-,"),","),teststr+"-")) = -1 then document.write("teststr not found in searchstr")

you could wrap the above condition in your own function if you wanted as suggested above
0
 
sdstuberCommented:
if the ubound(....) returns something other than -1 then the teststr was found
0
 
drix76Author Commented:
I think I'm very newbee for your answers. I apologize.
Could you print the code for me, please?!

Thanks in advance!
0
 
sdstuberCommented:
instead of...

If Instr(conteudo,id_new) Then

do

If ubound(filter(split(replace(replace(conteudo," ","")+",",",","-,"),","),id_new+"-")) = -1 Then

make similar substitutions anyplace you are using Instr
0
 
drix76Author Commented:
I did the substitutions, but it still showing checked the number 2, while the numbers in conteudo are (24, 25, 32).
0
 
sdstuberCommented:
I'll ask the admins to move this question to a more appropriate zone since this is a VB Script question and has nothing to do with SQL syntax.

You'll get better help from people there than I'm able to give.
My filter method worked for my testing but I'm sure VB Script experts probably have better ideas than me.
0
 
drix76Author Commented:
Ok. Thank You!
0
 
Bobaran98Commented:
sdstuber was right on in his second post, and I think we should go back to that... we're getting a bit more complicated than is probably necessary, especially if drix76 is new to VB.
Try the following code.  I know it looks like there's a lot to it, but if it works properly for you, we can rewrite it as a function so that you don't have to add fifteen lines of code everytime you're looking for a value in contuedo.

Dim conteudo as string
Dim id_new as string
 
'assign conteudo values for testing:
contuedo = "24,25,32"
id_new = "2"
 
'I'm commenting out the following for testing, but
' ultimately, you'll want to add this back in:
contuedo = Replace(contuedo," ","") 'remove spaces
contuedo = Replace(contuedo,")","") 'remove close parentheses
contuedo = Replace(contuedo,"(","") 'remove open parentheses
contuedo = "-," & contuedo & ",-" 'add commas and dummy characters fore and aft
 
If instr(contuedo, "," & id_new & ",") > 0 Then
   Response.Write("ID " & id_new & " exists.")
Else
   Response.Write("ID " & id_new & " does not exist.")
End If

Open in new window

0
 
Bobaran98Commented:
Sorry, I misspelled your variable name in places.  I fixed it in the code I'm posting now. :-)
By the way, in all of the examples either of us have provided, we were working under the assumption that id_new was a string, not an integer.  If that is not the case, this could be why our solutions weren't working for you.

Dim conteudo as string
Dim id_new as string
 
'assign conteudo values for testing:
conteudo = "24,25,32"
id_new = "2"
 
'I'm commenting out the following for testing, but
' ultimately, you'll want to add this back in:
conteudo = Replace(conteudo," ","") 'remove spaces
conteudo = Replace(conteudo,")","") 'remove close parentheses
conteudo = Replace(conteudo,"(","") 'remove open parentheses
conteudo = "-," & conteudo & ",-" 'add commas and dummy characters fore and aft
 
If instr(conteudo, "," & id_new & ",") > 0 Then
   Response.Write("ID " & id_new & " exists.")
Else
   Response.Write("ID " & id_new & " does not exist.")
End If

Open in new window

0
 
drix76Author Commented:
It worked!!! Thank you very very much!
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 7
  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now