CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: select command

  1. #1
    Join Date
    Nov 2009
    Posts
    40

    Angry select command

    Hi,
    I'm new in VB so my question can be easy for you. I need to select some columns from a table and in where clause I might have a lot of columns. I created more than one select for this job but I suppose there is a way to create only one:
    My select is big but let's say I have 3 column for the where clause. First I can select having only one in the where clause, second I have 2 of them and in the end I have all of them. How can I create a single select for all 3?

  2. #2
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: select command

    It would help if you had showed your select and where clauses but I can give you a basic example.

    Code:
    Select Field1,Field2 .... From TableName Where Field1=Value And Field2=Value And Field3=Value
    This assumes that you only want the records where all 3 fields match the given values You could use OR in place of And or a combination of the 2 depending on what you want.
    Always use [code][/code] tags when posting code.

  3. #3
    Join Date
    Nov 2009
    Posts
    40

    Re: select command

    Well it's not like that. I have to select and I want to combine them.
    first select:
    select ... from tablename where field1 = value1 (given the codition the user asked)
    and the second, if the user asked (to query) for field2 is with field2.
    select ... from tablename where field2 = value2 (given the codition the user asked)
    But the user can ask for the first field, or for the second or for both. I want a single select for all 3. I'm thinking at the where clause like a string....

  4. #4
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: select command

    So you need the select statement to be built after the user selects something.
    You could make your where clause a seperate string then combine it with the select portin basd on the users selection.

    Code:
    Dim strSelect as String
    Dim strWhere as String
    
    strSelect="Select  ..... from tablename "
    If UserChoice1 Then
        strWhere="Where Filed1=Value"
    ElseIf UserChoice2 Then
        strWhere="Where Filed2=Value"
    End If
    strSelect=strSelect & strWhere
    You could also just use a variable for the fieldname and insert that at runtime.
    Always use [code][/code] tags when posting code.

  5. #5
    Join Date
    Nov 2009
    Posts
    40

    Re: select command

    Yes, the user can select one or many items. Depending on this I have to select the lines. Thanks. I'll try.
    Is there a way to combine the conditions in only one? I want to say: in the first select I need the field1 value but not in the second one. Can I say field1 = value if value is not null and field1 = field1 is value is null? Like the if condition?!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured