Forum Moderators: open
My problem is, the textfeld Product is ok, it is showed with the corresponding value of the column Product of the row, but the combobox either shows me the value Service to the Service_ID in the row, but the user can't select another values, or it shows me 29x the same value Service or nothing. And the textfeld Report can't be showed with the corresponding value at the row.
What happens?, how do you do in this case?, did you have the same experience, so you must fill a combobox with a second table for Updating a row? I would really appreciate, if you help me..
It sounds like this is your foreign key to services --> Service_ID. So you just need to join on the key. This will get you the service for an individual item while in *edit* mode.
select s.service, s.Service_ID
from LU_Service as s
inner join LU_Product as p on s.Service_ID = p.Service_ID
where p.Product_id = 'YOUR-prod-id-here'
You'll need to fire this above query and return the Service_ID. This sample code below assumes that the service id will be stored in this variable...
iServiceID = s.Service_ID
But you want to show all services and auto-set the one which was previously assigned. So you'll need a ComboBox Selector Function. Here's how you can accomplish this.
Function SetSelectedService(controlValue, persistedValue)
If Trim(lcase(controlValue)) = Trim(lcase(persistedValue)) Then
retVal = " value='" & controlValue & "' selected "
Else
retVal = " value='" & controlValue & "'"
End If
SetSelectedService = retVal
End Function
That function would get fired while you're unwinding your services recordset which is built from
select Service_ID, Service from LU_Service.
Set rstServices = Server.CreateObject("adodb.recordset")
With rstServices
.Open "your command text", "your connection", adOpenForwardOnly, adLockReadOnly
If Not .EOF Then
aServices = .GetRows()
Else
aServices = Null
End If
.Close
End With
Set rstServices = Nothing
If Not IsNull(aServices) Then
Response.Write "<select name='servicesCollection'>"
For y = LBound(aServices,2) To UBound(aServices,2)
Response.Write "<option " & SetSelectedService(aServices(0,y), iServiceID) & ">" & aServices(1,y) & "</option>"
Next
Response.Write "</select>"
Else
Response.Write "No Services Exist in the Services Repository"
End If
This code is meant to get you going in the right direction so assume that it's not tested. Chime back in if you need more assistance.
<%private function ShowChangeForm()
%>
<%
SQLquery="SELECT * FROM LU_Product WHERE Product_id=" & Request("Product_id")
Set objRS=Conn.execute(SQLquery)
%>
<head>
<title>Table LU_PRODUCT</title>
<link rel="stylesheet" type="text/css" href="format.css">
</head>
<body bgcolor="#ffffea">
<form action="change.asp" method="post">
<input type="hidden" id="form_action" name="form_action" value="chg_save">
<input type="hidden" id="Product_id" name="Product_id" value="<%Response.write(Request("Product_id")) %>">
<div>
<table align=center cellspacing=1 cellpadding=0 width="100%" border=0>
<tr><td height=25 class="headback"><font size=2><b>LU_PRODUCT - SAVE CHANGES</b></font></td></tr>
<tr><td height=15 class="textback" valign=middle style="font-family:Verdana, Arial, Helvetica, sans-serif; font-size:13px">Here you can make changes:</td>
</tr>
</table>
</div>
<br><br>
<div>
<table border=0 width=100% align="center" height="200">
<tr><td class="text"><b>Product:</b></td></tr>
<tr><td class="text"><input type="text" name="Product" size="40" maxlength="50" value="<%Response.write objRS("Produkt") %>"></td></tr>
<tr><td class="text" ><b>Service:</b></td></tr>
<tr><td class="text">
<select name="Service" id="Service">
<% SQLquery="select s.Service, s.Service_ID from LU_Service as inner join LU_Product as p on s.Service_ID = p.Service_ID where p.Product_id =" & Request("Product_id)
Set objRS=Conn.execute(SQLquery)
Function SetSelectedService(controlValue, persistedValue)
If Trim(lcase(controlValue)) = Trim(lcase(persistedValue)) Then
retVal = " value='" & controlValue & "' selected "
Else
retVal = " value='" & controlValue & "'"
End If
SetSelectedService = retVal
End Function
Set rsServices = Server.CreateObject("adodb.recordset")
With rsServices
.Open "your command text", "your connection", adOpenForwardOnly, adLockReadOnly
If Not .EOF Then
aServices = .GetRows()?aServices?
Else
aServices = Null
End If
.Close
End With
Set rsServices = Nothing
If Not IsNull(aServices) Then
Response.Write "<select name='servicesCollection'>"
For y = LBound(aServices,2) To UBound(aServices,2) 'What does the "2" mean?
Response.Write "<option " & SetSelectedService(aServices(0,y), iServiceID) & ">" & aServices(1,y) & "</option>"
Next
Response.Write "</select>"
Else
Response.Write "No Services Exist in the Services Repository"
End If %>
</td></tr>
...
<%End Function%>
Oh, and I'm not sure what your scope is on the conn connection but if it's open then you can use it for the rsServices Recordset too.
Set rsServices = Server.CreateObject("adodb.recordset")
With rsServices
.Open "select Service_ID, Service from LU_Service", conn, adOpenForwardOnly, adLockReadOnly, adCmdText
If Not .EOF Then
aServices = .GetRows()
Else
aServices = Null
End If
.Close
End With
Set rsServices = Nothing
Error Type:
Microsoft VBScript compilation (0x800A03EA)
Syntax error
/_rootverzeichnis/change.asp, line 259
Function SetSelectedService(controlValue, persistedValue)
Here is the code:
<select name="Service" id="Service">
<% SQLquery="select s.Service, s.Service_ID from LU_Service as s inner join LU_Product as p on s.Service_ID = p.Service_ID where p.Product_id =" & Product_id
Set rsServices=Conn.execute(SQLquery)
iServiceID = s.Service_ID
Function SetSelectedService(controlValue, persistedValue)
If Trim(lcase(controlValue)) = Trim(lcase(persistedValue)) Then
retVal = " value='" & controlValue & "' selected "
Else
retVal = " value='" & controlValue & "'"
End If
SetSelectedService = retVal
End Function
Set rsServices = Server.CreateObject("adodb.recordset")
With rsServices
.Open "select Service_ID, Service from LU_Service", Conn, adOpenForwardOnly, adLockReadOnly, adCmdText
If Not .EOF Then
aServices = .GetRows() '?aServices?
Else
aServices = Null
End If
.Close
End With
Set rsServices = Nothing
If Not IsNull(aServices) Then
Response.Write "<select name='servicesCollection'>"
For y = LBound(aServices,2) To UBound(aServices,2) 'What does the "2" mean?
Response.Write "<option " & SetSelectedService(aServices(0,y), iServiceID) & ">" & aServices(1,y) & "</option>"
Next
Response.Write "</select>"
Else
Response.Write "No Services Exist in the Services Repository"
End If
%>
Set rsServices=Conn.execute(SQLquery)
iServiceID = s.Service_ID <-- No worky, this was pseudo...
iServiceID = rsServices.Fields("Service_ID").Value <-- You want to do something like this.
<% SQLquery="select s.Service, s.Service_ID from LU_Service as s inner join LU_Product as p on s.Service_ID = p.Service_ID where p.Product_id =" & Product_id
Set rsServices=Conn.execute(SQLquery)
iServiceID = rsServices.Fields("Service_ID").Value
'What is controlValue and persistedValue?
Function SetSelectedService(controlValue, persistedValue)
If Trim(lcase(controlValue)) = Trim(lcase(persistedValue)) Then
retVal = " value='" & controlValue & "' selected "
Else
retVal = " value='" & controlValue & "'"
End If
SetSelectedService = retVal
End Function
Set rsServices = Server.CreateObject("adodb.recordset")
SQLquery="select Service_ID, Service from LU_Service"
rsServices.Open SQLquery, Conn, adOpenForwardOnly, adLockReadOnly
With rsServices
If Not rsServices.EOF Then
aServices = rsServices.GetRows() 'why this?
Else
aServices = Null
End If
rsServices.Close
End With
Set rsServices = Nothing
If Not IsNull(aServices) Then
Response.Write "<select name='servicesCollection'>"
For y = LBound(aServices,2) To UBound(aServices,2) 'What does the "2" mean?
Response.Write "<option " & SetSelectedService(aServices(0,y), iServiceID) & ">" & aServices(1,y) & "</option>"
Next
Response.Write "</select>"
Else
Response.Write "No Services Exist in the Services Repository"
End If
%>
'What is controlValue and persistedValue?
These are input parameters to the SetSelectedService routine. What you’re basically doing is unwinding the collection of services and auto-selecting the item up for edit.
So for every service in your collection you fire the SetSelectedService routine. For example, say you have these 3 services in your database and you want to edit 'Tire Rotation'.
id service
--------------
1 Oil Change
2 Fuel Injection System Cleaning
3 Tire Rotation
For every service in the collection this routine gets called and the values will actually get passed in like this:
Loop iteration 1: SetSelectedService(1, 3) <-- no match
Loop iteration 2: SetSelectedService(2, 3) <-- no match
Loop iteration 3: SetSelectedService(3, 3) <-- bingo
The third time through you get a match so the 'selected' attribute gets passed back thereby auto-setting the Tire Rotation service in the ComboBox control.
set rsService = Conn.execute("select * from LU_Service")
%>
<select id="Service" name="Service">
<% While not rsService.eof
if rsService("Service_ID") = objRS("Service_ID") then %>
<option SELECTED value="<%=rsService("Service_ID")%>"><%=rsService("Service")%>
<% else %>
<option value="<%=rsService("Service_ID")%>"><%=rsService("Service")%>
<% end if
rsService.movenext
Wend %>
</select>