|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
variable in variable?
Hello ive written this function:
Function Legenda(CampagneID) SQL1 = "SELECT Model,modelid,campaignid,Campagne,[totale kosten],[opbrengst uit verkopen] FROM tbwanissan.ocd_campaigns_cost WHERE modelid=" & request("ModelID") & " AND campaignid=2 ORDER BY [" & TotaalType & "] DESC" Set RS1 = DBC.Execute(SQL1) If RS1.EOF <> True Then CampagneNaam1 = RS1("Campagne") Response.Write("<td class='tdtotaal'><img src='1.gif' width='10' height='10'></td><td class='standard'> " & CampagneNaam1 & " </td>") RS1.MoveNext End If SQL2 = "SELECT Model,modelid,campaignid,Campagne,[totale kosten],[opbrengst uit verkopen] FROM tbwanissan.ocd_campaigns_cost WHERE modelid=" & request("ModelID") & " AND campaignid=12 ORDER BY [" & TotaalType & "] DESC" Set RS2 = DBC.Execute(SQL2) If RS2.EOF <> True Then CampagneNaam2 = RS2("Campagne") Response.Write("<td class='tdtotaal'><img src='2.gif' width='10' height='10'></td><td class='standard'> " & CampagneNaam2 & " </td>") RS2.MoveNext End If End Function As you can probably see there is the same piece of code displayed twice, but then with instead of 1 everywhere a 2. I want this 1 and 2 (from for instance SQL1) to be read out of the call legenda(1) So if i say call legenda(1) call legenda(2) the SQL1 var changes from SQL1 to SQL2.. Ive tried: SQL(campagneID) .. and since campagneID = 1 it should work right? But i get this error: Subscript out of range: SQL Please help! |
|
#2
|
|||
|
|||
|
Ok...I think the first thing you need to understand is the difference between a Function and a Sub/Procedure.
A Function returns things A Sub/Procedure doesn't. Now, don't get me wrong, you can make a Function not return anything and vice versa but people get confused and mix them up. For example: Function Calculate(ByVal lngNumber1, ByVal lngNumber2) Calculate = Clng(lngNumber1) + Clng(lngNumber2) End Function 'Notice that the function Calculate adds the value of *lngNumber1* and *lngNumber2*, then the result is returned. Notice the line Calulate = ... that's how it's being returned. A Sub...End Sub on the other hand doesn't return something so if I take the same example as above then I could do it like this: Sub Calculate(ByVal lngNumber1, ByVal lngNumber2) Response.Write Clng(lngNumber1) + Clng(lngNumber2) End Sub Notice the difference between the two? The Sub makes a Response.Write so whenever you invoke that Sub...End Sub you'll get a Response.Write But if you invoke the Function...End Function you won't since there isn't any Response.Write's in it. In fact, when caling the Function...End Function the result should be put inside a variable for later use for example... Now I look at your Code(your function) and I don't see it return anything. Not only that, you are not showing the entire code. For example where do you declare variable SQL1 and SQL2 also where/when do you declare your connection object DBC And on top of that, you're not even Closing and Freeing your objects from memory using the RS1.Close and Set RS1 = nothing Then inside your SQL queries, you have, in the ORDER BY clause this instruction: [" & TotaalType & "] Now where does this variable comes from??? and all the others for that matter? If I understand this correctly, you want your Function to execute either SQL1 **OR** SQL2 depending on what parameter value is? Is that correct ? If so, why not simply make a Select Case...End Select isinde your Function...End Function? And remember, Functions should be used to return things NOT to write things, otherwise use a Sub...End Sub Hope this helps! Sincerely Vlince |
|
#3
|
|||
|
|||
|
hya vince! Thanks somuch for your time.
The Code i posted was indeed not the entire code, i merely posted the function, its working fine, as long as i: --------------------------------------------------- post the code for -selecting the table and some fields -putting a db field1 in a var1 and -print1 continued by -selecting the db and some fields -putting a db field2 in a var2 and -print2. --------------------------------------------------- Get that? I know you do. The thing i want is the following: I want to create a function, which, later in the document i call with a certain value in it, and that certain value, 1 or 2, should fill not only the value of some vars, that i can do, but also the name of the var itself! Say i've got a function: Function DoSomeThing(value) varSQL(value) = "some nasty *** sql query" varRS(value) = "you know" varIMAGE(value) = "eek!" End Function I want to do this so that if i call the function: call DoSomeThing(1) It kind of makes the vars in the function like this: Function DoSomeThing(value) varSQL1 = "some nasty *** sql query" varRS1 = "you know" varIMAGE1 = "eek!" End Function Just as: call DoSomeThing(2) should make the vars in the function like this: Function DoSomeThing(value) varSQL2 = "some nasty *** sql query" varRS2 = "you know" varIMAGE2 = "eek!" End Function Catch my drift? And dont know if im supposed to do such a thing with varSQL(value) , even worse i know it cant, cause it isnt working, thats what i need you for ;D And thanks for pointing me on the mistakes i made.. Can you help? Last edited by wijbenga : November 6th, 2003 at 08:39 AM. |
|
#4
|
|||
|
|||
|
---BEGIN QUOTE---
Can you help? ---END QUOTE--- Well I can try to help :-) Ok I think I have an idea of what your trying to do...but I still have a question. You have a Function...End Function with *one* parameter Depending on the value of that *one* parameter, you want to fill some variables right? Do you want to fill them for later use ? What I mean is that, assuming you pass the value of 1 for the paramater, do you want to put the result of your query into a variable **OR** do you want to put the SQL Query itself in the variable so that later in your code, you'll execute that variable? I'm attaching an asp page, download it and look at the two examples... Hope this helps! Sincerely Vlince |
|
#5
|
|||
|
|||
|
i can see what you mean totally, ive worked with cases before, in php, but what im trying to do is do 2 things with 1 piece of code...understand?
i only want to do this so the code is smaller, and better workable. Is it possible? EG i dont want to write 2 queries.... |
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > ASP Programming > variable in variable? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|