Solved

Passing SQL parameters to Access database in Visual Basic 2010

Posted on 2012-04-02
2
369 Views
Last Modified: 2012-04-02
Hi,

This is the way I am used to pass parameters to an SQL server database. This time I'm using an Access database and my code doesn't seem to work.
The @Id parameter is passed like it should be, but @strLatitude and @strLongitude are not.
Everything works when I hard code the latitude and longitude in the SQL command, but that is not what I want.

   dim strSQL as string
   dim strLongitude as string = "4.123456"
   dim strLatitude as string = "51.123456"

   strSQL = "UPDATE    Bedrijven " & _
             "SET       Bdr_strLatitude = @strLatitude, " & _
             "          Bdr_strLongitude = @strLongitude " & _
             "WHERE     Bdr_Id = @Id;"
     Using cmdSQL As OleDbCommand = New OleDbCommand(strSQL, CN)
      cmdSQL.Parameters.Add("@Id", OleDbType.Integer).Value = Id
      cmdSQL.Parameters.Add("@strLatitude", OleDbType.VarChar).Value = strLatitude
      cmdSQL.Parameters.Add("@strLongitude", OleDbType.VarChar).Value = strLongitude
    Try
      If CN.State <> ConnectionState.Open Then CN.Open()
      cmdSQL.ExecuteNonQuery()
    Catch ex As Exception
      Call Fout71(ex.Message)
      Return
    Finally
      CN.Close()
    End Try
  End Using

Open in new window


What am I doing wrong?
0
Comment
Question by:NoraWil
[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 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 37798528
OleDB uses positional parameters. Use a question mark for the placeholder ( ? ) within the query, then add the parameters to the Parameters collection in the order they appear within the query--the names you pass to the Add method are meaningless, so you can pass any arbitrary name.
0
 

Author Closing Comment

by:NoraWil
ID: 37798621
Sometimes is simplier than expected.
Thanks.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying 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

A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

749 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