Results 1 to 7 of 7

Thread: Build query based on two variables

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2010
    Posts
    13

    Build query based on two variables

    Hi All,

    In my program I have a combo box that the user selects a date from and a list view that the user selects an employee ID from. How do I properly design my database query string to use both the date selected and the employee ID selected? The date is stored in a variable called YearMonth and the employee ID is stored in a variable called EmpID.

    Here is what I am trying to do with my query:
    Code:
            Dim ExactQuery As String = _
               "SELECT * " & _
                "FROM[JobTypeExact_Query] WHERE (EMPID = ?) AND WHERE (YEARMONTH = YM)" 'Define Exact job type query
    
            ExactQuery = ExactQuery.Replace("?", CStr(EmpID)).Replace("YM", CStr(YearMonth)) 'Constrain to Employee and YearMonth chosen
    Is there something wrong with my syntax here and/or is there an easier way to accomplish this?

    Thanks in advance for your help,
    Diana

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Build query based on two variables

    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3
    Addicted Member vb_ftw's Avatar
    Join Date
    Dec 2010
    Posts
    139

    Re: Build query based on two variables

    y dont u just put the variables in when declaring? y replace?

    Dim ExactQuery As String = _
    "SELECT * " & _
    "FROM[JobTypeExact_Query] WHERE (EMPID = EmpID) AND WHERE (YEARMONTH = YearMonth)"

  4. #4
    Addicted Member vb_ftw's Avatar
    Join Date
    Dec 2010
    Posts
    139

    Re: Build query based on two variables

    i meant
    Dim ExactQuery As String = _
    "SELECT * " & _
    "FROM[JobTypeExact_Query] WHERE (EMPID = " & EmpID & ") AND WHERE (YEARMONTH = "& YearMonth & ")" 'Define Exact job type query

  5. #5
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Build query based on two variables

    because the query would still be wrong... and it's a bad habit.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  6. #6

    Thread Starter
    New Member
    Join Date
    Dec 2010
    Posts
    13

    Re: Build query based on two variables

    Well,

    That was a lot to read and I am still confused and not sure how to implement using parameters in my query at all. It seems that I would also have to change a lot more than just my query.

    Can someone please be more helpful than telling me to read an article?

    Thanks!
    -Diana

  7. #7
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Build query based on two variables

    First, in all SQL queries, you only need one WHERE clause
    Code:
    'it would not be
    "FROM[JobTypeExact_Query] WHERE (EMPID = ?) AND WHERE (YEARMONTH = YM)"
    'but rather
    "FROM[JobTypeExact_Query] WHERE (EMPID = ?) AND YEARMONTH = YM"
    With that in mind, lets add some parameters. Try this:
    Code:
    Dim ExactQuery As String = _
               "SELECT * " & _
                "FROM[JobTypeExact_Query] WHERE EMPID = @EMPID AND YEARMONTH = @YM" 
    Dim command As New SqlCommand(ExactQuery, myConnection) 'here replace myConnection 
                                                            'with your connection object
    command.Parameters.AddWithValue("@EMPID", YourListViewName)        
    command.Parameters.AddWithValue("@YM", YourComboBoxName)
    That is the basics of using parametized queries.

    <Pet_Peeve>Do you really need to have every single solitary field in the entire table returned for your query to work? If not, then why are you doiing SELECT *? Your SELECT clause should contain only those fields that you need for that particular query.</Pet_Peeve>

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