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

Populating a Select Box

Status
Not open for further replies.

JackD4ME

Programmer
Jun 4, 2002
228
US
I have tried using an array and it keeps erroring out as Name Defined. So i simply changed all the array items to variables (z1,z2 [...6]). i have been able to loop through a while statement using a counter and variable (z & cntr) but for some reason this time it keeps populating with the counter. i have tried MANY different syntaxes to no avail. any ideas?

<%cntr=0
z0="Variable0"
z1="Variable1"
z2="Variable2"
z3="Variable3"
z4="Variable4"
z5="Variable5"%></td>

<td><select name=ap class=insl>
<option value="">Please Select</option>
<%While cntr<>6
If tRst("Code")<>z&cntr Then%>
<option value="<%=z&cntr%>"><%=z&cntr%></option>
<%Else%>
<option value="<%=z & cntr%>" selected><%=z & cntr%></option>
<%End If
cntr=cntr+1
Wend%>
</select>
 
Its sometimes mysterious what VBScript will do but in the broader scheme of things I would think that z is the name of a variable. I dont see any value assigned to it in the code that you posted. One might wish therefore that VBScript would ignore it and move on. Perhaps to cntr which has values.

I think you may mean to use a string, "z", instead. And that you intend to build the name of a variable such as z2. I dont know whether that is possible in VBScript. In Javascript you can do that trick with eval("z" + cntr).

Something that might work for you is
Code:
Dim z(6)
z(0) = "variable0"
...
<option value="<%= z(cntr) %>"><%= z(cntr) %></option>


One further suggestion. Use Option Explicit. This will check whether you have used a variable but not declared it and give an error. This is handy when you mispell the name of a variable.
Code:
<% Option Explicit %>
<html>
...
This must be the first line of code in your script.


Oh yes. When using loops, it is not a good practice to depend upon an exact value occurring to terminate the loop. A whole lot of things are not equal to 6. Maybe 6 will never happen. Best to write less than 6. Because many things are more than 6, so the loop will probably stop. Which is a good thing.
 
you are correct on a number of issues. only i have tried using an array and it always gives me "Name Defined" as an error. so i figured i would simply try using them as variables since there are only 6. i am not sure about the letter z being a reserved word, i have tried x1, x2... and xRst1, xRst2....I think I need to look at all my other code because I have done this before and you may be right about using it in JS.
 
I did not mean that z is a reserved word. I meant that you have used it as a variable. In other words, any letter in that place in an ASP program would be the name of a variable.

Did you declare the array as I show in my post?

 
yes i have. i have also tried x(n) and yP(n). the error returns name redefined Dim yP(6). Is there any reason why I cannot declair an array? It seems that no matter where I place the array or what I name it I get this error.


Jack
 
Either way, like this.
[tt]
<%
dim cntr
cntr=0

dim z0,z1,z2,z3,z4,z5
z0="Variable0"
z1="Variable1"
z2="Variable2"
z3="Variable3"
z4="Variable4"
z5="Variable5"

dim z
z=Array("Variable0", "Variable1", "Variable2", "Variable3", "Variable4", "Variable5")

dim zz(5)
zz(0)="Variable0"
zz(1)="Variable1"
zz(2)="Variable2"
zz(3)="Variable3"
zz(4)="Variable4"
zz(5)="Variable5"
%>
<select name="ap" class="insl">
<option value="">Please Select</option>
<%While cntr<6
If tRst("Code")<>eval("z"&cntr) Then%>
<option value="<%=eval("z" & cntr)%>"><%=eval("z" & cntr)%></option>
<%Else%>
<option value="<%=eval("z" & cntr)%>" selected><%=eval("z" & cntr)%></option>
<%End If
cntr=cntr+1
Wend%>
</select>
<br />
<% cntr=0 %>
<select name="ap2" class="insl">
<option value="">Please Select</option>
<%While cntr<6
If tRst("Code")<>z(cntr) Then%>
<option value="<%=z(cntr)%>"><%=z(cntr)%></option>
<%Else%>
<option value="<%=z(cntr)%>>" selected><%=z(cntr)%></option>
<%End If
cntr=cntr+1
Wend%>
</select>
<br />
<% cntr=0 %>
<select name="ap3" class="insl">
<option value="">Please Select</option>
<%While cntr<6
If tRst("Code")<>z(cntr) Then%>
<option value="<%=zz(cntr)%>"><%=zz(cntr)%></option>
<%Else%>
<option value="<%=zz(cntr)%>>" selected><%=zz(cntr)%></option>
<%End If
cntr=cntr+1
Wend%>
</select>
[/tt]
ps: I would like better to see attributes quoted.
 
ok, it seems that no matter what i do there is a problem with the dim statement. i always get an error (Name Redefined) on the dim statement even though i have never used it. the rest of the code works, i just cant declare an array.
 
>i just cant declare an array.
If that is not a dynamic array, open to redim, I have shown you cases how to set up the static array. Don't overlook the detail, like dim z and dim zz(5). In the case above, you cannot dim z like this dim z(5) and then use z=Array(..,..,..etc). And, you never show how you set your array up. Is it that difficult to show the forum that rather than just making some statement of grievance?
 
Amen tsuji.

One thing we know, computers dont lie. If you are getting the error message Name Redefined, then somewhere in your script you have used the variable name and that is prior to where you have declared it with the Dim statement. The job now is to find that previous use.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top