Page 1 of 1

Connect to SQL2005 DB from excel VBA

Posted: 30 Jul 2010, 08:36
by DeeVeeDee
Ive got the following code which works.

Code: Select all

MyQuery = "INSERT INTO FC_VALUES (ITEM_NUMBER,ITEM_DESC) VALUES ('EXCELTESTNUMBER','TEST DESCRIPTION')"
'MsgBox MyQuery

Sheets("Sheet1").Select
Range("A1").Select
Sqlstring = MyQuery '"select IDCUST from ARCUS"

connstring = "ODBC;DSN=ODBCTEST;UID=sa;PWD=password01#;Database=FC_DB"
With ActiveSheet.QueryTables.Add(Connection:=connstring, Destination:=Range("A1"), Sql:=Sqlstring)

 .Refresh
 End With
Problem is the above puts some text in to A1 which i dont want.
If i had to run the query twice its shifts A1 to A2 and put some text into A1 "Excel Query" or something like that.
I want to run a SQL query without it putting text on the sheet.

How can i do that?
I even tried to put the code in Sheet1 then Say Sheet2.A1