I solved my first issue. Here is the fixed coding that properly increments the ChemicalID generation. It does create gaps if you delete a record that had been assigned a chemicalID, but I am okay with that.
Code:
Option Compare DatabaseOption Explicit
Public Function NewChemicalID() As String
Dim strChemicalID As String
strChemicalID = Nz(DMax("ChemicalID", "tblChemicalID"), 0)
If strChemicalID <> "" Then
NewChemicalID = Left(strChemicalID, 5) & Format(Right(strChemicalID, 4) + 1, "0000")
Else
NewChemicalID = Year(Date) & "-0001"
End If
End Function
There had been an extra If statement that appeared to just be a duplication of NewChemicalID=Year(Date) & "-0001" that June7's code had a comment that it was needed to generate new increment of -0001 for a new year. Where should that line have been June7? Because I think the coding as written will change the Year to 2014, but keep the numbering increasing from the last 2013 ChemicalID (i.e. 2014-0987 instead of 2014-0001)?
The second problem is the looping code is not doing anything that I can notice. It doesn't error out, but it does not create any number of records based on the value in txtQuantity. I am not sure how to tackle that code:
Code:
Private Sub cmdAddAnotherChemical_Click()Dim strChemicalID As String
'search for aborted ChemicalID and use it, else if none then NewChemicalID
strChemicalID = Nz(DLookup("ChemicalID", "tblChemicalID"), "")
DoCmd.SetWarnings False
strChemicalID = NewChemicalID()
If strChemicalID <> "" Then
DoCmd.RunCommand acCmdSaveRecord
For i = 1 To Me.txtQuantity - 1
DoCmd.RunSQL "INSERT INTO tblChemicalID(chemicalID, [Received Date], [Catalog Number]) VALUES ('" & NewChemicalID() & "', #" & Date & "# ,'" & Me.Catalog_Number & "')"
Next
End If
DoCmd.SetWarnings True
End Sub
Thoughts would be greatly appreciated. Thanks!