Click here to Skip to main content
15,885,161 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I am using below code in VB 2008 to update a table in MS Access 2010. Its not updating, no error as well. Whats is wrong with below code.

VB
Dim cons, ins As String
        cons = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Queue Manager - Offline\Queue Manager - Offine.accdb"
        Dim con As New OleDbConnection(cons)
        con.Open()
        Dim cmd As New OleDbCommand
        ins = "UPDATE OrderInfo SET InitialBatchID ='" & 5000 & "' WHERE 'GLID Number' = '" & TextBox9.Text & "'"
        cmd.CommandText = ins
        cmd.Connection = con
        cmd.ExecuteNonQuery()
        cmd.Dispose()
        MsgBox("success")
        con.Close()
Posted
Comments
Maciej Los 18-Mar-15 7:59am    
Have you tried to execute above query in MS Access database?

Could be anything: we can't tell from that (and we can't test your code, because we don't have access to your database.)

So start with the debugger: put a breakpoint on the first line of that code, and run your app in the debugger. When it reaches the breakpoint, execution will stop, and let you step through your code and see exactly what is happening.

But please, don't do it like that. Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.
 
Share this answer
 
Comments
Maciej Los 18-Mar-15 8:08am    
+5 for the last statement!
Shafiul Alam 18-Mar-15 22:07pm    
Hi Thanks for your suggestion on Parametrized queries, But i dont know about that. Can you please share some resources/Links. Thanks a lot for you time.
OriginalGriff 19-Mar-15 6:06am    
https://www.google.co.uk/search?q=Parametrized+queries&oq=Parametrized+queries&aqs=chrome..69i57&sourceid=chrome&es_sm=93&ie=UTF-8
Please, read my comment to the question.

I see at least 2 reasons of above behaviour:
1) Connection string is incomplete!
Should be:
VB
cons = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\myFolder\myAccessFile.accdb; Persist Security Info=False;"


2) Update statement could be wrong!
a) If InitialBatchID and GLID number are numeric fields, you should remove ' arounding values.
b) If field name contains space between words, you should add [] bracket around it: [GLID number] instead of 'GLID number'

Finally:
If executable is in the same folder, you need to move it into another folder.
OriginalGriff is right. You should use parametrized queries.
 
Share this answer
 
v2
Comments
Shafiul Alam 18-Mar-15 22:10pm    
Hi Maciej Los, Thank you very much for your time. I updated the SQL as you suggested and it works great :)

Thank you very much.
Maciej Los 19-Mar-15 2:51am    
You're very welcome ;)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900