?
Solved

OLEDB command parameters not being assigned in order in SQL string

Posted on 2006-07-01
4
Medium Priority
?
235 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
  • 2
  • 2
4 Comments
 
LVL 25

Accepted Solution

by:
clockwatcher earned 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

840 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