I have a code i want to use to create "Packages". I have two tables. tblSections and tblLBCPackages.
They are related by SectionID a foreign key in tblLBCPackages.
I have a field PackageRef in tblLBCPackage. it is text field . Each year we create new packages and they are numbered like "LBC/FPL/2011_2012/001".
To help me get the last bit of the numbering, i added a sequence field . Each Financial Year, it must begin from 1.
My tblLBCPackage then have this fields: ID(auto),SectionID,PackageRef, FinancialYearID(FK to tblFinancialYears) and other fields.
I created a form with combo box, cboFinancialYear. I want to select a financial Year and be able to create the packages.
I use a record set to get the sectionIDs from tblSections , open recordset on the LBCPackage tables and add the values in the loop.
The problem is that the Sequence field is not incrementing.
Here is the code.
The part i want to increment on in the loop is
CODE
!Sequence = Nz(DMax("Sequence", "tblLBCPackage", "FinancialYearID=" & Me.cboFY.Column(0)), 0) + 1
CODE
Private Sub cmdCreate_Click()
''''''use Transactions to ensure all success or failure
''''''date 12/03/2012
Dim db As DAO.Database
Dim wks As DAO.Workspace
Dim rst As DAO.Recordset ''''recordset for sections
Dim rst1 As DAO.Recordset ''''recordset to add new packages
Dim strSQL As String '''''store IDs
Dim mySQL As String ''''''update previous packages to non current
Dim strmySQL As String ''''update contractor information
Dim strString As String
Dim I As Integer
On Error GoTo myErr
strSQL = "Select SectionID" & _
" From tblSections" & _
" Where CurrentSection=True;"
mySQL = " UpDate tblLBCPackage Set CurrentPackage=No Where FinancialYearID<>" & Me.cboFY.Column(0)
strmySQL = " Update tblPackage_Contractor set Active=No" & _
" Where PackageID In(Select PackageID From tblLBCPackage Where FinancialYearID<>" & Me.cboFY.Column(0) & ")"
If IsNull(Me.cboFY) Then
MsgBox " No Financial Year selected. Select one to proceed", vbExclamation
Exit Sub
End If
If DCount("*", "tblLBCPackage", "FinancialYearID=" & Me.cboFY.Column(0)) <> 0 Then
MsgBox " LBC Packages for this FY already created", vbCritical
Exit Sub
End If
'''initialise variables
Set wks = DBEngine.Workspaces(0)
Set db = CurrentDb()
Set rst = db.OpenRecordset(strSQL, dbOpenDynaset)
Set rst1 = db.OpenRecordset("tblLBCPackage", dbOpenDynaset)
wks.BeginTrans
rst.MoveFirst
Do Until rst.EOF
With rst1
.AddNew
!SectionID = rst!SectionID
!FinancialYearID = Me.cboFY.Column(0)
strString = Nz(DMax("Sequence", "tblLBCPackage", "FinancialYearID=" & Me.cboFY.Column(0)), 0) + 1
Debug.Print strString
!Sequence = Nz(DMax("Sequence", "tblLBCPackage", "FinancialYearID=" & Me.cboFY.Column(0)), 0) + 1
!PackageRef = "UNRA" & "/" & DLookup("Prefix", "Stations") & "/" & "LBC" & "/" & Replace(Me.cboFY.Column(1), "/", "_") & "/" & Format(!Sequence, "000")
!CreatedBy = GetCurrentUserName()
!DateCreated = Date
!CurrentPackage = True
.Update
End With
rst.MoveNext
I = I + 1 ''''count each package created
Loop
CurrentDb().Execute mySQL, 128
CurrentDb.Execute strmySQL, 128
wks.CommitTrans
MsgBox I & " LBC Packages created", vbInformation
myExit:
On Error Resume Next
rst.Close
rst1.Close
Set rst = Nothing
Set rst1 = Nothing
Set db = Nothing
Set wks = Nothing
Exit Sub
myErr:
wks.Rollback
MsgBox " Error is " & Err.Description & " Error Number is " & Err.number
Resume myExit
End Sub
''''''use Transactions to ensure all success or failure
''''''date 12/03/2012
Dim db As DAO.Database
Dim wks As DAO.Workspace
Dim rst As DAO.Recordset ''''recordset for sections
Dim rst1 As DAO.Recordset ''''recordset to add new packages
Dim strSQL As String '''''store IDs
Dim mySQL As String ''''''update previous packages to non current
Dim strmySQL As String ''''update contractor information
Dim strString As String
Dim I As Integer
On Error GoTo myErr
strSQL = "Select SectionID" & _
" From tblSections" & _
" Where CurrentSection=True;"
mySQL = " UpDate tblLBCPackage Set CurrentPackage=No Where FinancialYearID<>" & Me.cboFY.Column(0)
strmySQL = " Update tblPackage_Contractor set Active=No" & _
" Where PackageID In(Select PackageID From tblLBCPackage Where FinancialYearID<>" & Me.cboFY.Column(0) & ")"
If IsNull(Me.cboFY) Then
MsgBox " No Financial Year selected. Select one to proceed", vbExclamation
Exit Sub
End If
If DCount("*", "tblLBCPackage", "FinancialYearID=" & Me.cboFY.Column(0)) <> 0 Then
MsgBox " LBC Packages for this FY already created", vbCritical
Exit Sub
End If
'''initialise variables
Set wks = DBEngine.Workspaces(0)
Set db = CurrentDb()
Set rst = db.OpenRecordset(strSQL, dbOpenDynaset)
Set rst1 = db.OpenRecordset("tblLBCPackage", dbOpenDynaset)
wks.BeginTrans
rst.MoveFirst
Do Until rst.EOF
With rst1
.AddNew
!SectionID = rst!SectionID
!FinancialYearID = Me.cboFY.Column(0)
strString = Nz(DMax("Sequence", "tblLBCPackage", "FinancialYearID=" & Me.cboFY.Column(0)), 0) + 1
Debug.Print strString
!Sequence = Nz(DMax("Sequence", "tblLBCPackage", "FinancialYearID=" & Me.cboFY.Column(0)), 0) + 1
!PackageRef = "UNRA" & "/" & DLookup("Prefix", "Stations") & "/" & "LBC" & "/" & Replace(Me.cboFY.Column(1), "/", "_") & "/" & Format(!Sequence, "000")
!CreatedBy = GetCurrentUserName()
!DateCreated = Date
!CurrentPackage = True
.Update
End With
rst.MoveNext
I = I + 1 ''''count each package created
Loop
CurrentDb().Execute mySQL, 128
CurrentDb.Execute strmySQL, 128
wks.CommitTrans
MsgBox I & " LBC Packages created", vbInformation
myExit:
On Error Resume Next
rst.Close
rst1.Close
Set rst = Nothing
Set rst1 = Nothing
Set db = Nothing
Set wks = Nothing
Exit Sub
myErr:
wks.Rollback
MsgBox " Error is " & Err.Description & " Error Number is " & Err.number
Resume myExit
End Sub
The reason i put a string there was for testing purposes.
The problem is that it craetes all Packages with the same Reference 001.
Why is that the sequence field not incremented in the loop?
Ronald
