Firebolt
Thanks for the additional info, and thanks for the star although I have not yet provided you with a solution.
There are several solutions. Here are two...
Minimal coding - display amount owing, and use a command button to open up the employee record.
Create a function. On the menu, select "Insert" -> "Module"
Copy this code. The code may have to be tweaked to accomodate differences in table and field names.
Code:
Option Compare Database
Option Explicit
Function CalcOwing(lngStudentID As Long) As Currency
Dim dbs As DAO.Database, rst As DAO.Recordset
Dim strSQL As String
If Nz(lngStudentID, 0) > 0 Then
Set dbs = CurrentDb()
strSQL = "Select * from tblstudent where studentid = " & lngStudentID
Set rst = dbs.OpenRecordset(strSQL)
With rst
If .RecordCount Then
.MoveFirst
CalcOwing = !coursefee + !basicfee - !amountpaid
Else
CalcOwing = 0
End If
End With
rst.Close
Set rst = Nothing
dbs.Close
Set dbs = Nothing
Else
CalcOwing = 0
End If
End Function
A bit of bullet proofing
- must have a studentid
- must have a record for the studentid
If either fails, the calculation returns 0.
Amount owing is per info you provided.
I did not retrieve the value from amount_due. Although this would be another approach, I calcualte the amount based on the other fields.
Save the module - give it a meaning full name such as MyFirstModule ;-)
Now open the book payment form and make sure the Properties window is open. (From the menu, "View" -> "Properties")
Add an unbount text field. Click on the "Data" tab in the Properties window and select the ControlSource and add...
=CalcOwing([studentid])
I am assuming the
studentid exists on this form -- since you are tracking which student is buying which book.
Then add a command button and use the wizard to open the Student form and find the specific (student) record. You can recieve the payment from this screen for outstanding fees.
The advantage of the function is that you can use it anywhere - in a form, in a report, in a query as long as the studentid is available.
More code - treat the amount owing as a special "book" and apply the payment against the student table.
The first part of the solution is like the previous one -- the CalcOwing function should be created.
Create a second function, also in the MyFirstModule"...
Code:
Function ReceivePayment(lngStudentID As Long, curPayment As Currency) As Boolean
Dim dbs As DAO.Database, rst As DAO.Recordset
Dim strSQL As String, curAmountPaid As Currency
If Nz(lngStudentID, 0) > 0 Then
Set dbs = CurrentDb()
strSQL = "Select * from tblstudent where studentid = " & lngStudentID
Set rst = dbs.OpenRecordset(strSQL)
With rst
If .RecordCount Then
.MoveFirst
curAmountPaid = !amountpaid
.Edit
!amountpaid = curAmountPaid + curPayment
!amount_due = !coursefee + !basicfee - !amountpaid - curAmountPaid
.Update
ReceivePayment = True
Else
ReceivePayment = False
End If
End With
rst.Close
Set rst = Nothing
dbs.Close
Set dbs = Nothing
Else
ReceivePayment = False
End If
End Function
Previous assumptions apply.
The code updates both the amount_due and amountpaid. The amount_due is not adjusted by the payment amount, but by re-calculating the amount due based on the other three fields.
To actually call this update this function is tough to explain since I do not have details on your system where students pay for their books.
One way would be to create a "book" called "PayFees" with a variable prices that the cashier can adjust per the amount owing. Code would then call the aforementioned function with the payment amount.
One other tool.
DLookup is pretty cool.
=DLookup("[Amount_Due]", "tblStudent", "[studentid] = " &
studentid)
Where the "blue"
studentid is a value on the form.
...Moving on
A comment on the table design.
I suspect you want students to come back for more than one year, and take more than one course. You should create a "Fee" table that includes...
tblFee
FeeID - primary key
StudentID - foreign key to student table
CourseTerm
CourseCode - foreign key to course table
CourseFee - fee for course
Note that for the ...
CourseCode CourseFee
BasicFee 500.00
Access101 300.00
Access201 350.00
Access401 750.00
CourseTerm could be...
Year - 2003, 2004, 2005
Year+Season - F2003, W2004, S2004 (fall, winter, summer)
Now you can see what the fees were over several terms, etc for one student, or BasicFees for all terms, or all fees for Winter 2004.
Richard