Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
LD LD is offline
external usenet poster
 
Posts: 10
Default Truncate, Insert, or Update SQL command in EXCEL Macro

I want to do other types of SQL commands in an EXCEL macro besides standard
"select" queries. In particular, I'd like to do a "truncate table" , and also
an "Insert" and an "Update". I recorded a macro to do a "select", thinking I
would then just edit the "select" statement to do what I want, but it does
not appear to do anything when I execute it. Here's a sample of the code that
was recorded. The query is passed to an ORACLE database through an ODBC
connection.

Step 2 will be to pass some parameters (data) to SQL in the insert and
update commands. Any suggestions and examples would be most sincerely
appreciated. Thanks!

Sub Exec_Query()
'
'
With ActiveSheet.QueryTables.Add(Connection:=Array(Arra y( _
"ODBC;DSN=lss;UID=userid;PWD=password;DBQ=LSS; DBA=
W;APA=T;EXC=F;FEN=T;QTO=T;FRC=10;FDL=10;LOB=T;RST=
T;BTD=F;BAM=IfAllSuccessful;NUM=N" _
), Array("LS;DPM=F;MTS=T;MDI=F;CSR=F;FWC=F;FBS=64000; TLO=O;")),
Destination:= _
Range("A1"))
..CommandText = Array( _
"SELECT var1, var2" & Chr(13) & "" & Chr(10) & "FROM master_tbl" & Chr(13) &
"" & Chr(10) & "WHERE (var1 = var2)")
..Name = "Query"
..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 With
End Sub

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 131
Default Truncate, Insert, or Update SQL command in EXCEL Macro

"LD" wrote:

I want to do other types of SQL commands in an EXCEL macro besides standard
"select" queries. In particular, I'd like to do a "truncate table" , and also
an "Insert" and an "Update". I recorded a macro to do a "select", thinking I
would then just edit the "select" statement to do what I want, but it does
not appear to do anything when I execute it. Here's a sample of the code that
was recorded. The query is passed to an ORACLE database through an ODBC
connection.

Step 2 will be to pass some parameters (data) to SQL in the insert and
update commands. Any suggestions and examples would be most sincerely
appreciated. Thanks!

Sub Exec_Query()
'
'
With ActiveSheet.QueryTables.Add(Connection:=Array(Arra y( _
"ODBC;DSN=lss;UID=userid;PWD=password;DBQ=LSS; DBA=
W;APA=T;EXC=F;FEN=T;QTO=T;FRC=10;FDL=10;LOB=T;RST=
T;BTD=F;BAM=IfAllSuccessful;NUM=N" _
), Array("LS;DPM=F;MTS=T;MDI=F;CSR=F;FWC=F;FBS=64000; TLO=O;")),
Destination:= _
Range("A1"))
.CommandText = Array( _
"SELECT var1, var2" & Chr(13) & "" & Chr(10) & "FROM master_tbl" & Chr(13) &
"" & Chr(10) & "WHERE (var1 = var2)")
.Name = "Query"
.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 With
End Sub


One way to do that is using ADO, Connection and Command objects. Pass the
Command object strings containing SQL queries and then use Execute

Sub ADOTest()

Dim Cnn As ADODB.Connection
Dim Cmd As ADODB.Command

Set Cnn = New ADODB.Connection

Cnn.ConnectionString = _
"Data Source='MyDataSource';"
Cnn.ConnectionTimeout = 30
Cnn.Open

Set Cmd = New ADODB.Command
Cmd.ActiveConnection = Cnn

strSQLCreate = "CREATE TABLE MyTable " & _
"(Field1 int , " & _
"Field2 char(20) NULL, " & _
"Field3 datetime NULL)"
Cmd.CommandText = strSQLCreate
Cmd.Execute

strSQLInsert = "INSERT INTO MyTable " & _
"VALUES (987, 'some value', '11/25/2006')"
Cmd.CommandText = strSQLInsert
Cmd.Execute

strSQLUpdate = "UPDATE MyTable " & _
"SET MyTable.Field2 = 'another value' " & _
"WHERE Field1 = 987"
Cmd.CommandText = strSQLUpdate
Cmd.Execute

strSQLTruncate = "TRUNCATE TABLE MyTable"
Cmd.CommandText = strSQLTruncate
Cmd.Execute

strSQLDrop = "DROP TABLE MyTable"
Cmd.CommandText = strSQLDrop
Cmd.Execute

