Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default Trying DROP TABLE in SQL from Excel

Hi all

I am trying to do a DROP TABLE in MSSQL from Excel without success
(well, at least with partial success).

Enclosed is the code:
sSql = "if exists (select * from dbo.sysobjects where id =
object_id(N'tmpCst')) DROP TABLE tmpCst"
Selection.QueryTable.Sql = sSql
' for some reason the query will not run with
BackgroundQuery:=False
Selection.QueryTable.Refresh BackgroundQuery:=True
' loop added but never ends
Do While Selection.QueryTable.Refreshing
Sleep (1000)
Loop


What's happening is that if I run the code with BackgroundQuery:=True,
it executes perfectly, table is dropped. However this means that my
next lines of codes will start executing, and that won't do. So I add
a line to check while query is still Refreshing, if not sleep for
1sec, check again, but it never ends. It will work in debug mode,
meaning if I step thru the .Refreshing code it will actually complete.

I have checked that even at the point when it sleeps for the first
time, the SQL command is executed already (table is dropped) but the
..Refreshing remains True. Only if there is a break will the
..Refreshing change to a False.

Any help here is appreciated.

Regards.

Bernard.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 107
Default Trying DROP TABLE in SQL from Excel

This might be a help for getting data to and from Excel and Access: It
includes examples of using variables in SQL queries.
http://www.bygsoftware.com/examples/sql.html

Or you can get there from the "Excel with Access Databases" section on page:
http://www.bygsoftware.com/examples/examples.htm

It demonstrates how to use SQL in Excel's VBA to:

* create a database,
* create a table
* insert records
* select records,
* update records,
* delete records,
* delete a table,
* delete a database.

DAO and ADO files available.

You can also download the demonstration file called "excelsql.zip".

The code is open and commented.


--
Regards
Andy Wiggins
www.BygSoftware.com
Home of "Save and BackUp",
"The Excel Auditor" and "Byg Tools for VBA"



"Bernard Soh" wrote in message
om...
Hi all

I am trying to do a DROP TABLE in MSSQL from Excel without success
(well, at least with partial success).

Enclosed is the code:
sSql = "if exists (select * from dbo.sysobjects where id =
object_id(N'tmpCst')) DROP TABLE tmpCst"
Selection.QueryTable.Sql = sSql
' for some reason the query will not run with
BackgroundQuery:=False
Selection.QueryTable.Refresh BackgroundQuery:=True
' loop added but never ends
Do While Selection.QueryTable.Refreshing
Sleep (1000)
Loop


What's happening is that if I run the code with BackgroundQuery:=True,
it executes perfectly, table is dropped. However this means that my
next lines of codes will start executing, and that won't do. So I add
a line to check while query is still Refreshing, if not sleep for
1sec, check again, but it never ends. It will work in debug mode,
meaning if I step thru the .Refreshing code it will actually complete.

I have checked that even at the point when it sleeps for the first
time, the SQL command is executed already (table is dropped) but the
.Refreshing remains True. Only if there is a break will the
.Refreshing change to a False.

Any help here is appreciated.

Regards.

Bernard.



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default Trying DROP TABLE in SQL from Excel

Thanks for all the replies. I have figured out the problem I was
facing with ADO which I tried unsuccessfully earlier.

Instead of doing a
Dim cnn as New ADODB.Connection
Dim cmd as New ADODB.Command
(which will give me a user-defined type not defined)

I did a
Dim cnn As Object
Dim cmd As Object

Set cnn = CreateObject("ADODB.Connection")
Set cmd = CreateObject("ADODB.Command")

cnn.ConnectionString = "DSN=mydsn;UID=myUID;PWD=myPWD"
cnn.Open
cmd.ActiveConnection = cnn

cmd.CommandText = "blah blah sql"
cmd.Execute


Thanks for the advises which helped me find the solution.

Regards.

Bernard.



(Bernard Soh) wrote in message . com...
Hi all

I am trying to do a DROP TABLE in MSSQL from Excel without success
(well, at least with partial success).

Enclosed is the code:
sSql = "if exists (select * from dbo.sysobjects where id =
object_id(N'tmpCst')) DROP TABLE tmpCst"
Selection.QueryTable.Sql = sSql
' for some reason the query will not run with
BackgroundQuery:=False
Selection.QueryTable.Refresh BackgroundQuery:=True
' loop added but never ends
Do While Selection.QueryTable.Refreshing
Sleep (1000)
Loop


What's happening is that if I run the code with BackgroundQuery:=True,
it executes perfectly, table is dropped. However this means that my
next lines of codes will start executing, and that won't do. So I add
a line to check while query is still Refreshing, if not sleep for
1sec, check again, but it never ends. It will work in debug mode,
meaning if I step thru the .Refreshing code it will actually complete.

I have checked that even at the point when it sleeps for the first
time, the SQL command is executed already (table is dropped) but the
.Refreshing remains True. Only if there is a break will the
.Refreshing change to a False.

Any help here is appreciated.

Regards.

Bernard.

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
how do i refresh a pivot table drop down list in excel 2003? Greg W Excel Discussion (Misc queries) 0 December 8th 09 05:26 PM
table w/ some columns as drop down purplec0ws Excel Discussion (Misc queries) 1 October 1st 09 11:46 PM
Drop down table RB[_2_] Excel Worksheet Functions 1 March 6th 08 10:16 PM
Pivot Table drop down Bhanji Excel Discussion (Misc queries) 1 November 1st 07 12:14 AM
Pivot Table Drop down Dale Hagan Charts and Charting in Excel 2 July 23rd 07 07:20 PM


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