Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 73
Default 'UPDATE' sql statement

I have an ODBC driver to access a database but I can't figure out how to run
an UPDATE statement from Excel VBA.
I can create a query to return data and below is the recorded macro which
works fine and so shows that I can connect to the database OK.
So, how would I change this to run a simple UPDATE statement like
"UPDATE root.CLNDR CLNDR SET CLNDR.YN='Y'
WHERE CLNDR.DATE_=20041001;"

Sub Macro1()
With ActiveSheet.QueryTables.Add(Connection:="ODBC;DSN= TIMS.udd;", _
Destination:=Range("A1"))
.Sql = Array("SELECT CLNDR.DATE_, CLNDR.DAY, " & _
"CLNDR.NUMA, CLNDR.NUMB, CLNDR.YN" & vbCrLf & _
"FROM root.CLNDR CLNDR")
.FieldNames = True
.RefreshStyle = xlInsertDeleteCells
.RowNumbers = False
.FillAdjacentFormulas = False
.RefreshOnFileOpen = False
.HasAutoFormat = True
.BackgroundQuery = True
.TablesOnlyFromHTML = True
.Refresh BackgroundQuery:=False
.SavePassword = True
.SaveData = True
End With
End Sub

Thanks
Fred


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,588
Default 'UPDATE' sql statement

I'm not sure the built-in query table functionality will handle
updates.

Add a reference to the ADO 2.x library in the VBE and try this
(untested - you may have to fix the connection string a bit)

Tim.


Sub Tester()
Dim sSQL

sSQL = "UPDATE root.CLNDR CLNDR SET " & _
"CLNDR.YN='Y' WHERE CLNDR.DATE_=20041001"

MsgBox RunUpdate(sSQL)
End Sub



Function RunUpdate(sSQL As String) As Long

Dim lngRecs As Long
Dim oConn As ADODB.Recordset

Set oConn = CreateObject("ADODB.Recordset")
oConn.Open "ODBC;DSN=TIMS.udd;"
oConn.Execute sSQL, lngRecs, adCmdText
oConn.Close
Set oConn = Nothing

RunUpdate = lngRecs

End Function



"Fred" <leavemealone@home wrote in message
...
I have an ODBC driver to access a database but I can't figure out how
to run
an UPDATE statement from Excel VBA.
I can create a query to return data and below is the recorded macro
which
works fine and so shows that I can connect to the database OK.
So, how would I change this to run a simple UPDATE statement like
"UPDATE root.CLNDR CLNDR SET CLNDR.YN='Y'
WHERE CLNDR.DATE_=20041001;"

Sub Macro1()
With
ActiveSheet.QueryTables.Add(Connection:="ODBC;DSN= TIMS.udd;", _
Destination:=Range("A1"))
.Sql = Array("SELECT CLNDR.DATE_, CLNDR.DAY, " & _
"CLNDR.NUMA, CLNDR.NUMB, CLNDR.YN" & vbCrLf & _
"FROM root.CLNDR CLNDR")
.FieldNames = True
.RefreshStyle = xlInsertDeleteCells
.RowNumbers = False
.FillAdjacentFormulas = False
.RefreshOnFileOpen = False
.HasAutoFormat = True
.BackgroundQuery = True
.TablesOnlyFromHTML = True
.Refresh BackgroundQuery:=False
.SavePassword = True
.SaveData = True
End With
End Sub

Thanks
Fred




  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 73
Default 'UPDATE' sql statement

Thanks Tim but the ADODB.Recordset object is not recognised and I get an
error when I compile the project "User-defined type not defined.
I did add the reference to "Microsoft ADO Ext 2.7 for DDL and security". I
also tried adding "Microsoft DAO 3.51" but ADODB.Recordset is not
recognised.
I am running Office '97. Do I need a later version?

Fred

"Tim Williams" <saxifrax@pacbell*dot*net wrote in message
...
I'm not sure the built-in query table functionality will handle
updates.

Add a reference to the ADO 2.x library in the VBE and try this
(untested - you may have to fix the connection string a bit)

Tim.


Sub Tester()
Dim sSQL

sSQL = "UPDATE root.CLNDR CLNDR SET " & _
"CLNDR.YN='Y' WHERE CLNDR.DATE_=20041001"

MsgBox RunUpdate(sSQL)
End Sub



Function RunUpdate(sSQL As String) As Long

Dim lngRecs As Long
Dim oConn As ADODB.Recordset

