Solved

Passing SQL parameters to Access database in Visual Basic 2010

Posted on 2012-04-02
2
345 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 74

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
Comment Utility
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
Comment Utility
Sometimes is simplier than expected.
Thanks.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

762 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now