Results 1 to 8 of 8

Thread: passing variable into sql statement

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2006
    Posts
    55

    passing variable into sql statement

    Hi guys i have a small problem passing a variable into a sql statement. Could i please get some assistance.
    Here is my current code:

    Code:
    Private Sub Form_Open(Cancel As Integer)
    Dim dc As Variant
    Select Case Me.OpenArgs
        Case "Dealer_Totals"
            Me.Date_Combo.Visible = True
            Me.Distributor_Combo.Visible = False
            Me.Date_Combo.RowSource = "SELECT COMMISSION_MONTH FROM COMMISSION_MONTH WHERE (TRANSACTIONS_IMPORTED = 1)"
        Case "Dealer_Product_Group"
            '1st make Date_combo visible = false, until user first selects distributor, then make date_combo visible = true, and pass in distributor value into select statement below
            Me.Date_Combo.Visible = false
            Me.Distributor_Combo.Visible = True
            dc = Me.Distributor_Combo
            Me.Date_Combo.RowSource = "SELECT distinct COMMISSION_MONTH FROM commission_txn WHERE (payment_point = @dc)"
       Case "nilMajor_Movement"
            Me.Date_Combo.Visible = True
            Me.Distributor_Combo.Visible = False
            Me.Date_Combo.RowSource = "SELECT COMMISSION_MONTH FROM COMMISSION_MONTH WHERE (TRANSACTIONS_IMPORTED = 1)"
        End Select
    End Sub
    As the comment in case Dealer_Product_Group states I wish to do 2 things:
    1) make date_combo invisible, then visible once the user firstly selects a distributor in the distributor_combo box.

    2) Once this is done i want to pass this value into the dc variable as defined in the sub. Is this the correct way to do it? the problem is in sql to pass a variable requires the @ symbol but i cant do this in VB i.e: cant do dim @dc as variant

    The reason why i want to do this is because i want to limit the amount of dates the user inputs once a distributor has been chosen. (type of validation such that the user cant put in bogus dates based on a distributor)
    Cheers
    Champinco

  2. #2
    Frenzied Member
    Join Date
    Nov 2001
    Location
    Mass USA
    Posts
    1,674

    Re: passing variable into sql statement

    Gotta concatenate the variable to the sql string

    VB Code:
    1. Me.Date_Combo.RowSource = "SELECT distinct COMMISSION_MONTH FROM commission_txn WHERE (payment_point = '"& dc &"')"

  3. #3

    Thread Starter
    Member
    Join Date
    Nov 2006
    Posts
    55

    Re: passing variable into sql statement

    That seems to work...i.e: the code runs and i get no errors, but it does not return back the dates in that date_combo box. Am i capturing the dc variable correctly from the distributor_combo box? The query itself works and i tested it in sql analyser, i think the '" & dc & "' may not be correct or the way i declare the variable and call it? i.e: Dim dc As Variant

    Code:
    Case "Dealer_Product_Group"
            Me.Distributor_Combo.Visible = True
            Me.Distributor_Combo.SetFocus
            dc = Me.Distributor_Combo
            Me.Date_Combo.RowSource = "SELECT distinct COMMISSION_MONTH FROM commission_txn WHERE (payment_point = '" & dc & "')"
            'Me.Date_Combo.Visible = True

  4. #4
    Frenzied Member
    Join Date
    Oct 2003
    Posts
    1,301

    Re: passing variable into sql statement

    What is the value of dc when you put it in the query?
    What happens when you dim dc as a string?

  5. #5
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: passing variable into sql statement

    Moved to Database Development forum

    Variables should not be declared as Variant unless you have no other choice.

    If it is a text value, use the data type String. If it is numeric you can use Integer (whole numbers up to 32k), Long (bigger whole numbers), Single (numbers with decimal places), ....

    If your payment_point field is not text you need to remove the ' marks. (if it is a Date you should use # in Access, ' for most other systems).

  6. #6
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    Re: passing variable into sql statement

    And, as you've pointed out in other threads, Si, if it's a field holding a date, it should be a Date type field if you ever want to do comparisons (before, after, between, etc.)
    The most difficult part of developing a program is understanding the problem.
    The second most difficult part is deciding how you're going to solve the problem.
    Actually writing the program (translating your solution into some computer language) is the easiest part.

    Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.

    Please Help Us To Save Ana

  7. #7
    Member
    Join Date
    Jan 2007
    Posts
    38

    Re: passing variable into sql statement

    Hey. I am trying to do something similar in a program I am writing, but my combobox does not have a .rowsource option.

    In essence, I want to populate a combobox with data from a query that updates each time another combobox's selected index is changed.

    Any thoughts on how I would do this, why my combobox does not have the .rowsource, or what I should use instead of the .rowsource.

    I am working in VB 2005

    Thanks

  8. #8
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: passing variable into sql statement

    The code above is for VBA (basically Classic VB), so is not appropriate for you - I'm afraid I dont know how to use Comboboxes in VB.Net (which is what VB 2005 is), so you'll have to find a method somewhere else.. presumably there are several threads about it in our VB.Net forum.

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