How to call the column which is selected in Combobox in SQL?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Gobindap
    New Member
    • Feb 2013
    • 25

    How to call the column which is selected in Combobox in SQL?

    I want to call the column which is selected in combobox. Suppose, a table contains ID, name, A1, A2, A3, A4, A5 ......An. I want to return value from ID, name and a column which is selected from Combobox. (i.e. if A1 is selected in combobox then value from A1 should come if A2 is selected in combobox then value from A2 should come.

    Is it possible? If possible then how can I do it? If impossible then how to solve such problems?
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    You can do so by building a dynamic SQL string. That's all I can say without seeing your existing code.

    Comment

    • wizarddrummer
      New Member
      • Feb 2013
      • 1

      #3
      If you are referring to columns then what is stored in a combo box are the fields for whatever the query or record source of the combo box.
      In your question you have ID, Name, A1 ... A5. If your table has 100 records then the combo box will have 100 rows from that table.
      If you are able to select ID, Name, A1, A2 or A3, etc. as individual selections, in other words the drop down box displays the ID, Name, A1, etc. on a separate line then you don't have columns you have a field with many different types of data in it.

      There would be no way to single out A1 or A2 or A3 if they are a column without creating a drop down box for each column based on a query.

      Comment

      • Gobindap
        New Member
        • Feb 2013
        • 25

        #4
        Thank you for your king support.

        I think my problem is poorly described. I am making it clear now.

        Suppose there are 10 columns in a table as: ID, name, A1, A2, A3, A4, A5, A6, A7 and A8. I want to return records from ID, name and one another column from among A1-A8. The next column I return is selected from dropdown list. Now general SQL to be written will be:
        Code:
        SELECT ID, name, <column to be selected from list> From ..... Where .....
        For this I simply tried
        Code:
        String col = (String)comboBox.getSelectedItem();
        Code:
        SELECT ID, name, col FROM ..... WHERE .....
        But it did not function correctly.
        So, please help me to overcome my problem.
        Last edited by NeoPa; Feb 11 '13, 11:14 AM. Reason: Added mandatory [CODE] tags.

        Comment

        • Rabbit
          Recognized Expert MVP
          • Jan 2007
          • 12517

          #5
          I understand your question. The answer remains the same. You have to build a dynamic SQL string. And again, that's all I can say if you don't post the code you're using. So far, you've only posted one line of code and some generic SQL. The SQL, while code, is unlikely to be the exact line of code that you're actually using.

          So unless you post the information that is asked for, there's not much I can do to help.

          Comment

          Working...