View Full Version : Problem with Append Query
Tanya 01-09-2009, 03:49 PM Hi I have a commandbutton which invokes an append query. The idea is when I add a student to a class the student is allocated a number of units, elements and competencies which they need to become competent in. I could run the append query for the whole class without paramater for studentclassid but then the query would append all students in the class even if they had already been appended.
Here is the code:
CurrentDb.Execute ("qryAppendUnitstoStudentClass"), dbFailOnError
I get the following error:
Run-time error '3061' Too few paramaters expected 2
When I tried the openquery method no rows were appended, however there were no errors:
'DoCmd.OpenQuery "qryAppendUnitstoStudentClass"
'DoCmd.Close acDefault, "qryAppendUnitstoStudentClass"
My sql skills are very poor but here is a copy of the append query in sql:
INSERT INTO tblCompetency ( StudentClassID, SubjectID, UnitID, ElementID, PerformanceID )
SELECT tblStudentsClasses.StudentClassID, tblSubjects.SubjectID, tblUnit.UnitID, tblElements.ElementID, tblPerformanceCriteria.PerformanceID
FROM ((tblSubjects INNER JOIN tblUnit ON (tblSubjects.SubjectID = tblUnit.SubjectID) AND (tblSubjects.SubjectID = tblUnit.SubjectID)) INNER JOIN (tblClasses INNER JOIN (tblStudents INNER JOIN tblStudentsClasses ON tblStudents.StudentID = tblStudentsClasses.StudentID) ON tblClasses.ClassID = tblStudentsClasses.ClassID) ON tblSubjects.SubjectID = tblClasses.SubjectID) INNER JOIN (tblElements INNER JOIN tblPerformanceCriteria ON tblElements.ElementID = tblPerformanceCriteria.ElementID) ON tblUnit.UnitID = tblElements.UnitID
WHERE (((tblStudentsClasses.StudentClassID)=[forms]![frmAddStudentstoClass]![StudentClassID]) AND ((tblSubjects.SubjectID)=[Forms]![frmClasses]![SubjectID]));
Grateful for any support.
regards
Tanya
Tanya 01-09-2009, 04:41 PM I have been fiddling with the append query and have found I could do away with a couple of the tables, here is what I have come up with:
INSERT INTO tblCompetency ( StudentClassID, SubjectID, UnitID, ElementID, PerformanceID )
SELECT tblStudentsClasses.StudentClassID, tblClasses.SubjectID, tblUnit.UnitID, tblElements.ElementID, tblPerformanceCriteria.PerformanceID
FROM tblClasses INNER JOIN tblStudentsClasses ON tblClasses.ClassID = tblStudentsClasses.ClassID, tblUnit INNER JOIN (tblElements INNER JOIN tblPerformanceCriteria ON tblElements.ElementID = tblPerformanceCriteria.ElementID) ON tblUnit.UnitID = tblElements.UnitID
WHERE (((tblStudentsClasses.StudentClassID)=[forms]![frmAddStudentstoClass]![StudentClassID]) AND ((tblClasses.SubjectID)=[forms]![frmClasses]![ClassID]));
Now.. when I run the query with a fictitious studentclassID as though I am taking the the next autonumber I append '0' rows which is the exact problem I have been having with the following code:
'DoCmd.OpenQuery "qryAppendUnitstoStudentClass"
'DoCmd.Close acDefault, "qryAppendUnitstoStudentClass"
Which makes me think, it has something to do with the fact that I am dealing with a new record that hasn't been saved yet.
Any thoughts anyone?
RainLover 01-09-2009, 05:07 PM Tanya
Let's fix the Parameters problem first.
What is happening is that your VBA Code cannot Evaluate the Values located on the Form. For some reason it simply can't see the values.
So place the Following Code in a Module.
Public Function fRunQuery(NameOfQuery)
On Error GoTo Err_Func
Dim qdf As querydef
Dim prm As Parameter
Set qdf = CurrentDb.QueryDefs(NameOfQuery)
For Each prm In qdf.Parameters
prm.Value = Eval(prm.Name)
Next prm
qdf.Execute dbFailOnError
Exit_Func:
Exit Function
Err_Func:
Select Case Err.Number
Case 3265
MsgBox "No query of this name exists.", vbCritical + vbOKOnly, _
"Query Not Found..."
Case 2450
MsgBox "The form needed to run this query is not open.", vbCritical + vbOKOnly, _
"Form Not Open..."
Case Else
MsgBox Err.Number & " - " & Err.Description
End Select
Resume Exit_Func
End Function
Then to run the query use
fRunQuery "qryAppendUnitstoStudentClass"
Hope this helps in part.
Tanya 01-09-2009, 05:39 PM Hi
Thank you for your input. I placed the code as you said in module1 and added the code fRunQuery "qryAppendUnitstoStudentClass" and nothing happened. It was like there was no code attached to the commandbutton.
Any thoughts?
Regards
Tanya
Tanya 01-09-2009, 05:44 PM Here is a copy of the db, perhaps it will help.
From frmClasses, there is a commandbutton 'Add Students' which opens another form frmAddStudentstoclass which has the commandbutton in question.
cheers
Tanya
RainLover 01-09-2009, 05:46 PM Tanya
I assume you replaced your code
CurrentDb.Execute ("qryAppendUnitstoStudentClass"), dbFailOnError
with
fRunQuery "qryAppendUnitstoStudentClass"
If you did and nothing happened then you have fixed the Parameter problem. As in you did not get an error message. If you want to prove that the code is working place a temporary MsgBox after the code.
Just noticed that you posted a copy. I will have a look.
Tanya 01-09-2009, 05:51 PM Yes, I did replace the code, and as you suggested, placed a temporary message after
fRunQuery "qryAppendUnitstoStudentClass"
and the message appeared without error.
which is really really weird
Tanya 01-09-2009, 05:52 PM Do you think it is because the form is in new record mode?
RainLover 01-09-2009, 06:00 PM Tanya
I looked at the query. It has Criteria that looks at frmAddStudentstoClass.
So I opened that Form and it has no records. This is because you have the Properties in the > Data tab > Data Entry set to yes. This causes the form to always open to a new record with no data.
There must be data for the criteria.
RainLover 01-09-2009, 06:01 PM You answered the question for yourself before I finished replying.
RainLover 01-09-2009, 06:03 PM Yes, I did replace the code, and as you suggested, placed a temporary message after
fRunQuery "qryAppendUnitstoStudentClass"
and the message appeared without error.
which is really really weird
It looks like the code is working but the query has no records.
RainLover 01-09-2009, 06:06 PM Tanya
You can view the results of your query to see what is going to happen.
Open the Query in design View then Change the view to Datasheet. This will not update anything just allows you to see the results.
Tanya 01-09-2009, 07:47 PM Hi
I have changed the view to datasheet as you suggested and it works fine with studentid where the number exists. However when I use the next number i.e. for a new record the result is blank. Which again leads me to believe it must have something to do with the form being in new record mode?
Any suggestions on another method of achieving the same outcome would be greatly appreciated.
Kind regards
Tanya
Tanya 01-09-2009, 08:02 PM Perhaps I need to tacktle this a new way...
I am thinking of creating a tickbox which when selected runs the append query, the tick box would then indicate to me that I have appended the outcomes to the student, thereby completing enrolment.
I will play with this idea for now.
RainLover 01-09-2009, 08:15 PM Tanya
You need to look at this append Query. What you have created is know as a
Cartesian Query.
I think you need to add the table tblSubjects.
To see what I mean save the query as a standard Select Query. then open it. It will show you every possible combination available between the two sets of queries.
Then add the table tblSubjects then check the new results.
When you are ready please post an update of where you are up to.
EDIT
Remove the Criteria first.
Tanya 01-09-2009, 08:16 PM Ok.. here is the update,
I have created a yes/no field and an afterupdate event:
Private Sub chkEnrolled_Click()
DoCmd.OpenQuery "qryAppendUnitstoStudentClass"
DoCmd.Close acDefault, "qryAppendUnitstoStudentClass"
End Sub
problem now is it is not finding the studentclassID [NB: I have since added it to the datasource], therefore I am presuming updating the yes/no field for a particular record does not select the whole record, hence my new delema.
so.. new question, how do I select record with an event that I can use the data in other fields?
I hope this makes sense?
Cheers
Tanya
RainLover 01-09-2009, 08:23 PM As a by the way.
Do not use spaces in Names. Also do not use strange characters like %
Also please check my edit on my previous post
Tanya 01-09-2009, 08:30 PM I did as you said, and I remembered to remove the paramaters.
The result was:
without subject table; all subjects
with subject table; only subject 1
By my way of thinking the result without tblsubjects was accurate.
Please excuse me, but I don't quite follow what you are trying to achieve?
RainLover 01-09-2009, 08:35 PM It therefore would appear that my thing was incorrect.
If you are happy with the results then lets proceed along those lines.
RainLover 01-09-2009, 09:20 PM Tanya
In your Query qryAppendUnitsToStudenClass you have in the second field StudentID where it is equal to ClassID
I think this may be incorrect
Tanya 01-09-2009, 09:34 PM ? I can't see this in the version I uploaded or my current copy???
RainLover 01-09-2009, 09:59 PM Your upload failed.
Tanya 01-09-2009, 10:04 PM This is the db up to the moment.
Thank you for your time on this, I really appreciate it.
Regards
Tanya
Tanya 01-09-2009, 11:03 PM Success!! At last :)
I created a new field on with yes/no tick box and attached an event to open form readonly to run command to append the studentclassID. Finally........
Thank you for all your time and effort, it is greatly appreciated.
Kind Regards
Tanya
RainLover 01-10-2009, 12:34 AM Glad to hear of your success.
RainLover 01-10-2009, 05:33 PM Tanya
I don't know if you want more help or not, but out of interest I downloaded your file and it does not work.
If you are trying to add a Student to the list that appears on your form the attached copy does this. It is untidy but it works.
Tanya 01-10-2009, 05:40 PM Hi
My apperoach changed, because of the problems I was having, I went back to simply selecting a new student from combobox on subfrmAttClassList, then an event on click for Enroll to open the form frmAddunitstostudent which has a commandbutton to run the append query.
I will look at your version though. Thanks..
Tanya 01-10-2009, 05:42 PM OOps Looks like I uploaded the wrong copy. Sorry
RainLover 01-10-2009, 05:43 PM I attached the wrong file
edit
Looks like we both did
Tanya 01-10-2009, 05:47 PM This version is up to date. I have been working on another problem all morning.
A while ago I was working on attendance totals and didn't realise I had messed up another part of the process, so back to square one. At the moment I am trying to get studentClassID in frmqryAttendancesub2 to cooperate :) when I select a student from SubfrmAttClassList
Tanya 01-10-2009, 05:54 PM Thanks for looking at this problem, but you haven't seen the whole picture. What I was hoping to do was add a new student AND then run the append query from one command. Your copy doesn't append the student, at least the append part didn't work.
Thats ok though, as I mentioned earlier my latest version of this achieves this through two steps, one adding new student from dropdown, then selecting Enroll runs event to open next form with command to run query.
I really appreciate your efforts, I'm sorry I didn't make my problem a little clearer for you to understand.
Cheers
Tanya
RainLover 01-10-2009, 06:40 PM Hope this helps.
See attached
Tanya 01-11-2009, 01:03 AM Your code works well for adding a new student but it doesn't run the append query. That is ok, as I said, I finally managed to work it all out in the end.
I really do appreciate your time on this.
Thanks
|