Link to home
Start Free TrialLog in
Avatar of drix76
drix76

asked on

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

Avatar of Sean Stuber
Sean Stuber

append a comma to both and do the instr

instr(var1+',' , var2+',')
Avatar of drix76

ASKER

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!!!
same idea, put a comma in front of it

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

this assumes there is no whitespace though.
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

Avatar of drix76

ASKER

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)
--------------------------------^
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! :-)

Avatar of drix76

ASKER

It dindt work...
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
if the ubound(....) returns something other than -1 then the teststr was found
Avatar of drix76

ASKER

I think I'm very newbee for your answers. I apologize.
Could you print the code for me, please?!

Thanks in advance!
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
Avatar of drix76

ASKER

I did the substitutions, but it still showing checked the number 2, while the numbers in conteudo are (24, 25, 32).
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.
Avatar of drix76

ASKER

Ok. Thank You!
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

ASKER CERTIFIED SOLUTION
Avatar of Bobaran98
Bobaran98
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of drix76

ASKER

It worked!!! Thank you very very much!