Cnn.Close
Set Cnn = Nothing
End Sub


I use SQL Server, but I think you can easily change the queries to work with
your database. You also need to change the connectio string.

Hope this helps some.


--
urkec

  #3   Report Post  
Posted to microsoft.public.excel.programming
LD LD is offline
external usenet poster
 
Posts: 10
Default Truncate, Insert, or Update SQL command in EXCEL Macro

Seems I may not have all the correct "object libraries" selected to use ADO,
but cannot determine which to select when I go to Tools/References. The
error message I get for the very first dimension statement is "Compile Error:
User-defined type not defined".

Any suggestions?



"urkec" wrote:

"LD" wrote:

I want to do other types of SQL commands in an EXCEL macro besides standard
"select" queries. In particular, I'd like to do a "truncate table" , and also
an "Insert" and an "Update". I recorded a macro to do a "select", thinking I
would then just edit the "select" statement to do what I want, but it does
not appear to do anything when I execute it. Here's a sample of the code that
was recorded. The query is passed to an ORACLE database through an ODBC
connection.

Step 2 will be to pass some parameters (data) to SQL in the insert and
update commands. Any suggestions and examples would be most sincerely
appreciated. Thanks!

Sub Exec_Query()
'
'
With ActiveSheet.QueryTables.Add(Connection:=Array(Arra y( _
"ODBC;DSN=lss;UID=userid;PWD=password;DBQ=LSS; DBA=
W;APA=T;EXC=F;FEN=T;QTO=T;FRC=10;FDL=10;LOB=T;RST=
T;BTD=F;BAM=IfAllSuccessful;NUM=N" _
), Array("LS;DPM=F;MTS=T;MDI=F;CSR=F;FWC=F;FBS=64000; TLO=O;")),
Destination:= _
Range("A1"))
.CommandText = Array( _
"SELECT var1, var2" & Chr(13) & "" & Chr(10) & "FROM master_tbl" & Chr(13) &
"" & Chr(10) & "WHERE (var1 = var2)")
.Name = "Query"
.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 With
End Sub


One way to do that is using ADO, Connection and Command objects. Pass the
Command object strings containing SQL queries and then use Execute

Sub ADOTest()

Dim Cnn As ADODB.Connection
Dim Cmd As ADODB.Command

Set Cnn = New ADODB.Connection

Cnn.ConnectionString = _
"Data Source='MyDataSource';"
Cnn.ConnectionTimeout = 30
Cnn.Open

Set Cmd = New ADODB.Command
Cmd.ActiveConnection = Cnn

strSQLCreate = "CREATE TABLE MyTable " & _
"(Field1 int , " & _
"Field2 char(20) NULL, " & _
"Field3 datetime NULL)"
Cmd.CommandText = strSQLCreate
Cmd.Execute

strSQLInsert = "INSERT INTO MyTable " & _
"VALUES (987, 'some value', '11/25/2006')"
Cmd.CommandText = strSQLInsert
Cmd.Execute

strSQLUpdate = "UPDATE MyTable " & _
"SET MyTable.Field2 = 'another value' " & _
"WHERE Field1 = 987"
Cmd.CommandText = strSQLUpdate
Cmd.Execute

strSQLTruncate = "TRUNCATE TABLE MyTable"
Cmd.CommandText = strSQLTruncate
Cmd.Execute

strSQLDrop = "DROP TABLE MyTable"
Cmd.CommandText = strSQLDrop
Cmd.Execute

Cnn.Close
Set Cnn = Nothing
End Sub


I use SQL Server, but I think you can easily change the queries to work with
your database. You also need to change the connectio string.

Hope this helps some.


--
urkec

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
can i write a macro to truncate alpha-numeric data? T-Dot Excel Discussion (Misc queries) 2 August 12th 06 08:15 AM
How do I set up macro to truncate column/field? Macro to truncate column Excel Programming 4 June 29th 06 06:09 PM
How can I insert a run macro command in the IF function? emil Excel Programming 4 April 15th 06 02:57 PM
Can I insert roundup command into different formulas in excel? Margaret Excel Worksheet Functions 4 August 5th 05 12:22 PM
How to execute an SQL insert/update from Excel for Mac Laphan[_2_] Excel Programming 1 June 22nd 04 09:01 PM


All times are GMT +1. The time now is 08:36 PM.

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

About Us

"It's about Microsoft Excel"