Solved

OLEDB command parameters not being assigned in order in SQL string

Posted on 2006-07-01
4
223 Views
Last Modified: 2011-04-14
The parameters for this OLEDB command are not being placed in the order in which they appear in the SQL string, probably due to nesting.  How can I determine the order in which they are being applied?

        strSql = "SELECT Subjects.SubjectID, Subjects.BusinessName, Subjects.City, Subjects.StateCode FROM (Subjects) WHERE (((Subjects.Zip) In (SELECT Zip FROM ZipCodes WHERE ? > 3959 * ATN( SQR(1 - (SIN(?/ 57.3) * SIN(LAT/ 57.3) + COS(?/ 57.3) * COS(LAT/ 57.3) * COS((LNG/ 57.3) - (?/ 57.3))) ^2 ) / (SIN(?/ 57.3) * SIN(LAT/ 57.3) + COS(?/ 57.3) * COS(LAT/ 57.3) * COS((LNG/ 57.3) - (?/ 57.3))) ) )))"
        objCmd = New OleDbCommand(strSql, objConn)
        setParam(objCmd, "Rad", OleDbType.Numeric, vRad)
        setParam(objCmd, "Lat", OleDbType.Numeric, vLat)
        setParam(objCmd, "Lat", OleDbType.Numeric, vLat)
        setParam(objCmd, "Lng", OleDbType.Numeric, vLng)
        setParam(objCmd, "Lat", OleDbType.Numeric, vLat)
        setParam(objCmd, "Lat", OleDbType.Numeric, vLat)
        setParam(objCmd, "Lng", OleDbType.Numeric, vLng)

    Protected Sub setParam(ByRef objCmd As OleDbCommand, ByVal strParamName As String, ByVal varType As OleDbType, ByVal varValue As Object)
        objCmd.Parameters.Add(strParamName, varType).Value = varValue
    End Sub

I know they aren't being applied in the correct order because when I take the variables as supplied to the command in the web application:
vRad = 50
vLat = 40.2411
vLng = 76.0567

and then apply them explicitly in a SQL statement to the Access database:
SELECT Subjects.BusinessName, Subjects.City, Subjects.StateCode
FROM Subjects
WHERE (((Subjects.Zip) In (SELECT Zip FROM ZipCodes WHERE 50 > 3959 * ATN( SQR(1 - (SIN(40.2411/ 57.3) * SIN(LAT/ 57.3) + COS(40.2411/ 57.3) * COS(LAT/ 57.3) * COS((LNG/ 57.3) - (76.0567/ 57.3))) ^2 ) / (SIN(40.2411/ 57.3) * SIN(LAT/ 57.3) + COS(40.2411/ 57.3) * COS(LAT/ 57.3) * COS((LNG/ 57.3) - (76.0567/ 57.3))) ) )));

I get 156 results, not the 5000+ returned by the OLEDB command.

I'm thinking there might be a way to surround one parameter at a time in the SQL string with some kind of internal function which will cause certain values to fail, and then supply test values one at a time to the command to determine the location of each ordered parameter, but I've already spent too much time on trial-and-error and think this is a job for.. the Experts Exchange!
0
Comment
Question by:psk1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 25

Accepted Solution

by:
clockwatcher earned 500 total points
ID: 17024373
Access should let you force the parameter ordering by adding a parameter clause to your sql and then using the named parameters in your sql:

   strSql = "PARAMETERS myrad Long, mylat IEEEDouble, mylon IEEEDouble; SELECT Subjects.SubjectID, Subjects.BusinessName, Subjects.City, Subjects.StateCode FROM (Subjects) WHERE (((Subjects.Zip) In (SELECT Zip FROM ZipCodes WHERE [myrad] > 3959 * ATN( SQR(1 - (SIN([mylat]/ 57.3) * SIN(LAT/ 57.3) + COS([mylat]/ 57.3) * COS(LAT/ 57.3) * COS((LNG/ 57.3) - ([mylon]/ 57.3))) ^2 ) / (SIN([mylon]/ 57.3) * SIN(LAT/ 57.3) + COS([mylat]/ 57.3) * COS(LAT/ 57.3) * COS((LNG/ 57.3) - ([mylon]/ 57.3))) ) )))"

You'll still have to add them in the order you've defined them in your parameter clause:

  setParam(objCmd,"rad", OleDbType.Int32, vRad)
  setParam(objCmd,"lon", OleDBType.Double, vLon)
  setParam(objCmd,"lat", OleDBType.Double, vLat)

Give it a try.
0
 
LVL 25

Expert Comment

by:clockwatcher
ID: 17024379
Sorry... I can't even manage to keep them in the same order.  Time to pack it in:

  setParam(objCmd,"rad", OleDbType.Int32, vRad)
  setParam(objCmd,"lat", OleDBType.Double, vLat)
  setParam(objCmd,"lon", OleDBType.Double, vLon)
0
 
LVL 2

Author Comment

by:psk1
ID: 17025236
You did it.  Thanks (again) clockwatcher.
0
 
LVL 2

Author Comment

by:psk1
ID: 17025241
Seriously, thanks.  You've saved me much time and confusion and helped to keep the ball rolling.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question