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.
28 2731
What is the logic of this? -
-
WHERE cr.sport_ID = [Forms]![FormName]![comboSport]
-
and weekday([Forms]![FormName]![comboDate]) in
-
(
-
Select day
-
from coach_Availability
-
WHERE [time] = [Forms]![FormName]![comboStarttime]
-
)
-
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... -
SELECT c.Coach_Id, c.coach_first_name, c.coach_second_name
-
FROM Coach c INNER JOIN coachrates cr
-
ON c.coach_ID = cr.coach_ID
-
NeoPa 32,556
Expert Mod 16PB
What is the logic of this? - WHERE cr.sport_ID = [Forms]![FormName]![comboSport]
-
and weekday([Forms]![FormName]![comboDate]) in
-
(
-
Select day
-
from coach_Availability
-
WHERE [time] = [Forms]![FormName]![comboStarttime]
-
)
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 - 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.
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?
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.
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
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)'.
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].
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
NeoPa 32,556
Expert Mod 16PB
That's a good point.
Maybe you could post a confirmation of your cleverness robtyketto ;).
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 :)
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 !!
-
-
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 & _
-
'<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.>
-
" AND weekday(" & Me.DTPicker9 & ") IN" & _
-
"(SELECT day_of_the_week " & _
-
"FROM Coach_Availability " & _
-
'<Comment: Put single quotes into string and surround value not around value. You don't need the .Value.>
-
"WHERE Start_time = '" & Me.Combostart & "');"
-
I hope you can follow the comments
Any questions please ask
Mary
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.
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
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# & ");"
Not quite ... try this: -
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 & "#);"
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;
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. -
-
Private Sub cmdTotalPrice_Click()
-
Dim sportsPrice As Currancy
-
Dim coachPrice As Currancy
-
Dim member As Long
-
-
sportsPrice = nz(DLookup("[sports_price]","sports_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
-
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 ;).
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 :)
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.
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.
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"
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?
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
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
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 :(.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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 >...
|
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...
|
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...
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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
|
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...
|
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...
|
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,...
|
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,...
|
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...
| |