PDA

View Full Version : VBA - Need Help for a SQL Query



avadhutd2
08-06-2009, 11:44 PM
Hi,

I have a question regarding a SQL query. The scenario is as follows -

I have a couple of listboxes with me. One provides available options & another Selected options. User will select the options (which are column names) from the "ListBox1" & add to "ListBox2".

The query is needed where the columns "Selected" in ListBox2 are to be fetched with data from database.

For example -

If available options are - A, B, C, D, E, F, G, H (in ListBox1)
I select - A & B (in ListBox2)

Query will be something as - Select A, B from DB_NAME

The main question here is I am not sure about the selections that user has done. So I need help to formulate this query.


I hope my query is clear ....Can anybody help me out?

Thanks!

Bob Phillips
08-07-2009, 05:44 AM
Dim i As Long
Dim SQL as string

SQL = "SELECT "
With Me.ListBox1

For i = 0 To .ListCount - 1

If .Selected(i) Then

SQL = SQL & .List(i, 0) & ", "
End If
Next i

SQL = Left$(SQL, Len(SQL) -1) & " FROM DB_NAME"
End With