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

Add Number to a Variable Name 2

Status
Not open for further replies.

DebbieC

Programmer
Mar 29, 2001
168
US
I have a form that I'm having a problem with. I need to return the values that are in the database and have blank textboxes for those fields that don't have any values for adding more later (with a total of 12 textboxes). The only way I can think of to do this is to have the variable name change each time it goes through the loop. However I can't figure out how to get the variable to change from sSequenceNum1 to sSequenceNum2. I'm trying (i) but it doesn't work. I also tried "sSequenceNum & i" but it didn't work either. I probably have to use an array but I don't know how to do that.

Here is what I have:


Code:
<%  For i = 1 to 12
			
	If not objRs2.EOF Then 	
					
		sSequenceNum(i) = objRs("SequenceNum") 
		sDuplicate(i) = objRs("Duplicate")	

	objRs2.moveNext
					
	Else	

		sSequenceNum(i) = objRs("SequenceNum") 
		sDuplicate(i) = objRs("Duplicate")								
	End If
					
%>	
			
<tr>
<td>
<input type="text" name="sSequenceNum1" value="<%= SequenceNum1 %>">
<input type="text" name="sDuplicate1" value="<%= sDuplicate1 %>">
</td>
<td>
<input type="text" name="sSequenceNum2" value="<%= sSequenceNum2 %>">
<input type="text" name="sDuplicate2" value="<%= sDuplicate2 %>">
</td>
<td>							
<input type="text" name="sSequenceNum3" value="<%= sSequenceNum3 %>">
<input type="text" name="sDuplicate3" value="<%= sDuplicate3 %>">
</td>
</tr>	                    
<tr>
<td>
<input type="text" name="sSequenceNum4" value="<%= sSequenceNum4 %>">
<input type="text" name="sDuplicate4" value="<%= sDuplicate4 %>">
</td>
<td>							
<input type="text" name="sSequenceNum5" value="<%= sSequenceNum5 %>">
<input type="text" name="sDuplicate5" value="<%= sDuplicate5 %>">
</td>
<td>						
<input type="text" name="sSequenceNum5" value="<%= sSequenceNum6 %>">
<input type="text" name="sDuplicate5" value="<%= sDuplicate6 %>">
</td>
</tr>	
<tr>
<td>                   
<input type="text" name="sSequenceNum7" value="<%= sSequenceNum7 %>">
<input type="text" name="sDuplicate7" value="<%= sDuplicate7 %>">
</td>
<td>						
<input type="text" name="sSequenceNum8" value="<%= sSequenceNum8 %>">
<input type="text" name="sDuplicate8" value="<%= sDuplicate8 %>">
</td>
<td>							
<input type="text" name="sSequenceNum9" value="<%= sSequenceNum9 %>">
<input type="text" name="sDuplicate9" value="<%= sDuplicate9 %>">
</td>
</tr>	
<tr>
<td>						
<input type="text" name="sSequenceNum10" value="<%= sSequenceNum10 %>">
<input type="text" name="sDuplicate10" value="<%= sDuplicate10 %>">
</td>
<td>						
<input type="text" name="sSequenceNum11" value="<%= sSequenceNum11 %>">
<input type="text" name="sDuplicate11" value="<%= sDuplicate11" %>">
</td>
<td>						
<input type="text" name="sSequenceNum12" value="<%= sSequenceNum12 %>">
<input type="text" name="sDuplicate12" value="<%= sDuplicate12 %>">
</td>
</tr>
			
<%
Next
%>

Any suggestions?
 
Something like this maybe:
Code:
<%  

For i = 1 to 12 step 3
            
    If not oRS.EOF and oRS.BOF Then     
                    
response.write("<tr>")

