Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
KC KC is offline
external usenet poster
 
Posts: 107
Default Running a SQL Update Query in Excel VBA

I am using VBA in Excel to connect to a SQL server and retreve data. That
is working. The problem is that I want to run an Update query to update one
line in the database and it isn't working. Below is an example of the code I
am using.


Dim cmdCommand As New ADODB.Command
Dim recSet As New ADODB.Recordset
Dim cn As New ADODB.Connection
cmdCommand.ActiveConnection = cn
strSQLCommand = "UPDATE Materials.ReportName = test Where
materials.MaterialID = 5"
cmdCommand.CommandText = strSQLCommand
cmdCommand.CommandType = adCmdText
Set recSet = cmdCommand.Execute
please help
--
KC
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 108
Default Running a SQL Update Query in Excel VBA

Roedd <<KC wedi ysgrifennu:

I am using VBA in Excel to connect to a SQL server and retreve
data. That is working. The problem is that I want to run an Update
query to update one line in the database and it isn't working. Below
is an example of the code I am using.

Dim cmdCommand As New ADODB.Command
Dim recSet As New ADODB.Recordset
Dim cn As New ADODB.Connection
cmdCommand.ActiveConnection = cn
strSQLCommand = "UPDATE Materials.ReportName = test Where
materials.MaterialID = 5"
cmdCommand.CommandText = strSQLCommand
cmdCommand.CommandType = adCmdText
Set recSet = cmdCommand.Execute
please help


Your UPDATE syntax is wrong. The correct syntax for update is:

UPDATE table_name
SET column_name = new_value
WHERE column_name = some_value

so try something like "UPDATE Materials set ReportName = 'test' Where
MaterialID = 5". Note that I have also enclosed the string value 'test' in
single quotes.

HTH

Rob



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,452
Default Running a SQL Update Query in Excel VBA

Try something like this:

Sub test()

Dim objCommand As ADODB.Command
Set objCommand = New ADODB.Command

With objCommand
.ActiveConnection = ADOConn
.CommandText = "UPDATE Materials " & _
"set ReportName = 'test' " & _
"Where MaterialID = 5"
.Execute
End With

End Sub


RBS

"KC" wrote in message
...
I am using VBA in Excel to connect to a SQL server and retreve data.
That
is working. The problem is that I want to run an Update query to update
one
line in the database and it isn't working. Below is an example of the
code I
am using.


Dim cmdCommand As New ADODB.Command
Dim recSet As New ADODB.Recordset
Dim cn As New ADODB.Connection
cmdCommand.ActiveConnection = cn
strSQLCommand = "UPDATE Materials.ReportName = test Where
materials.MaterialID = 5"
cmdCommand.CommandText = strSQLCommand
cmdCommand.CommandType = adCmdText
Set recSet = cmdCommand.Execute
please help
--
KC


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Running MS Query on an Excel Spreadsheet [email protected] Excel Discussion (Misc queries) 3 October 1st 09 08:53 AM
Running multiple queries in Microsoft Query in Excel [email protected] Excel Programming 3 April 26th 05 09:58 PM
How access/query/update Excel using VC? Ong Hong Peow Excel Programming 1 September 21st 04 01:59 PM
Running an Access Query From Excel Paul Smith[_3_] Excel Programming 0 August 17th 04 11:16 PM
Excel-Macro running through SQL update. rudolpsh Excel Programming 0 January 22nd 04 12:39 PM


All times are GMT +1. The time now is 10:21 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"