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!

Getting A value out of a db 2

Status
Not open for further replies.

mbutler55

Instructor
Feb 5, 2003
33
US
This has to be THE dumbest question ever asked, but I feel like I have tried everything and nothing seems to work! I have a list of numbers, sorted in ascending order in a table called Median - (yes I am trying to find the median value)

My code to peruse the list of numbers until finding the middle number is:
' First I find the total number of records. Is there an
easier way?
iNumRec = 1
Do while NOT objRS.EOF
iNumRec = iNumRec + 1
objRS.MoveNext
Loop
' Now to peruse to the middle record
for iCounter = 1 to iNumRec \ 2
next
' I am now at the middle record what comes next?
response.write _________
 
I'm not sure about when the End If got seperated, but it is fixed now. So here is what I have:

Dim tempAge, iNumRec, Median, intMedian, iCounter
Set objRS = Server.CreateObject ("ADODB.Recordset")
sql="SELECT PTAGE FROM Median"
objRS.Open sql, objConn, 3

Response.Write ("Age Median: ")
objRS.MoveFirst
iCounter = 0
intMedian = CInt(objRS.RecordCount / 2)
Do While NOT objRS.EOF
  iCounter = iCounter + 1
  If iCounter = intMedian Then
   Response.Write objRS("PTAGE")
   objRS.MoveLast
  End If
   objRS.MoveNext
Wend

And yes, iCounter = iCounter + 1 still gives the error. I'm not sure about declaring variables, but could iCounter be a non-Integer?
 
OKay - I took out EVERYTHING except what pertains to this code and the page works fine. I then copied this to another page. Just plain and dirty - nothing perty. Here it is:

<%@ Language=VBScript %>
<%
Option Explicit
Dim objConn
Set objConn = Server.CreateObject(&quot;ADODB.Connection&quot;)
objConn.ConnectionString = &quot;DRIVER={Microsoft Access Driver (*.mdb)};&quot; & &quot;DBQ=&quot; & Server.MapPath(&quot;fpdb/CryoCathIPR.mdb&quot;) & &quot;;&quot;
objConn.Open
Dim tempAge, iNumRec, Median, intMedian
Set objRS = Server.CreateObject (&quot;ADODB.Recordset&quot;)
sql=&quot;SELECT PTAGE FROM Median&quot;
objRS.Open sql, objConn, 3

Response.Write (&quot;Age Median: &quot;)
objRS.MoveFirst
Dim iCounter
iCounter = 0
intMedian = CInt(objRS.RecordCount / 2)
While NOT objRS.EOF
   iCounter = iCounter + 1
  If iCounter = intMedian Then
   Response.Write objRS(&quot;PTAGE&quot;)
    objRS.MoveLast
  End If
   objRS.MoveNext
Wend
objConn.close
%>

I still get iCounter = iCounter + 1 has an illegal character.
 
Your Do While needs a Loop at the end instead of a Wend.

Also try placing the recordCount value into a variable directly after opening the recordset and then use the variables value from then on.

Ive got to go now - hope the other guys can help you wrap this up - good luck
Tony
reddot.gif WIDTH=500 HEIGHT=2 VSPACE=3

 
mbutler try the suggestion in my last post and let me know if it works ;-)
 
I'm so sorry - I did try that and no it did not work. Please don't stop on me now.
 
Now it is even spookier - all I have is

<%@ Language=VBScript %>
<%
Option Explicit
Dim iNumRec
iNumRec = 0
   iNumRec = iNumRec + 1
response.write (iNumRec)
%>

and I still get that there is an illegal character. I am going to repost just this in another thread in hopes to open it up and get some new eyes that can see what is happening.
 
Well if this doesn't work, I'm flumoxed;

Dim iCounterVAR
iCounterVAR = 0
intMedian = CInt(objRS.RecordCount / 2)

While (NOT objRS.EOF)
iCounterVAR = iCounterVAR + 1
If iCounterVAR = intMedian Then
Response.Write(objRS(&quot;PTAGE&quot;))
objRS.MoveLast
Else
objRS.MoveNext
End If
Wend

Make sure intMedian is returning what you expect.

I'm afraid I have to leave work now, I might check in at the weekend to see how your doing ;-)

Happy Friday, BDC
 
P.S. Post the error message and code it might help someone find out whats going on.

BDC.
 
Okay guys (& gals) I have no clue what happened, but I went back and added 1 line at a time and it know works. Here is the final code for any of you who care. This computes the median of a list of numbers.
<%@ Language=VBScript %>
<% Option Explicit %>
<!--#include virtual=&quot;CyroCathIPR/adovbs.inc&quot;-->
<%

Dim objConn, objRS, sql
Set objConn = Server.CreateObject(&quot;ADODB.Connection&quot;)
objConn.ConnectionString = &quot;DRIVER={Microsoft Access Driver (*.mdb)};&quot; & &quot;DBQ=&quot; & Server.MapPath(&quot;fpdb/CryoCathIPR.mdb&quot;) & &quot;;&quot;
objConn.Open
Set objRS = Server.CreateObject (&quot;ADODB.Recordset&quot;)
sql=&quot;SELECT PTAGE FROM Median&quot;
objRS.CursorLocation = adUseClient
objRS.Open sql, objConn, 3
objRS.Sort = &quot;PTAGE DESC&quot;
Dim intMedian
intMedian = CInt(objRS.RecordCount)

intMedian = CInt(intMedian \ 2)
Response.Write(&quot;Median Age: &quot;)
objRS.MoveFirst
Dim iNumRec, Median
If intMedian <> objRS.RecordCount/2 then
' Number of Records is odd
For iNumRec = 1 to intMedian
Median = objRS(&quot;PTAGE&quot;)
objRS.MoveNext
Next
Median = objRS(&quot;PTAGE&quot;)
response.write (Median)
Else
' Number of Records is even
For iNumRec = 1 to intMedian
Median = objRS(&quot;PTAGE&quot;)
objRS.MoveNext
Next
Median = (Median + objRS(&quot;PTAGE&quot;)) / 2
response.write (Median)
End If
objConn.close
%>
The good news is I know what this does and can actually help others. I still don't know why x = x + 1 has an illegal character.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top