Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Passing SQL parameters to Access database in Visual Basic 2010

Posted on 2012-04-02
2
Medium Priority
?
397 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
2 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Windows Explorer let you handle zip folders nearly as any other folder: Copy, move, change, and delete, etc. In VBA you can also handle normal files and folders, but zip folders takes a little more - and that you'll find here.
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…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

877 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