473,387 Members | 1,574 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Help with this SQL statement into VBA

108 100+
Need some help with some SQL below to use as an event for a combo box.

Select c.Coach_Id, c.coach_first_name, c.coach_second_name
from Coach c, coachrates cr
where c.coach_ID = cr.coach_ID
and cr.sport_ID = me.comboSport.value
and weekday(me.comboDate.value) in
(
Select day
from coach_Availability
and me.comboStarttime.value = time
)

me.comboDate.value = dd/mm/yy
me.comboStarttime.value = hh:mm

I have used SQL on other database packages, with MS Access syntax being different (never having to specifiy the join type) its sometimes difficult to test as I cant run this as creating this as a query (or can I, me.combo.value will just be accepted as parameters?)

Can anyone help me out?
Anyone recommend how to test SQL code that use values on form like here im using combo boxes?

Thought I could just somehow pass them in as variables.
Thanks.
Nov 24 '06 #1
28 2731
MMcCarthy
14,534 Expert Mod 8TB
What is the logic of this?

Expand|Select|Wrap|Line Numbers
  1.  
  2. WHERE cr.sport_ID = [Forms]![FormName]![comboSport]
  3. and weekday([Forms]![FormName]![comboDate]) in
  4. (
  5. Select day 
  6. from coach_Availability 
  7. WHERE [time] = [Forms]![FormName]![comboStarttime]
  8. )
  9.  
I have used SQL on other database packages, with MS Access syntax being different (never having to specifiy the join type) its sometimes difficult to test as I cant run this as creating this as a query (or can I, me.combo.value will just be accepted as parameters?)
Access does use joins INNER, LEFT and RIGHT. No join implies a full outer join.

Try this for the join...

Expand|Select|Wrap|Line Numbers
  1. SELECT c.Coach_Id, c.coach_first_name, c.coach_second_name
  2. FROM Coach c INNER JOIN coachrates cr 
  3. ON c.coach_ID = cr.coach_ID
  4.  
Nov 25 '06 #2
NeoPa
32,556 Expert Mod 16PB
What is the logic of this?

Expand|Select|Wrap|Line Numbers
  1. WHERE cr.sport_ID = [Forms]![FormName]![comboSport]
  2. and weekday([Forms]![FormName]![comboDate]) in
  3. (
  4. Select day 
  5. from coach_Availability 
  6. WHERE [time] = [Forms]![FormName]![comboStarttime]
  7. )
This is saying :
Include only those values where the (named) form's comboDate field matches any one of the values in the subquery.
The WHERE clause of the subquery, though, should probably be formulated something like
Expand|Select|Wrap|Line Numbers
  1. WHERE [time] Between [Forms]![FormName]![comboStarttime] And [Forms]![FormName]![comboEndtime]
