I looked at your dB and noticed there are things you should fix:
Shouldn't use spaces, special characters (exception is the underscore) or punctuation on object names
In table "Model",
"Part#" - has the hash mark. Better would be "PartNum"
In table "Complaint Database", (has space):
"City,State" - has a comma. Better would be "CityState" or "City_State"
"Lot/SerialNum" - has a slash. Better would be "LotSerialNum" or "Lot_SerialNum"
"CAPA#" - has the hash mark. Better would be "CAPANum"
"Description" (in table "Model") and "Index" (in table "Model") are reserved words in Access and shouldn't be used for object names.
"Description" is not very descriptive - I would use "PartDesc"
Instead of "Index", I would use "ComplaintID_PK".
About modules:
The top two lines in EVERY module should be
Code:
Option Compare Database
Option Explicit
-------------------------------------------------
I couldn't get the DMax() function to return a value (many problems), so I wrote a UDF.
Here is the code:
Code:
Private Function fnCreateComplaintNum() As String
Dim d As DAO.Database
Dim r As DAO.Recordset
Dim sSQL As String
Dim sComplaintNum As String
Set d = CurrentDb
'define query
sSQL = "SELECT Max(Right([ComplaintNum],3)) AS MaxNum"
sSQL = sSQL & " FROM [Complaint Database]"
sSQL = sSQL & " GROUP BY Left([ComplaintNum],2)"
sSQL = sSQL & " HAVING Left([ComplaintNum],2) = " & Format(Date, "yy") & ";"
'Debug.Print sSQL
Set r = d.OpenRecordset(sSQL)
'check for records
If Not r.BOF And Not r.EOF Then
'record found
sComplaintNum = Format(Date, "yy") & "-" & Format(Val(Nz(r("MaxNum"), "0")) + 1, "000")
Else
'record not found
sComplaintNum = Format(Date, "yy") & "-" & "001"
End If
fnCreateComplaintNum = sComplaintNum
'clean up
r.Close
Set r = Nothing
Set d = Nothing
End Function
To use it you would have:
Code:
Private Sub Form_BeforeInsert(Cancel As Integer)
Me.ComplaintNumfield = fnCreateComplaintNum
End Sub
I think it would be better to use the "Form_BeforeUpdate" event rather than the "Form_BeforeInsert" event.
And, I modified the complaint form. I added a tab control, moved controls to their own tab and shortened the form........
I am attaching the dB, but I'm not sure that you will be able to open it because I have A2010 on this confuser; I have A2K at home.