Set oConn = CreateObject("ADODB.Recordset")
oConn.Open "ODBC;DSN=TIMS.udd;"
oConn.Execute sSQL, lngRecs, adCmdText
oConn.Close
Set oConn = Nothing

RunUpdate = lngRecs

End Function



"Fred" <leavemealone@home wrote in message
...
I have an ODBC driver to access a database but I can't figure out how
to run
an UPDATE statement from Excel VBA.
I can create a query to return data and below is the recorded macro
which
works fine and so shows that I can connect to the database OK.
So, how would I change this to run a simple UPDATE statement like
"UPDATE root.CLNDR CLNDR SET CLNDR.YN='Y'
WHERE CLNDR.DATE_=20041001;"

Sub Macro1()
With
ActiveSheet.QueryTables.Add(Connection:="ODBC;DSN= TIMS.udd;", _
Destination:=Range("A1"))
.Sql = Array("SELECT CLNDR.DATE_, CLNDR.DAY, " & _
"CLNDR.NUMA, CLNDR.NUMB, CLNDR.YN" & vbCrLf & _
"FROM root.CLNDR CLNDR")
.FieldNames = True
.RefreshStyle = xlInsertDeleteCells
.RowNumbers = False
.FillAdjacentFormulas = False
.RefreshOnFileOpen = False
.HasAutoFormat = True
.BackgroundQuery = True
.TablesOnlyFromHTML = True
.Refresh BackgroundQuery:=False
.SavePassword = True
.SaveData = True
End With
End Sub

Thanks
Fred






  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default 'UPDATE' sql statement

Try it with late binding. Change

Dim oConn As ADODB.Recordset

to

Dim oConn As Object

--

HTH

RP

"Fred" <leavemealone@home wrote in message
...
Thanks Tim but the ADODB.Recordset object is not recognised and I get an
error when I compile the project "User-defined type not defined.
I did add the reference to "Microsoft ADO Ext 2.7 for DDL and security". I
also tried adding "Microsoft DAO 3.51" but ADODB.Recordset is not
recognised.
I am running Office '97. Do I need a later version?

Fred

"Tim Williams" <saxifrax@pacbell*dot*net wrote in message
...
I'm not sure the built-in query table functionality will handle
updates.

Add a reference to the ADO 2.x library in the VBE and try this
(untested - you may have to fix the connection string a bit)

Tim.


Sub Tester()
Dim sSQL

sSQL = "UPDATE root.CLNDR CLNDR SET " & _
"CLNDR.YN='Y' WHERE CLNDR.DATE_=20041001"

MsgBox RunUpdate(sSQL)
End Sub



Function RunUpdate(sSQL As String) As Long

Dim lngRecs As Long
Dim oConn As ADODB.Recordset

Set oConn = CreateObject("ADODB.Recordset")
oConn.Open "ODBC;DSN=TIMS.udd;"
oConn.Execute sSQL, lngRecs, adCmdText
oConn.Close
Set oConn = Nothing

RunUpdate = lngRecs

End Function



"Fred" <leavemealone@home wrote in message
...
I have an ODBC driver to access a database but I can't figure out how
to run
an UPDATE statement from Excel VBA.
I can create a query to return data and below is the recorded macro
which
works fine and so shows that I can connect to the database OK.
So, how would I change this to run a simple UPDATE statement like
"UPDATE root.CLNDR CLNDR SET CLNDR.YN='Y'
WHERE CLNDR.DATE_=20041001;"

Sub Macro1()
With
ActiveSheet.QueryTables.Add(Connection:="ODBC;DSN= TIMS.udd;", _
Destination:=Range("A1"))
.Sql = Array("SELECT CLNDR.DATE_, CLNDR.DAY, " & _
"CLNDR.NUMA, CLNDR.NUMB, CLNDR.YN" & vbCrLf & _
"FROM root.CLNDR CLNDR")
.FieldNames = True
.RefreshStyle = xlInsertDeleteCells
.RowNumbers = False
.FillAdjacentFormulas = False
.RefreshOnFileOpen = False
.HasAutoFormat = True
.BackgroundQuery = True
.TablesOnlyFromHTML = True
.Refresh BackgroundQuery:=False
.SavePassword = True
.SaveData = True
End With
End Sub

Thanks
Fred








  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default 'UPDATE' sql statement


I'm not too familiar with this stuff, but this is something that worke
for me in Excel2000