though, assuming a time RANGE is required rather than simply those where it exactly matches the start time.
Nov 25 '06 #3
MMcCarthy
14,534 Expert Mod 8TB
This is saying :
Include only those values where the (named) form's comboDate field matches any one of the values in the subquery.
But outside of the first line how is it connected to the original query?
Nov 25 '06 #4
NeoPa
32,556 Expert Mod 16PB
It's the last part of the WHERE clause.
Does that answer your question (I'm not sure I understand it correctly)?
In the mean-time - I'll do some tests to ensure this IS valid SQL - I think it is.
Nov 25 '06 #5
MMcCarthy
14,534 Expert Mod 8TB
It's the last part of the WHERE clause.
Does that answer your question (I'm not sure I understand it correctly)?
In the mean-time - I'll do some tests to ensure this IS valid SQL - I think it is.
Maybe it's just my Brain.

The last part of the where statement says
"and where day of value in combo box is in nested select statement"

Neither of these is connected in any way that I can see to the original query so how can they affect the results?

Is it just me?

Mary
Nov 25 '06 #6
NeoPa
32,556 Expert Mod 16PB
No Mary, it's not just you.
But, I think it's valid anyway, if a little obscure to understand why.
To explain, consider the case where, due to the selection of comboStarttime, there are no valid slots available, this WOULD (correctly) have the effect that no items would be listed in the query either.

So, I suppose you could say the dependent data for the query is not just the underlying table, but also the selection in the combo boxes (It's actually quite clever really).

BTW the subquery in the WHERE clause does work.
In the design grid it shows up in the Criteria section as 'In (SELECT blah blah blah)'.
Nov 25 '06 #7
NeoPa
32,556 Expert Mod 16PB
Another way to look at it is :
SELECT blah blah blah
FROM blah
WHERE (other where clause data)
...but only show any lines at all if the comboDate and comboStarttime combination produces valid results from [coach_Availability].
Nov 25 '06 #8
MMcCarthy
14,534 Expert Mod 8TB
No Mary, it's not just you.
But, I think it's valid anyway, if a little obscure to understand why.
To explain, consider the case where, due to the selection of comboStarttime, there are no valid slots available, this WOULD (correctly) have the effect that no items would be listed in the query either.

So, I suppose you could say the dependent data for the query is not just the underlying table, but also the selection in the combo boxes (It's actually quite clever really).

BTW the subquery in the WHERE clause does work.
In the design grid it shows up in the Criteria section as 'In (SELECT blah blah blah)'.
I wasn't really worried about the use of the sub query just that it is normally used to compare a value in the original query to a value in the sub query.

I get your explanation about the query not returning any values if there is no valid day available in the sub query based on the value in the combo box. It is clever and I hope that it's what the poster intended.

Mary
Nov 25 '06 #9
NeoPa
32,556 Expert Mod 16PB
That's a good point.
Maybe you could post a confirmation of your cleverness robtyketto ;).
Nov 25 '06 #10
robtyketto
108 100+
Ive just woke up, thanks for the syntax corrections Im'm just about to try it.

The reason behind my structuring of the SQL is from techniques I used at an old job when I was writing code for the database package 'INGRES' I was trained in-house and saw this techinique used alot.

INGRES SQL was more user friendly and didnt have to specify the JOIN type thats why I struggle with the syntax in MS ACCESS.

Since then I left my job to become student.
Will post back later :)
Nov 25 '06 #11
robtyketto
108 100+
Ok, so after learning alittle more of the syntax of VBA I created this below, assigned it to an event and now get a syntax error :(

"The expression is typed incorrectly, or it is too complex to be evaluated.
For example, a numeric expression may contain too many complicated elements.
Try simplifying the expression by assigning parts of the expression to variables."

Me.comboCoach.RowSource = "Select c.Coach_Id, c.Coach_first_name, c.Coach_last_name " & _
"FROM Coach c INNER JOIN Coach_rates cr ON c.Coach_ID = cr.Coach_ID " & _
"WHERE cr.Sport_ID = " & Me.comboSport.Value & _
"AND weekday(" & "'Me.DTPicker9.Value'" & ") IN" & _
"( SELECT day_of_the_week " & _
"FROM Coach_Availability " & _
"WHERE Start_time = " & "'Me.Combostart.Value'" & ");"

Can anyone help me otu here?
Thanks !!
Nov 25 '06 #12
MMcCarthy
14,534 Expert Mod 8TB
Expand|Select|Wrap|Line Numbers
  1.  
  2. Me.comboCoach.RowSource = "Select c.Coach_Id, c.Coach_first_name, c.Coach_last_name " & _
  3. "FROM Coach c INNER JOIN Coach_rates cr ON c.Coach_ID = cr.Coach_ID " & _
  4. "WHERE cr.Sport_ID = " & Me.comboSport.Value & _
  5. '<Comment: You need a space before the AND and put single quotes into string only not around value.   You don't need the .Value.  Space after IN.>
  6. " AND weekday(" & Me.DTPicker9 & ") IN" & _
  7. "(SELECT day_of_the_week " & _ 
  8. "FROM Coach_Availability " & _
  9. '<Comment: Put single quotes into string and surround value not around value.   You don't need the .Value.>
  10. "WHERE Start_time = '" & Me.Combostart & "');"
  11.  

I hope you can follow the comments

Any questions please ask

Mary
Nov 25 '06 #13
NeoPa
32,556 Expert Mod 16PB
I had my reply all prepared and ready a while ago then I took a call and found this when I got back :(.
Mary's version has more than I found anyway. It assumes [Start_Time] is a string though. You'll need to change that if it's not.
Let us know if you have more problems.
Nov 25 '06 #14
MMcCarthy
14,534 Expert Mod 8TB
I had my reply all prepared and ready a while ago then I took a call and found this when I got back :(.
Mary's version has more than I found anyway. It assumes [Start_Time] is a string though. You'll need to change that if it's not.
Let us know if you have more problems.
Adrian is of course right, as always. :)

If start_time is of a Date/Time data type. Replace the single quotes with #.

Mary
Nov 25 '06 #15
robtyketto
108 100+
Thanks alot didnt quite follow all the comments :(
Still im learning more VBA code all the time!


Me.comboCoach.RowSource = "Select c.Coach_Id, c.Coach_first_name, c.Coach_last_name " & _
"FROM Coach c INNER JOIN Coach_rates cr ON c.Coach_ID = cr.Coach_ID " & _
"WHERE cr.Sport_ID = " & Me.comboSport & _
" AND weekday(" & #Me.DTPicker9.Value # & ") IN" & _
"( SELECT day_of_the_week " & _
"FROM Coach_Availability " & _
"WHERE Start_time =" & #Me.Combostart# & ");"
Nov 25 '06 #16
MMcCarthy
14,534 Expert Mod 8TB
Not quite ... try this:

Expand|Select|Wrap|Line Numbers
  1. Me.comboCoach.RowSource = "Select c.Coach_Id, c.Coach_first_name, c.Coach_last_name " & _
  2. "FROM Coach c INNER JOIN Coach_rates cr ON c.Coach_ID = cr.Coach_ID " & _
  3. "WHERE cr.Sport_ID = " & Me.comboSport & _
  4. " AND weekday(#" & Me.DTPicker9 & "#) IN " & _
  5. "(SELECT day_of_the_week " & _
  6. "FROM Coach_Availability " & _
  7. "WHERE Start_time =#" & Me.Combostart & "#);"
Nov 25 '06 #17
robtyketto
108 100+
Thanks for all your help, VBA code working.
I now understand the syntax :)

I have one LAST field to populate with VBA code, its a Total fee field.

Total fee = Sports rate (combosport must be populated) + coach_rate (option if combocoach populated
then if client is a member they get 25% off.

How can I do this in VBA??
The problem being I want to store results of SQl in variables and then used them to populate text field
and have no experience of doing this before.

Also when do I populate the field as user may not select a coach.
Can attach piccy of form if neccessary which shows field orders.

Here's my inital thoughts (I dont want to change my current database schema):-



Sports rates
~~~~~~~~~~~~
Area_id
Facility_id
sport_id
Sports_rate


Coach_rates
~~~~~~~~~~~
coach_id
sport_id
coach_price

SQL queries
~~~~~~~~~~~

Sports_price =
(perhaps does this after sports combo box populated ??)
Select sports_price,
from sports_rates sr
where me.comboSport.value = sr.sport_ID
and me.comboArea.value = sr.facility
and me.combofacility.value = sr.facility_ID

Coach_price =
(perhaps does this after sports combo box populated ??)
Select cr.coach_price
from coach_rates cr
Where me.comboSport.value = sr.sport_ID
and me.combocoach.value = cr.coach_ID

Where to put this code??

select member = membership_ID
from Client c
where me.comboClient.value = c.client_ID'


If Membership_ID is not null
then
fee = (sports_rice + coach_price)/
else
fee = Sports_price + coach_price
endif;
Nov 25 '06 #18
robtyketto
108 100+

Nov 25 '06 #19
MMcCarthy
14,534 Expert Mod 8TB
You will need a command button as otherwise you would have to repeat this code on an event on at least 2 if not 3 controls.

Expand|Select|Wrap|Line Numbers
  1.  
  2. Private Sub cmdTotalPrice_Click()
  3. Dim sportsPrice As Currancy
  4. Dim coachPrice As Currancy
  5. Dim member As Long
  6.  
  7.    sportsPrice = nz(DLookup("[sports_price]","sports_rates","[sport_ID]=" & Me.comboSport & _
  8.    " And [facility]=" & Me.comboArea & " And [facility_ID]=" & Me.combofacility), 0)
  9.  
  10.    coachPrice = nz(DLookup("[coach_price]","coach_rates","[sport_ID]=" & Me.comboSport & _
  11.    " And [coach_ID]=" & Me.combocoach), 0)
  12.  
  13.    member = nz(DLookup("[membership_ID]","Client","[client_ID]=" & Me.comboClient), 0)
  14.  
  15.    If member <> 0 Then
  16.       If coachPrice <> 0 Then
  17.          Me.total_fee = ((sportsPrice + coachPrice) / 100) * 75
  18.       Else
  19.          Me.total_fee = (sportsPrice / 100) * 75
  20.       End If
  21.    Else
  22.       If coachPrice <> 0 Then
  23.          Me.total_fee = sportsPrice + coachPrice
  24.       Else
  25.          Me.total_fee = sportsPrice
  26.       End If
  27.    End If
  28.  
  29. End Sub
  30.  
Nov 26 '06 #20
NeoPa
32,556 Expert Mod 16PB
You will need a command button as otherwise you would have to repeat this code on an event on at least 2 if not 3 controls.
Alternatively, and assuming that it would actually work better from the multiple events, you could put the code in a separate procedure (can even be private in the module of the form) and just call the procedure from all the relevant event procedures. I normally work from the perspective that relying on a human operator to do things the right way is asking for trouble ;).
Nov 26 '06 #21
robtyketto
108 100+
Thanks, thats a greast solution.

Im thinking of using the code in events 'after update' on combo box for sport and then again for coach.

What was the third event you were thinking of?

Thanks again, really appreciate the help, learnt alot in the last few days.
My assignment is due in on Friday and soon to be fully functional :)
Nov 26 '06 #22
NeoPa
32,556 Expert Mod 16PB
Thanks, thats a greast solution.

Im thinking of using the code in events 'after update' on combo box for sport and then again for coach.

What was the third event you were thinking of?

Thanks again, really appreciate the help, learnt alot in the last few days.
My assignment is due in on Friday and soon to be fully functional :)
I suspect Mary was just speaking generally and didn't want to go and work it out exactly. I'm sure if there are only two then you're fine.
Good luck with your assignment.
Nov 26 '06 #23
MMcCarthy
14,534 Expert Mod 8TB
I suspect Mary was just speaking generally and didn't want to go and work it out exactly. I'm sure if there are only two then you're fine.
Good luck with your assignment.
Just watch out for client (member) will have to be passed as a parameter to any funciton.
Nov 26 '06 #24
robtyketto
108 100+
Thank for the help, it fell over though at the line below:-

Runtime error '2001' you cancelled the previous operation" placen in afterupdate in comboSport.

sportsPrice = Nz(DLookup("[sport_price]", "sport_rates", "[sport_ID]=" & Me.comboSport & _
" And [facility]=" & Me.comboArea & " And [facility_ID]=" & Me.combofacility), 0)

I didnt list you the field names so I made these small changes:-
Changed the the field [sports_price] to [sport_price] and table "Sports_rates" to "Sport_rates"
Nov 26 '06 #25
NeoPa
32,556 Expert Mod 16PB
Thank for the help, it fell over though at the line below:-

Runtime error '2001' you cancelled the previous operation" placen in afterupdate in comboSport.
If it gave that error message then it didn't so much fail, as be cancelled by the operator (you).
I guess you hit Ctrl-Break on the keyboard.
Did it stop responding for a long time?
Nov 26 '06 #26
robtyketto
108 100+
Retried again (Shutdown access and restarted) definately didnt press a key and the same message appeared.

This time Ive copied all the code from the event.

Private Sub comboSport_AfterUpdate()

Dim sportsPrice As Currency
Dim coachPrice As Currency
Dim member As Long

Me.comboCoach.RowSource = "Select c.Coach_Id, c.Coach_first_name, c.Coach_last_name " & _
"FROM Coach c INNER JOIN Coach_rates cr ON c.Coach_ID = cr.Coach_ID " & _
"WHERE cr.Sport_ID = " & Me.comboSport & _
" AND weekday(#" & Me.DTPicker9 & "#) IN " & _
"(SELECT day_of_the_week " & _
"FROM Coach_Availability " & _
"WHERE Start_time =#" & Me.Combostart & "#);"


sportsPrice = Nz(DLookup("[sport_price]", "sport_rates", "[sport_ID]=" & Me.comboSport & _
" And [facility]=" & Me.comboArea & " And [facility_ID]=" & Me.combofacility), 0)

coachPrice = Nz(DLookup("[coach_price]", "coach_rates", "[sport_ID]=" & Me.comboSport & _
" And [coach_ID]=" & Me.comboCoach), 0)

member = Nz(DLookup("[membership_ID]", "Client", "[client_ID]=" & Me.ComboClient), 0)

If member <> 0 Then
If coachPrice <> 0 Then
Me.Total_fee = ((sportsPrice + coachPrice) / 100) * 75
Else
Me.Total_fee = (sportsPrice / 100) * 75
End If
Else
If coachPrice <> 0 Then
Me.Total_fee = sportsPrice + coachPrice
Else
Me.Total_fee = sportsPrice
End If
End If

End Sub
Nov 26 '06 #27
MMcCarthy
14,534 Expert Mod 8TB
There is nothing in this code to cancel an operation. Are you sure there is no other code running on this control? Check the events list in the control properties.


Retried again (Shutdown access and restarted) definately didnt press a key and the same message appeared.

This time Ive copied all the code from the event.

Private Sub comboSport_AfterUpdate()

Dim sportsPrice As Currency
Dim coachPrice As Currency
Dim member As Long

Me.comboCoach.RowSource = "Select c.Coach_Id, c.Coach_first_name, c.Coach_last_name " & _
"FROM Coach c INNER JOIN Coach_rates cr ON c.Coach_ID = cr.Coach_ID " & _
"WHERE cr.Sport_ID = " & Me.comboSport & _
" AND weekday(#" & Me.DTPicker9 & "#) IN " & _
"(SELECT day_of_the_week " & _
"FROM Coach_Availability " & _
"WHERE Start_time =#" & Me.Combostart & "#);"


sportsPrice = Nz(DLookup("[sport_price]", "sport_rates", "[sport_ID]=" & Me.comboSport & _
" And [facility]=" & Me.comboArea & " And [facility_ID]=" & Me.combofacility), 0)

coachPrice = Nz(DLookup("[coach_price]", "coach_rates", "[sport_ID]=" & Me.comboSport & _
" And [coach_ID]=" & Me.comboCoach), 0)

member = Nz(DLookup("[membership_ID]", "Client", "[client_ID]=" & Me.ComboClient), 0)

If member <> 0 Then
If coachPrice <> 0 Then
Me.Total_fee = ((sportsPrice + coachPrice) / 100) * 75
Else
Me.Total_fee = (sportsPrice / 100) * 75
End If
Else
If coachPrice <> 0 Then
Me.Total_fee = sportsPrice + coachPrice
Else
Me.Total_fee = sportsPrice
End If
End If

End Sub
Nov 27 '06 #28
NeoPa
32,556 Expert Mod 16PB
I have seen Access spontaneously cancel itself before, but never with an error message. It just stops quietly so you don't even know until you check :(.
Nov 27 '06 #29

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Pixie | last post by:
I was asked by professor to figure out the mistakes and fill in the functions for this program. I cannot figure it out and I hope someone can help me.Any input would be helpful. Thanks, PIXIE...
11
by: Scott C. Reynolds | last post by:
In VB6 you could do a SELECT CASE that would evaluate each case for truth and execute those statements, such as: SELECT CASE True case x > y: dosomestuff() case x = 5: dosomestuff() case y >...
6
by: redashley40 | last post by:
This is my first attempt in SQL and PreparedStatement I have add the PreparedStatement and I'm not to sure if I'm doing it correctly. When I do a test run on Choose 1 ,or 2 I get this error. Error...
0
by: RCapps | last post by:
When running the below SQL Query I keep getting the following error: Server: Msg 4924, Level 16, State 1, Line 1 ALTER TABLE DROP COLUMN failed because column 'ContractDef' does not exist in table...
6
by: charindal | last post by:
i am trying to create a program that is able to draw a housing plan. the user should be able to input the required meters then upon clicking a left,right,up, down button, the system draws a line in...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.