|
-
Jan 11th, 2007, 07:37 PM
#1
Thread Starter
Member
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
-
Jan 11th, 2007, 08:10 PM
#2
Frenzied Member
Re: passing variable into sql statement
Gotta concatenate the variable to the sql string
VB Code:
Me.Date_Combo.RowSource = "SELECT distinct COMMISSION_MONTH FROM commission_txn WHERE (payment_point = '"& dc &"')"
-
Jan 11th, 2007, 09:46 PM
#3
Thread Starter
Member
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
-
Jan 12th, 2007, 03:43 AM
#4
Frenzied Member
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?
-
Jan 12th, 2007, 10:07 AM
#5
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).
-
Jan 12th, 2007, 12:51 PM
#6
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
-
Jan 12th, 2007, 04:52 PM
#7
Member
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
-
Jan 12th, 2007, 05:01 PM
#8
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|