Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Passing SQL parameters to Access database in Visual Basic 2010

Posted on 2012-04-02
2
Medium Priority
?
386 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 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

Application Discovery Service in AWS

In the era of the cloud, customers migrating away from their existing on-premise infrastructure. This requires lots of planning, strategies, and effort to identify their existing resources and determine how best to migrate.  Datacenter migrations happen in four phases -

Question has a verified solution.

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

In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
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…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

688 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