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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using array elements to highlight menu options 1

Status
Not open for further replies.
Aug 1, 2003
85
US
I'm using this code from Sheco for the array.

<%
myvalues=Split(Recordset1.Fields.Item("Resolved_by").Value, ",")
For i = LBound(myvalues) TO UBound(myvalues)
Response.Write myvalues(i)
Next
%>

Using (Response.Write myvalues(i)) temporarily for debugging.

Here's the menu code.

<select name="Resolved_by" size="4" multiple="multiple" id="Resolved_by">
<%
While (NOT Recordset2.EOF)
%>
%>
<option value="<%=(Recordset2.Fields.Item("Username").Value)%>"
<%If (Not isNull(myvalues)) Then If (CStr(Recordset2.Fields.Item("Username").Value) = CStr(myvalues))
Then Response.Write("SELECTED") : Response.Write("")%>><%=(Recordset2.Fields.Item("Username").Value)%>
</option>
<%
Recordset2.MoveNext()
Wend
If (Recordset2.CursorType > 0) Then
Recordset2.MoveFirst
Else
Recordset2.Requery
End If
%>

I get the error (Type Mismatch 'CStr'), and I checked the value for recordset2 and "myvalues" which is dan.

Any Ideas?
Dan
 
Generally about the only time you will get a type mismatch on cStr is if your trying to convert an object (or complex data type like an array) with no default value or trying to convert a null value.

If your myvalues variable is the same one produced by your code at the top, then that is likely your culprit. In the top code myvalues is an array, but you don't reference a single index in the line that is throwing the error.

Let us know if the myvalues in the second chunk of code is not, in fact an array, and we can dig in a little deeper.

Additionally, if that is the case and you need some assistance reworking that statement, let us know.

-T

barcode_1.gif
 
Thanks for the response Tarwn,

The myvalues in the first part of the code is my array.
followed by Response.Write myvalues(i) which outputs the correct values.

the second part of the code is just my dynamic menu, with the IF Then statement If (Not isNull(myvalues)) Then If (CStr(Recordset2.Fields.Item("Username").Value) = CStr(myvalues)) using [myvalues] as the variable.

I've tried several different ways to fix the code but I'm missing something important.

Thanks,
Dan
 
So you have created another variable called myvalues that is not an array for the second part?

Quick way to see which opne is failing, replace your loop with:
Code:
While (NOT Recordset2.EOF)
   Response.Write "Username = "
   Response.Flush
   Response.Write CStr(Recordset2.Fields.Item("Username").Value)
   Response.Flush
   Response.Write "myvalues = "
   Response.Flush
   Response.Write CStr(myvalues)
   Response.Flush

   Recordset2.MoveNext()
Wend

That should tell you exactly which one of the values is failing and which loop through the recordset it is failing on.


Also, I would advise you not to try and cram everything on one line like this:
Code:
<%If (Not isNull(myvalues)) Then If (CStr(Recordset2.Fields.Item("Username").Value) = CStr(myvalues))
Then Response.Write("SELECTED") : Response.Write("")%>
Your code won't execute any faster and all your doing is making it harder to read and, thus, harder to maintain.

Also, you have an error here:
Code:
<select name="Resolved_by" size="4" multiple="multiple" id="Resolved_by">
<%
While (NOT Recordset2.EOF)
[highlight]%>
%>[/highlight]

-T

barcode_1.gif
 
I ran the code and got Username = dan myvalues = Type mismatch: 'CStr'... a little groggy just got back from the dentist.

Since myvalues is the array name I thought I had to call it in my if then statment. HOw do I get the values from the array to equal CStr(Recordset2.Fields.Item("Username").Value)

Here is that part of the code

<td width="133" valign="top" class="adstat2"><select name="Resolved_by" size="4" multiple="multiple" id="Resolved_by">
<%
myvalues=Split(Recordset1.Fields.Item("Resolved_by").Value, ",")
For i = 0 TO UBound(myvalues)
Next
%>
<%
While (NOT Recordset2.EOF)
%>
<option value="<%=(Recordset2.Fields.Item("Username").Value)%>"
<%If (Not isNull((Recordset1.Fields.Item("Resolved_by").Value))) Then If (CStr(Recordset2.Fields.Item("Username").Value) = CStr(myvalues)) Then Response.Write("SELECTED")
: Response.Write("")%> ><%=(Recordset2.Fields.Item("Username").Value)%></option>
<%
Recordset2.MoveNext()
Wend
If (Recordset2.CursorType > 0) Then
Recordset2.MoveFirst
Else
Recordset2.Requery
End If
%>
</select>
 
This is what I have now. If I select one name form the dropdownlist and update then that name is highlighted, but multiple names doesn't work still.

<td width="133" valign="top" class="adstat2"><select name="Resolved_by" size="4" multiple="multiple" id="Resolved_by">
<%
Dim MyArray
MyArray = Split(Recordset1.Fields.Item("Resolved_by").Value, ",")
For i=0 to UBound(MyArray)
mystring = myArray(i)
Next
%>
<%
While (NOT Recordset2.EOF)
%>
<option value="<%=(Recordset2.Fields.Item("Username").Value)%>"
<%If (Not isNull((Recordset1.Fields.Item("Resolved_by").Value)))
Then If (CStr(Recordset2.Fields.Item("Username").Value) = mystring)
Then Response.Write("SELECTED") : Response.Write("")%>>
<%=(Recordset2.Fields.Item("Username").Value)%></option>
<%
Recordset2.MoveNext()
Wend
If (Recordset2.CursorType > 0) Then
Recordset2.MoveFirst
Else
Recordset2.Requery
End If
%>
</select>