Code
-------------------
sqlstring = "SELECT DISTINCT EQUIPMENTSPEC.EQNUM, EQUIPMENTSPEC.ASSETATTRID, EQUIPMENTSPEC.ALNVALUE FROM MAXIMO.EQUIPMENTSPEC EQUIPMENTSPEC"
connstring = "ODBC;DSN=maximo;Database=maximo"

With Worksheets("Data").QueryTables.Add(Connection:=con nstring, Destination:=Worksheets("Data").Range("B1"), Sql:=sqlstring)
.Name = "Maximo Query for Data Worksheet"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = True
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.Refresh BackgroundQuery:=False

End Wit
-------------------


I imagine this will work with any query as the sqlstring so long a
your username has the proper access privilege

--
ob3ron0
-----------------------------------------------------------------------
ob3ron02's Profile: http://www.excelforum.com/member.php...fo&userid=1545
View this thread: http://www.excelforum.com/showthread.php?threadid=27269



  #6   Report Post  
Posted to microsoft.public.excel.programming
TK TK is offline
external usenet poster
 
Posts: 177
Default 'UPDATE' sql statement

Hi Fred
To use ADO objects in an application add a reference
to the ADO component. From the VBA window select
Tools/References< check the box

"Microsoft ActiveX Data Objects 2.x Library"

Update table-name
Set Field_Name = 'new value'
Where Field_Name = Condition
i.e
Update Products
Set apples = 'Red Delicious'
Where State = Washington

Good Luck
Tk




"Fred" wrote:

Thanks Tim but the ADODB.Recordset object is not recognised and I get an
error when I compile the project "User-defined type not defined.
I did add the reference to "Microsoft ADO Ext 2.7 for DDL and security". I
also tried adding "Microsoft DAO 3.51" but ADODB.Recordset is not
recognised.
I am running Office '97. Do I need a later version?

Fred

"Tim Williams" <saxifrax@pacbell*dot*net wrote in message
...
I'm not sure the built-in query table functionality will handle
updates.

Add a reference to the ADO 2.x library in the VBE and try this
(untested - you may have to fix the connection string a bit)

Tim.


Sub Tester()
Dim sSQL

sSQL = "UPDATE root.CLNDR CLNDR SET " & _
"CLNDR.YN='Y' WHERE CLNDR.DATE_=20041001"

MsgBox RunUpdate(sSQL)
End Sub



Function RunUpdate(sSQL As String) As Long

Dim lngRecs As Long
Dim oConn As ADODB.Recordset

Set oConn = CreateObject("ADODB.Recordset")
oConn.Open "ODBC;DSN=TIMS.udd;"
oConn.Execute sSQL, lngRecs, adCmdText
oConn.Close
Set oConn = Nothing

RunUpdate = lngRecs

End Function



"Fred" <leavemealone@home wrote in message
...
I have an ODBC driver to access a database but I can't figure out how
to run
an UPDATE statement from Excel VBA.
I can create a query to return data and below is the recorded macro
which
works fine and so shows that I can connect to the database OK.
So, how would I change this to run a simple UPDATE statement like
"UPDATE root.CLNDR CLNDR SET CLNDR.YN='Y'
WHERE CLNDR.DATE_=20041001;"

Sub Macro1()
With
ActiveSheet.QueryTables.Add(Connection:="ODBC;DSN= TIMS.udd;", _
Destination:=Range("A1"))
.Sql = Array("SELECT CLNDR.DATE_, CLNDR.DAY, " & _
"CLNDR.NUMA, CLNDR.NUMB, CLNDR.YN" & vbCrLf & _
"FROM root.CLNDR CLNDR")
.FieldNames = True
.RefreshStyle = xlInsertDeleteCells
.RowNumbers = False
.FillAdjacentFormulas = False
.RefreshOnFileOpen = False
.HasAutoFormat = True
.BackgroundQuery = True
.TablesOnlyFromHTML = True
.Refresh BackgroundQuery:=False
.SavePassword = True
.SaveData = True
End With
End Sub

Thanks
Fred







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
Creating an Oracle UPDATE Statement from Excel rows PSULionRP Excel Discussion (Misc queries) 1 November 2nd 09 03:59 PM
Reconcile Bank statement & Credit card statement & accounting data Bklynhyc Excel Worksheet Functions 0 October 7th 09 09:07 PM
Data Validation lists update orginal cell with list update [email protected] Excel Worksheet Functions 3 July 11th 08 07:56 AM
HOW DO I SET UP AN IF STATEMENT THAT WILL UPDATE MONTHLY DEVONA Excel Worksheet Functions 2 January 5th 08 09:28 AM


All times are GMT +1. The time now is 03:42 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"