ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Discussion (Misc queries) (https://www.excelbanter.com/excel-discussion-misc-queries/)
-   -   extract data from combo box n use it in SQL (https://www.excelbanter.com/excel-discussion-misc-queries/13317-extract-data-combo-box-n-use-sql.html)

benj

extract data from combo box n use it in SQL
 

not sure if i got the right forum

I have a form created by VBA, and on the form I have a combo box where
the user can specify the month and year. I need to retrieve data from a
database using SQL but i require the input from the combo box to be part
of my condition in my SQL statement to retrieve the data. so any idea
wat is the syntax for writing a SQL statement with the consideration of
the input from the combo box?

right now i have 2 combo box. 1 holding the months, the other holding
the year

SELECT b.OrgID, a.OrgName,eventDate, FROM tblossevent AS b, tbOrg AS a
WHERE eventdate BETWEEN convert(varchar,getdate()-30, 106) AND
convert(varchar,getdate(), 106)

this is wat i have initially
but i wan the eventdate to be obtained from the input of the combo box.
how shd i change my sql coding?


--
benj
------------------------------------------------------------------------
benj's Profile: http://www.excelforum.com/member.php...o&userid=19303
View this thread: http://www.excelforum.com/showthread...hreadid=345762


Oliver Ferns via OfficeKB.com

Hi,
I would hardcode the SQL Statements (Select, From, Where, Between) in to
Global variables as such....

Private Const sqlSelect As String = "SELECT b.OrgID, a.OrgName, "
Private Const sqlFrom as String = " FROM tblossevent AS b, tbOrg AS a"
Private Const sqlWhere as String = " WHERE "
Private Const sqlBetween as String = " BETWEEN convert(varchar, _
getdate()-30, 106) AND convert(varchar,getdate(), 106)"

You can then construct your SQL statement fairly easily with the following
syntax:

Let strSQL = sqlSelect & sqlFrom & sqlWhere & YourVariable & sqlBetween

Then you can use strSQL as your SQL string!
Does this help?

Oli

--
Message posted via http://www.officekb.com

Jamie Collins


benj wrote:
SELECT b.OrgID, a.OrgName,eventDate, FROM tblossevent AS b, tbOrg AS

a
WHERE eventdate BETWEEN convert(varchar,getdate()-30, 106) AND
convert(varchar,getdate(), 106)


What data type is your eventdate column? You seem to be converting your
parameter date values to text. Perhaps this is the source of your
confusion.

Jamie.

--



All times are GMT +1. The time now is 04:32 AM.

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