Any Ideas?
Dan
 
Code:
mystring = myArray[highlight](i)[/highlight]

That was why I was asking whether that variable was still an array in your lower code. You can't CStr an array, but you can CStr an element of the array.

Now if you want to check if one value is in the array (instead of just one element in the array) you could use the Join() function to chunk your entire array into a string, then use InStr() to determine if a value from your recordset is inside that string, something like:
Code:
mystring = "###" & Join(myvalues,"###") & "###"
...
...
If InStr(mystring,"###" & cStr(Recordset2("Username")) & "###") > 0
...

I added the #'s so that you wouldn't get a submatch. For instance if one of the values in your array was "blahblahblah" then you wouldn't want to get a match on "blahblah".

Oh, and your loop here (which isn't needed anymore):
Code:
For i=0 to UBound(MyArray)
mystring = myArray(i)
Next
mystring is always going to be the value of the last entry in the array.

-T

barcode_1.gif
 
Is this what you mean?

<td width="133" valign="top" class="adstat2"><select name="Resolved_by" size="4" multiple="multiple" id="Resolved_by">
<%
Dim MyArray
MyArray = Split(Recordset1("Resolved_by"), ",")
mystring = "###" & Join(MyArray,"###") & "###"
%>
<%
While (NOT Recordset2.EOF)
%>
<option value="<%=(Recordset2("Username"))%>"
<%If (Not isNull((Recordset1("Resolved_by"))))
Then If InStr(mystring,"###" & cStr(Recordset2("Username")) & "###") > 0
Then Response.Write("SELECTED") : Response.Write("")%>>
<%=(Recordset2("Username"))%></option>
<%
Recordset2.MoveNext()
Wend
If (Recordset2.CursorType > 0) Then
Recordset2.MoveFirst
Else
Recordset2.Requery
End If
%>
</select>

I ran it and only get the first name highlighted still.
Dan
 
Hmm, try Response.Write'ing your data to the screen and compare it. I don't see any errors jumping out at me.

-T

barcode_1.gif
 
this is what I get from the array

###dan### jeff### Justin###

Is it the spaces?

Dan
 
It was the spaces. I changed the split code from
MyArray = Split(Recordset1("Resolved_by"), ",")
to this
MyArray = Split(Recordset1("Resolved_by"), ", ")
now the output is ###dan###jeff###Justin###
and the highlights are working great
Thanks Tarwn

 
I have another issue with this array.
Let me know if I should start a new thread.

I'm using dreamweavers recordset paging on the page.
When the page first loads up the script for the array and the highlight works great. But if you try using the next link then the menu is empty.(the "first" link brings it back to normal)

I put an array with a response.write back in to debug. When I hit the next link I got the error

Microsoft VBScript runtime error '800a005e'

Invalid use of Null: 'Split'

/intranet/test/tsrformadmin1.asp, line 697

<<<line 697 = Dim MyArray2>>>

<%
Dim MyArray2
MyArray2 = Split(Recordset1("Resolved_by"), ", ")
mystring2 = "###" & Join(MyArray2,"###") & "###"
response.write mystring2
%>

and here is the paging code


<%
' *** Move To Record: set the strings for the first, last, next, and previous links

Dim MM_keepMove
Dim MM_moveParam
Dim MM_moveFirst
Dim MM_moveLast
Dim MM_moveNext
Dim MM_movePrev

Dim MM_urlStr
Dim MM_paramList
Dim MM_paramIndex
Dim MM_nextParam

MM_keepMove = MM_keepBoth
MM_moveParam = "index"

' if the page has a repeated region, remove 'offset' from the maintained parameters
If (MM_size > 1) Then
MM_moveParam = "offset"
If (MM_keepMove <> "") Then
MM_paramList = Split(MM_keepMove, "&")
MM_keepMove = ""
For MM_paramIndex = 0 To UBound(MM_paramList)
MM_nextParam = Left(MM_paramList(MM_paramIndex), InStr(MM_paramList(MM_paramIndex),"=") - 1)
If (StrComp(MM_nextParam,MM_moveParam,1) <> 0) Then
MM_keepMove = MM_keepMove & "&" & MM_paramList(MM_paramIndex)
End If
Next
If (MM_keepMove <> "") Then
MM_keepMove = Right(MM_keepMove, Len(MM_keepMove) - 1)
End If
End If
End If

' set the strings for the move to links
If (MM_keepMove <> "") Then
MM_keepMove = Server.HTMLEncode(MM_keepMove) & "&"
End If

MM_urlStr = Request.ServerVariables("URL") & "?" & MM_keepMove & MM_moveParam & "="

MM_moveFirst = MM_urlStr & "0"
MM_moveLast = MM_urlStr & "-1"
MM_moveNext = MM_urlStr & CStr(MM_offset + MM_size)
If (MM_offset - MM_size < 0) Then
MM_movePrev = MM_urlStr & "0"
Else
MM_movePrev = MM_urlStr & CStr(MM_offset - MM_size)
End If
%>

Thanks,
Dan
 
You should probably start a new thread on that :)

barcode_1.gif
 
Quick Question Dan,

You are writing the "SELECTED" value for the <OPTION> based on values matching. When I do this in my form, it doesnt select the right one, it always selects the first item?

Thanks
Travis
 
I found that when the higlights don't work it's generally because the strings don't match. You should start a new thread, and I'll go look for that page to copy the code for you.

Dan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top