ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   SQL from VB (https://www.excelbanter.com/excel-programming/346095-sql-vbulletin.html)

kamil b.

SQL from VB
 
Hi,
I need to do function or sub, which is able to edit a SELECT in MS QUERY.
For example:

MS QUERY SQL:
select * from dba.company where name='ABC'

EXCEL VB:
function do_sql(X)
select * from dba.company where name=X
end function

What is the right way to do this. Is it possible to use SQL in VB?

Thanx, kamil



Bob Phillips[_6_]

SQL from VB
 
Yes it is. The best way to get started is to turn on the macro recorder, run
your MS Query, then edit the macro to your needs. You could post back if you
have any specific problems then.

--
HTH

Bob Phillips

(remove nothere from email address if mailing direct)

"kamil b." wrote in message
...
Hi,
I need to do function or sub, which is able to edit a SELECT in MS QUERY.
For example:

MS QUERY SQL:
select * from dba.company where name='ABC'

EXCEL VB:
function do_sql(X)
select * from dba.company where name=X
end function

What is the right way to do this. Is it possible to use SQL in VB?

Thanx, kamil





K Dales[_2_]

SQL from VB
 
First, you could do this by setting up a parameter in MSQuery linked to your
workbook; if you do not know how to do that then check the Help in MSQuery on
Parameters.

But if you need to do this in code, it is really quite easy. If you have
the query set up already for a particular set of data, the SQL used will be
in the QueryTable.CommandText property. You can modify it easily with code
then refresh your query:

With Worksheets("SheetName").QueryTables(1)
.CommandText = Replace(.CommandText, "'ABC'", "'X'")
.Refresh
End With
--
- K Dales


"kamil b." wrote:

Hi,
I need to do function or sub, which is able to edit a SELECT in MS QUERY.
For example:

MS QUERY SQL:
select * from dba.company where name='ABC'

EXCEL VB:
function do_sql(X)
select * from dba.company where name=X
end function

What is the right way to do this. Is it possible to use SQL in VB?

Thanx, kamil





All times are GMT +1. The time now is 02:22 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com