for j = 0 to 2
  response.write("<td><input type=""text"" name=""sSequenceNum" & (i + j) & """ value=""" & oRS.Fields("SequenceNum") & """>")
  response.write("<input type=""text"" name=""sDuplicate" & (i+j) &  """ value=""" & oRS.Fields("Duplicate") & "></td>")
next

response.write("</tr>")

    oRS.moveNext
                    
    Else    
        response.write("No Records")
    End If

%>

watch out for typo's etc...

A smile is worth a thousand kind words. So smile, it's easy! :)
 
Did you mean this:

Code:
<%  For i = 1 to 12
            
    If not objRs2.EOF Then     
                    
        sSequenceNum&i = objRs("SequenceNum") 
        sDuplicate&i = objRs("Duplicate")    

    objRs2.moveNext
                    
    Else    

        sSequenceNum&i = objRs("SequenceNum") 
        sDuplicate&i = objRs("Duplicate")                                
    End If
                    
%>    
            
<tr>
<td>
<%
for i=1 to 12
<input type="text" name="<%="sSequenceNum"&i%>" value="<%="sSequenceNum"&i %>">
<input type="text" name="<%="sDuplicate"&i%>" value="<%="sDuplicate"&i %>">
</td>
<%next%>

-DNG
 
Ooops, correction to my original code:

Code:
<%  

For i = 1 to 12 step 3
            
    If not oRS.EOF and oRS.BOF Then     
                    
response.write("<tr>")

for j = 0 to 2
  response.write("<td><input type=""text"" name=""sSequenceNum" & (i + j) & """ value=""" & oRS.Fields("SequenceNum") & """>")
  response.write("<input type=""text"" name=""sDuplicate" & (i+j) &  """ value=""" & oRS.Fields("Duplicate") & "></td>")
    [COLOR=red]oRS.moveNext[/color]
next

response.write("</tr>")
                    
    Else    
        response.write("No Records")
    End If

%>

DNG; I think that Debbie wanted the value from the recordset in the 'value' field, rather than the unique name. She also has 3 TD's per row, hence my nested looping.

Also, I don't think this would work:
Code:
sSequenceNum[COLOR=red]&i[/color] = objRs("SequenceNum")

To generate a dynamic variable name you would probably do something along the lines of:
Code:
sExec = "sSequenceNum" & i & " = objRs(""SequenceNum"") "
execute(sExec)

or you could use an array..


A smile is worth a thousand kind words. So smile, it's easy! :)
 
oops..my bad thanks for that correction but i thought she wanted all here values from the database to first stopre in 12 different numbered variables and then assign them as values to the textboxes on her form...

did i get that straight...

-DNG
 
some typos there... :)

......[red]her[/red] values from the database...[red]store[/red]...

-DNG
 
:)

I think the end aim is to take the values (SequenceNum & Duplicate) from the database and put them into the value attribute of each input element, and for each input to then have a unique name so that it can be distinguished on the results page... Not sure how these would then be related back to the original recordset (or if at all) as the ID's would not correlate with anything unique from the DB, but as a strict answer to her question - I think that should do it.

I actually just noticed that there may not be 12 records in the database and the remainder should be shown as empty boxes... so...

Code:
<%  
dim sSequenceNum, sDuplicate
For i = 1 to 12 step 3
            
    If not oRS.EOF and oRS.BOF Then     
                    
response.write("<tr>")
 
