russell576
Registered User.
- Local time
- Today, 17:24
- Joined
- Sep 19, 2014
- Messages
- 14
I wrote a database for my daughter's dance school. There are two tables, one for personal details, the other for exam results. The data is separated this way because when I was given it all, I got it from two people in an Excel file.
When I programmed it, I had the Primary Key in the Master table auto-generated (an Integer) which I also used as the Primary Key in the ExamResults table. All the data manipulation is handled in code by Event Procedures.
This is the code to update the Master Table when a new pupil's details has been entered and the 'Submit' button on the form clicked:
The woman who uses it told me the other day that she can't update the exam results for any pupil she has put into the database since I gave it her - records of pupils entered by me are OK.
When I checked the code I realised that when a new record is created, there isn't a corresponding one in ExamResults.
So my question is, when is the Primary Key first generated and how do I program the form to update the new pupil's details in the ExamResults form when the Master table is first written to?
When I programmed it, I had the Primary Key in the Master table auto-generated (an Integer) which I also used as the Primary Key in the ExamResults table. All the data manipulation is handled in code by Event Procedures.
This is the code to update the Master Table when a new pupil's details has been entered and the 'Submit' button on the form clicked:
Code:
Private Sub btnSubmit_Click()
On Error GoTo btnSubmit_Error
Dim curDatabase As Database
Dim rstNameAddress As Recordset
If txtNameFirst = "" Then
MsgBox "You must enter the member's first name to proceed.", _
vbOKOnly Or vbInformation, "F L Toppers"
txtNameFirst.SetFocus
Exit Sub
End If
If txtNameLast = "" Then
MsgBox "You must enter the member's last name to proceed.", _
vbOKOnly Or vbInformation, "F L Toppers"
txtNameFirst = ""
txtNameFirst.SetFocus
Exit Sub
End If
If cbxDOBDay <> "" Then
If cbxDOBMonth = "" Then
MsgBox "There is a problem" & vbCrLf & _
"with the details you have" & vbCrLf & _
"selected for the Date of Birth." & vbCrLf & vbCrLf & _
"Please check and try again.", _
vbOKOnly Or vbInformation, "F L Toppers"
cbxDOBDay.Value = ""
cbxDOBMonth.Value = ""
txtDOBYear.Value = ""
cbxDOBDay.SetFocus
Exit Sub
End If
Else
If cbxDOBDay = "" Then
If cbxDOBMonth <> "" Then
MsgBox "There is a problem" & vbCrLf & _
"with the details you have" & vbCrLf & _
"selected for the Date of Birth." & vbCrLf & vbCrLf & _
"Please check and try again.", _
vbOKOnly Or vbInformation, "F L Toppers"
cbxDOBDay.Value = ""
cbxDOBMonth.Value = ""
txtDOBYear.Value = ""
cbxDOBDay.SetFocus
Exit Sub
End If
End If
End If
If cbxDOBDay <> "" Then
If txtDOBYear = "" Then
MsgBox "There is a problem" & vbCrLf & _
"with the details you have" & vbCrLf & _
"selected for the Date of Birth." & vbCrLf & vbCrLf & _
"Please check and try again.", _
vbOKOnly Or vbInformation, "F L Toppers"
cbxDOBDay.Value = ""
cbxDOBMonth.Value = ""
txtDOBYear.Value = ""
cbxDOBDay.SetFocus
Exit Sub
End If
Else
If cbxDOBDay = "" Then
If txtDOBYear <> "" Then
MsgBox "There is a problem" & vbCrLf & _
"with the details you have" & vbCrLf & _
"selected for the Date of Birth." & vbCrLf & vbCrLf & _
"Please check and try again.", _
vbOKOnly Or vbInformation, "F L Toppers"
cbxDOBDay.Value = ""
cbxDOBMonth.Value = ""
txtDOBYear.Value = ""
cbxDOBDay.SetFocus
Exit Sub
End If
End If
End If
Set curDatabase = CurrentDb
Set rstNameAddress = curDatabase.OpenRecordset("Master")
With rstNameAddress
.AddNew
.Fields("NameFirst").Value = txtNameFirst
.Fields("NameLast").Value = txtNameLast
.Fields("NameFull").Value = .Fields("NameLast").Value & ", " & .Fields("NameFirst").Value
.Fields("ClassName1").Value = cbxClass1
.Fields("ClassName2").Value = cbxClass2
.Fields("Address1").Value = txtAddress1
.Fields("Address2").Value = txtAddress2
.Fields("Address3").Value = txtAddress3
.Fields("TownCity").Value = txtTownCity
.Fields("Postcode").Value = txtPostcode
.Fields("PhoneHome").Value = txtPhoneHome
.Fields("MobileHome").Value = txtPhoneMobile
.Fields("EmailHome").Value = txtEmail
.Fields("ParentDetails").Value = tglParent
.Fields("ParentTitle").Value = txtParentTitle
.Fields("ParentNameFirst").Value = txtParentNameFirst
.Fields("ParentNameLast").Value = txtParentNameLast
.Fields("EmailParent").Value = txtParentEmail
.Fields("EmergencyContactName").Value = txtEmergencyContactName
.Fields("EmergencyRelationship").Value = txtEmergencyRelationship
.Fields("EmergencyContactNumber").Value = txtEmergencyContactNumber
.Fields("DOBDay").Value = cbxDOBDay
.Fields("DOBMonth").Value = cbxDOBMonth
.Fields("DOBYear").Value = txtDOBYear
If txtDOBYear <> "" Or Null Then
.Fields("DOB").Value = DateOfBirth(.Fields("DOBDay").Value, _
.Fields("DOBMonth").Value, .Fields("DOBYear").Value)
.Fields("DayDOB").Value = CInt(Day(rstNameAddress("DOB").Value))
.Fields("MonthDOB").Value = CInt(Month(rstNameAddress("DOB").Value))
If Not IsDate(txtDOB) Then
MsgBox "There is a problem" & vbCrLf & _
"with the details you have" & vbCrLf & _
"selected for the Date of Birth." & vbCrLf & vbCrLf & _
"Please check and try again.", _
vbOKOnly Or vbInformation, "F L Toppers"
cbxDOBDay = ""
cbxDOBMonth = ""
txtDOBYear = ""
cbxDOBDay.SetFocus
txtDOB.Visible = False
txtAge = ""
DoCmd.CancelEvent
Else
txtAge.Visible = True
.Fields("Age").Value = Agenow(.Fields("DOB").Value)
End If
End If
If cbxClass1 = "Chaps" Then
.Fields("ClassName1").Value = cbxClass1
.Fields("ClassName2").Value = cbxClass2
Else
.Fields("ClassName2").Value = cbxClass1
.Fields("ClassName1").Value = ""
End If
.Update
End With
rstNameAddress.Close
curDatabase.Close
Set rstNameAddress = Nothing
Set curDatabase = Nothing
MsgBox "The details for this member have been added.", _
vbOKOnly Or vbInformation, "F L Toppers"
btnReset_Click
btnSubmit_Exit:
Exit Sub
btnSubmit_Error:
MsgBox "There was a problem when submitting this form.", _
vbOKOnly Or vbInformation, "F L Toppers"
Resume btnSubmit_Exit
End Sub
When I checked the code I realised that when a new record is created, there isn't a corresponding one in ExamResults.
So my question is, when is the Primary Key first generated and how do I program the form to update the new pupil's details in the ExamResults form when the Master table is first written to?
Last edited by a moderator: