Vito
Vito

Reputation: 299

Is it able to pass the VBA variable into ADO SQL statement?

I use ADO to run the SQL in excel .

The SQL is

 sSQLSting = "SELECT  officer ,sum(mkt) from [$DB] where month=2 group by officer"

In my system , there is a combobox that consist of the options of YES & NO.

For Yes , the SQL where statement would become where month=3

For No , the SQL where statement would become where month=4

In my plan , I want to Dim an integer variable to store month in VBA.Then , variable -month gets the combobox result so that 3 will be stored into month.


Lastly , the integer variable-month can pass to the SQL statement ***

     sSQLSting = "SELECT  officer ,sum(mkt) from [$DB] where month=***group by officer

This is my plan . Is it possible to catch the combobox result and pass the value to SQL statement ?

Upvotes: 2

Views: 876

Answers (1)

KyloRen
KyloRen

Reputation: 2741

Something like this will be able to pass a variable in a SQL statement. It is still a string so you just need to treat it like one.

Dim month As Long
 month = 2
 sSQLSting = "SELECT  officer ,sum(mkt) from [$DB] where month= " & month & " group by officer"

For a combobox, something like this,

Dim month As Long
     month = combobox1.Value
     sSQLSting = "SELECT  officer ,sum(mkt) from [$DB] where month= " & month & " group by officer"

Of coarse you will have to edit the code to suit your combo_box name etc.

Upvotes: 1

Related Questions