for j = 0 to 2
  [COLOR=red]if oRS.EOF then
     sSequenceNum = oRS.Fields("SequenceNum")
     sDuplicate = oRS.Fields("Duplicate")
  else
     sSequenceNum = ""
     sDuplicate = ""
  end if[/color]
  response.write("<td><input type=""text"" name=""sSequenceNum" & (i + j) & """ value=""" & [COLOR=red]sSequenceNum[/color] & """>")
  response.write("<input type=""text"" name=""sDuplicate" & (i+j) &  """ value=""" & [COLOR=red]sDuplicate[/color] & "></td>")
    oRS.moveNext
next

response.write("</tr>")
                    
    Else    
        response.write("No Records")
    End If

%>

(The code would look very similar if you used an array - you could use GetRows to return an array, and then use that in replacement of the oRS)

I'm still not sure if this is the best way for you - did you want to update the existing records into the database, and add the new ones ? Or just add the new ones ? The above will be ok for just adding new records, but will make it difficult to update existing ones.

If you need to update aswell, consider the following naming structure:

sSequenceNum-N-123 (Where N means New and 123 is a sequential number from your ASP page)
sSequenceNum-U-359 (Where U means Update and 359 is the Unique ID of the record in the database to update)

You can then use this to identify what to do on the results page. Alternatively you can add the N/U and the id to hidden fields with the same naming structure as the original post (sSequenceNum1 etc)



A smile is worth a thousand kind words. So smile, it's easy! :)
 
Thank you so much for all of your responses. I will give them a try and see how they work.


 
DotNetGnat is right. I want all of the values from the database to first store in 12 different numbered variables and then assign them as values to the textboxes on the form. And damber is also right. I want to have empty boxes if there aren't 12 in the recordset.

Yes, damber, I do want to also update the existing records as well as add the new ones.

I have the same thing working elsewhere in my code but the only difference is, the other recordset returns in two rows so I can just loop through and create the rows for those that exist in the database and create blank textboxes in the rows for those that don't but since these have three recordsets in each row, it's not as easy. In that case I did use a naming structure similar to what damber mentions. I hope that makes sense.

I will sort through everyone's suggestions and see what works best for what I am doing.




 
DotNetGnat is right. I want all of the values from the database to first store in 12 different numbered variables and then assign them as values to the textboxes on the form.

You can dynamically create the HTML with the values and the unique field names, making it much more efficient, rather than having the value stored in a dynamic variable, then put the value from the variable into the html. Or do you use them for something else? With the code I provided, you do not need to create the variables, and the output will be the same - just more efficient.

(You'll also notice from the below piece of code that you can define how many rows and columns you want to display for inputs.. as long as the target page dynamically iterates through the form variables, you can set this input table to whatever dimensions you wish..)

My last post will do what you requested with the exception of adding the unique id reference from the recordset. Without that unique id per record, you will not be able to reliably or efficiently update the existing record.

Try this: (I've updated it to reflect all of your requirements so far)

Code:
<%  
dim sSequenceNum, sDuplicate
dim iRows: iRows = 4
dim iCols: iCols = 3

  
For i = 1 to (iCols * iRows) step iCols
  response.write("<tr>")
  for j = 0 to (iCols-1)
    if not(oRS.EOF) then
      sSequenceNum = oRS.Fields("SequenceNum")
      sDuplicate = oRS.Fields("Duplicate")
      [COLOR=red]sID = oRS.Fields("YourUniqueIDField")[/color]
      oRS.moveNext
    else
      sSequenceNum = ""
      sDuplicate = ""
      [COLOR=red]sID = ""[/color]
   end if
   [COLOR=red]response.write("<td><input type=""hidden"" name=""sID" & (i + j) & """ value=""" & sID & """>")[/color]
   response.write("<td><input type=""text"" name=""sSequenceNum" & (i + j) & """ value=""" & sSequenceNum & """>")
   response.write("<input type=""text"" name=""sDuplicate" & (i+j) &  """ value=""" & sDuplicate & "></td>")
 next
 response.write("</tr>")               
next

%>

Then in your target page, cycle through the form fields, checking to see if the sID value is populated - if it is use that id in an update sql statement, otherwise do an insert with the corresponding fields. Watch out for the potential security issues here though, as always you should never trust data coming from outside of the server - especially user generated data like forms.

Hope that helps.

A smile is worth a thousand kind words. So smile, it's easy! :)
 
Thank you so much damber. I was trying to adapt your other code to what I needed and couldn't get it working and was just about to ask for more help. I will give this a try and let you know.

 

No problem, glad it worked out for you.

A smile is worth a thousand kind words. So smile, it's easy! :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top