Yup, kicked myself, but glad to know I was very close! I did a little more tweaking to get the code running the way I need (added a vbOkCancel msgbox). The code runs smoothly now.
SOLVED
Code:
Private Sub NoOfSamples_AfterUpdate()
Dim dbs As DAO.Database
Dim rsMycoData As DAO.Recordset
Dim strSQL1 As String
'Open pointer to current database
Set dbs = CurrentDb()
'SQL string to select records from [tblMycoData] where the [AccessionNo] & _
is the same as on the form [frmSampleLogIn01]
strSQL1 = "SELECT * FROM tblMycoData WHERE tblMycoData.AccessionNo = " & _
Forms!frmSampleLogIn01!AccessionNo
'Create recordset based on SQL1
Set rsMycoData = dbs.OpenRecordset(strSQL1)
'To check if the recordset on the subform [subMycoData] is empty
'then insert sequential values into [SampleNo], or else delete
'recordset before putting in new sequential values into [SampleNo]
Dim intSmplNo As Integer
Dim strSQL2 As String
intSmplNo = Forms!frmSampleLogIn01!NoOfSamples.Value
'If Me.subMycoData.Form.Recordset.RecordCount = 0 Then
If rsMycoData.RecordCount = 0 Then
'To autopopulate the [SampleNo] field in the subform with
'sequential numbers up to the value in the [NoOfSamples] field in the main form.
If Me.Dirty Then Me.Dirty = False
For i = 1 To intSmplNo
strSQL2 = "INSERT INTO tblMycoData (AccessionNo, SampleNo) Values(" & Forms!frmSampleLogIn01!AccessionNo & ", " & i & ")"
CurrentDb.Execute strSQL2, dbFailOnError
Next i
Me.subMycoData.Form.Requery
Else
Dim MsgResponse As Integer
MsgResponse = MsgBox("Changing the number of samples will delete" & vbCrLf & " all records on the subform.", vbOKCancel)
If MsgResponse = vbOK Then
'Delete the records on the subform [subMycoData] that are & _
associated with the [AccessionNo] field that links the main & subforms
Do Until Me.subMycoData.Form.Recordset.EOF
Me.subMycoData.Form.Recordset.Delete
Me.subMycoData.Form.Recordset.MoveNext
Loop
'All records in the subform should be deleted before continuting code below
If Me.Dirty Then Me.Dirty = False
For i = 1 To intSmplNo
strSQL2 = "INSERT INTO tblMycoData (AccessionNo, SampleNo) Values(" & Forms!frmSampleLogIn01!AccessionNo & ", " & i & ")"
CurrentDb.Execute strSQL2, dbFailOnError
Next i
Me.subMycoData.Form.Requery
Else
Exit Sub
End If
End If
'To close the recordset
rsMycoData.Close
End Sub
Another question (should I should start a new thread for this quesion?): I noticed that I have redundant code in the procedure above:
Redundant code highlighted in red:
Code:
Private Sub NoOfSamples_AfterUpdate()
Dim dbs As DAO.Database
Dim rsMycoData As DAO.Recordset
Dim strSQL1 As String
'Open pointer to current database
Set dbs = CurrentDb()
'SQL string to select records from [tblMycoData] where the [AccessionNo] & _
is the same as on the form [frmSampleLogIn01]
strSQL1 = "SELECT * FROM tblMycoData WHERE tblMycoData.AccessionNo = " & _
Forms!frmSampleLogIn01!AccessionNo
'Create recordset based on SQL1
Set rsMycoData = dbs.OpenRecordset(strSQL1)
'To check if the recordset on the subform [subMycoData] is empty
'then insert sequential values into [SampleNo], or else delete
'recordset before putting in new sequential values into [SampleNo]
Dim intSmplNo As Integer
Dim strSQL2 As String
intSmplNo = Forms!frmSampleLogIn01!NoOfSamples.Value
'If Me.subMycoData.Form.Recordset.RecordCount = 0 Then
If rsMycoData.RecordCount = 0 Then
'To autopopulate the [SampleNo] field in the subform with
'sequential numbers up to the value in the [NoOfSamples] field in the main form.
If Me.Dirty Then Me.Dirty = False
For i = 1 To intSmplNo
strSQL2 = "INSERT INTO tblMycoData (AccessionNo, SampleNo) Values(" & Forms!frmSampleLogIn01!AccessionNo & ", " & i & ")"
CurrentDb.Execute strSQL2, dbFailOnError
Next i
Me.subMycoData.Form.Requery
Else
Dim MsgResponse As Integer
MsgResponse = MsgBox("Changing the number of samples will delete" & vbCrLf & " all records on the subform.", vbOKCancel)
If MsgResponse = vbOK Then
'Delete the records on the subform [subMycoData] that are & _
associated with the [AccessionNo] field that links the main & subforms
Do Until Me.subMycoData.Form.Recordset.EOF
Me.subMycoData.Form.Recordset.Delete
Me.subMycoData.Form.Recordset.MoveNext
Loop
'All records in the subform should be deleted before continuting code below
If Me.Dirty Then Me.Dirty = False
For i = 1 To intSmplNo
strSQL2 = "INSERT INTO tblMycoData (AccessionNo, SampleNo) Values(" & Forms!frmSampleLogIn01!AccessionNo & ", " & i & ")"
CurrentDb.Execute strSQL2, dbFailOnError
Next i
Me.subMycoData.Form.Requery
Else
Exit Sub
End If
End If
'To close the recordset
rsMycoData.Close
End Sub
Would it be proper to put this code into different sub in a module, such as:
Code:
Sub AutoPopulate_subMycoData()
'To autopopulate the [SampleNo] field in the subform with
'sequential numbers up to the value in the [NoOfSamples] field in the main form.
Dim intSmplNo As Integer
Dim strSQL2 As String
intSmplNo = Forms!frmSampleLogIn01!NoOfSamples.Value
If Me.Dirty Then Me.Dirty = False
For i = 1 To intSmplNo
strSQL2 = "INSERT INTO tblMycoData (AccessionNo, SampleNo) Values(" & Forms!frmSampleLogIn01!AccessionNo & ", " & i & ")"
CurrentDb.Execute strSQL2, dbFailOnError
Next i
Me.subMycoData.Form.Requery
End Sub
and then call this sub in the other procedure when needed, using:
Code:
Call AutoPopulate_subMycoData
The reasoning I have for doing this is to reduce the redundant code (normalize it?) and to call it when it is needed, thus tidying up the first procedure.