I have the following code that is causing some problems for me and I was hoping someone might be able to help. It works, sometimes, when it wants to.. and then there are other times when it processes my request once and repeats the same values over and over. What it is built to do is look into my batch table and generate a new batch number. if the max is 1 the number generated should be 2 and reports out as 2006-00002. As I had said, it works half of the time and doesn't the other half and i'm not sure why. Thanks for your help!
Code:
Public Sub AddNewWPI()
Dim db As Database
Dim rs As Recordset 'Batch Table
Dim strSQLWhere As String
Dim intNewBatch As Integer
Dim txtNewBatch As String
Dim intAddNewCount As Integer
Dim txtListNewBatch As String
Dim txtYear As String
txtYear = CStr(Format(Date, "yyyy"))
txtListNewBatch = "New WPI #s: " & vbCrLf
intAddNewCount = 0
strSQLWhere = "SELECT txtBatch FROM WPI_Batch WHERE format(dtmDateCreated, 'yyyy') = format(date(), 'yyyy')"
If IsNull(Forms!frmMainMenu!intAddNewCount) Or Forms!frmMainMenu!intAddNewCount = 0 Then
intAddNewCount = 1
Else
intAddNewCount = Forms!frmMainMenu!intAddNewCount
End If
'----------------'
'Open Batch Table'
'----------------'
Set db = CurrentDb
Set rs = CurrentDb.OpenRecordset(strSQLWhere, dbOpenDynaset)
Do Until intAddNewCount = 0
rs.MoveLast
intAddNewCount = intAddNewCount - 1
intNewBatch = rs!txtBatch * 1 + 1
Dim vNewBatch As String
vNewBatch = CStr(intNewBatch)
If Len(vNewBatch) = 1 Then
txtNewBatch = "0000" & vNewBatch
Else
If Len(vNewBatch) = 2 Then
txtNewBatch = "000" & vNewBatch
Else
If Len(vNewBatch) = 3 Then
txtNewBatch = "00" & vNewBatch
Else
If Len(vNewBatch) = 4 Then
txtNewBatch = "0" & vNewBatch
Else
If Len(vNewBatch) = 5 Then
txtNewBatch = vNewBatch
Else
End If
End If
End If
End If
End If
db.Execute "Insert Into WPI_Batch (idEmployee, txtBatch) Values (" & Forms!frmMainMenu!idEmployee & ", " & txtNewBatch & ")"
txtListNewBatch = txtListNewBatch & txtYear & "-" & txtNewBatch & vbCrLf
Forms!frmMainMenu!txtNewBatchList = txtListNewBatch
rs.Requery
Loop
DoCmd.OutputTo acOutputReport, "rptNewWPI", acFormatRTF, "NewWPI.rtf", -1
rs.Close
db.Close
End Sub