9.8 KiB
9.8 KiB
New Process Sheet
Analysis generated on: 4/1/2025 4:04:01 PM
Record Source
Controls
Control Name | Reference |
---|---|
None | - |
VBA Code
Option Compare Database 'Use database order for string comparisons
Dim PartN$
Private Sub AddAProcShet()
Dim MainDB As Database, MainSet As Recordset
Dim Main2DB As Database, Main2Set As Recordset
Dim Main3DB As Database, Main3Set As Recordset
Dim MachNamesDB As Database, MachNamesSet As Recordset
Dim MachQDB As Database, MachQSet As Recordset
Dim MainTableDef As TableDef
Set MainDB = DBEngine.Workspaces(0).Databases(0)
Set Main2DB = DBEngine.Workspaces(0).Databases(0)
Set MachNamesDB = DBEngine.Workspaces(0).Databases(0)
Set MachQDB = DBEngine.Workspaces(0).Databases(0)
ErrMessage.Caption = " "
GoSub Open_Mains
Set MachNamesSet = MachNamesDB.OpenRecordset("MachineNames", DB_OPEN_TABLE) ' Create dynaset.
Set MachQSet = MachQDB.OpenRecordset("Machines", DB_OPEN_TABLE) ' Create dynaset.
If (Not (IsNull(Field1))) Or (Trim$(NewPartName_Parm$) = "") Then
PartN$ = Field1
If PartN$ <> "" Then
MainSet.AddNew
'MainSet!PartNumber = Left$(PartN$, 9)
'MainSet!PhantomNumber = Left$(PartN$, 9)
MainSet!PartNumber = Left$(PartN$, 11)
MainSet!PhantomNumber = Left$(PartN$, 11)
MainSet!IssueNumber = " "
MainSet!Deburr = "Within"
MainSet!PrintSize = "B"
MainSet!GrainNone = True
' mainset.IssueDate = " "
MainSet!CellDeburrTypes = "Auto"
MainSet!Warehouse = "90"
MainSet!PressBrake = 1
MainSet!CalculationStatus = 1
ef = 0
On Error GoTo DUPLICATE_rec
MainSet.Update
On Error GoTo 0
If ef = 1 Then
Exit Sub
End If
Refresh
If NewPartName_Parm$ <> "NEW" Then
MainSet.Seek "=", NewPartName_Parm$
Main2Set.Seek "=", PartN$
Main2Set.Edit
For i = 0 To MainSet.Fields.Count - 1
Debug.Print "; "; MainSet.Fields(i).name
If MainSet.Fields(i).name <> "PartNumber" Then
Main2Set.Fields(i) = MainSet.Fields(i)
End If
Next i
Main2Set!PartNumber = PartN$
'Main2Set!PhantomNumber = Left$(PartN$, 9)
Main2Set!PhantomNumber = Left$(PartN$, 11)
Main2Set!CalculationStatus = 1
Main2Set.Update
GoSub OPEN_Addnl
MainSet.MoveFirst
MainSet.Seek ">=", NewPartName_Parm$
If Not (MainSet.NoMatch) Then
Do While Trim$(MainSet!PartNumber) = NewPartName_Parm$
Main2Set.AddNew
For i = 0 To MainSet.Fields.Count - 1
Main2Set.Fields(i) = MainSet.Fields(i)
Next i
Debug.Print Main2Set!PartNumber
Debug.Print Main2Set!OpCode
Main2Set!PartNumber = PartN$
Main2Set.Update
MainSet.MoveNext
If MainSet.EOF Then Exit Do
Loop
End If
GoSub OPEN_Machs
MainSet.MoveFirst
MainSet.Seek ">=", NewPartName_Parm$
If Not (MainSet.NoMatch) Then
Do While Trim$(MainSet!PartNumber) = NewPartName_Parm$
Main2Set.AddNew
For i = 0 To MainSet.Fields.Count - 1
Main2Set.Fields(i) = MainSet.Fields(i)
Next i
Main2Set!PartNumber = PartN$
Main2Set.Update
MainSet.MoveNext
If MainSet.EOF Then Exit Do
Loop
End If
GoSub OPEN_Press
MainSet.MoveFirst
MainSet.Seek ">=", NewPartName_Parm$
If Not (MainSet.NoMatch) Then
Do While Trim$(MainSet!PartNumber) = NewPartName_Parm$
Main2Set.AddNew
For i = 0 To MainSet.Fields.Count - 1
Main2Set.Fields(i) = MainSet.Fields(i)
Next i
Main2Set!PartNumber = PartN$
Main2Set.Update
MainSet.MoveNext
If MainSet.EOF Then Exit Do
Loop
End If
GoSub OPEN_Pem
MainSet.MoveFirst
MainSet.Seek ">=", NewPartName_Parm$
If Not (MainSet.NoMatch) Then
Do While Trim$(MainSet!PartNumber) = NewPartName_Parm$
Main2Set.AddNew
For i = 0 To MainSet.Fields.Count - 1
Main2Set.Fields(i) = MainSet.Fields(i)
Next i
Main2Set!PartNumber = PartN$
Main2Set.Update
MainSet.MoveNext
If MainSet.EOF Then Exit Do
Loop
End If
GoSub Open_Mains
End If
End If
End If
Field1 = ""
ErrMessage.Caption = "Part " + PartN$ + " added"
Exit Sub
Open_Mains:
Set MainSet = MainDB.OpenRecordset("Process", DB_OPEN_TABLE) ' Create dynaset.
Set Main2Set = Main2DB.OpenRecordset("Process", DB_OPEN_TABLE) ' Create dynaset.
MainSet.Index = "PrimaryKey"
Main2Set.Index = "PrimaryKey"
Return
OPEN_Addnl:
Set MainSet = MainDB.OpenRecordset("AddnlProc", DB_OPEN_TABLE) ' Create dynaset.
Set Main2Set = Main2DB.OpenRecordset("AddnlProc", DB_OPEN_TABLE) ' Create dynaset.
MainSet.Index = "PartNumber"
Main2Set.Index = "PartNumber"
Return
OPEN_Machs:
Set MainSet = MainDB.OpenRecordset("Machines", DB_OPEN_TABLE) ' Create dynaset.
Set Main2Set = Main2DB.OpenRecordset("Machines", DB_OPEN_TABLE) ' Create dynaset.
MainSet.Index = "PartNumber"
Main2Set.Index = "PartNumber"
Refresh
Return
OPEN_Press:
Set MainSet = MainDB.OpenRecordset("PressBrakeOPs", DB_OPEN_TABLE) ' Create dynaset.
Set Main2Set = Main2DB.OpenRecordset("PressBrakeOPs", DB_OPEN_TABLE) ' Create dynaset.
MainSet.Index = "PartNumber"
Main2Set.Index = "PartNumber"
Refresh
Return
OPEN_Pem:
Set MainSet = MainDB.OpenRecordset("PemPress Ops", DB_OPEN_TABLE) ' Create dynaset.
Set Main2Set = Main2DB.OpenRecordset("PemPress Ops", DB_OPEN_TABLE) ' Create dynaset.
MainSet.Index = "PartNumber"
Main2Set.Index = "PartNumber"
Refresh
Return
DUPLICATE_rec:
ErrMessage.Caption = Error$ + " Part - " + PartN$
ef = 1
Resume Next
End Sub
Private Sub Button0_Click()
On Error GoTo Err_Button0_Click
Dim DocName As String
Dim LinkCriteria As String
PN$ = NewPartName_Parm$
DocName = PrimaryScreen$
DoCmd.OpenForm DocName, , , LinkCriteria
DoCmd.GoToControl "PartName"
DoCmd.GoToControl "PartNumber"
DoCmd.FindRecord PN$
DoCmd.SelectObject A_FORM, "New Process Sheet"
DoCmd.Close
Exit_Button0_Click:
Exit Sub
Err_Button0_Click:
MsgBox Error$
Resume Exit_Button0_Click
End Sub
Private Sub Button3_Click()
Call AddAProcShet
NewPartName_Parm$ = PartN$
Call Button0_Click
Call LogFile(PartN$, "Add Process")
End Sub
What it does
Add AProc Sheet Subroutine
Description
This VBA subroutine, AddAProcShet
, is responsible for adding new records to three different tables (Mains
, MachineNames
, and Machines
) in a database. It also updates the PartNumber
field of an existing record in the Mains
table.
Parameters
NewPartName_Parm$
: A string parameter that contains the part number of the machine being added or updated.
Flow
- The subroutine initializes several databases (
MainDB
,Main2DB
,MachNamesDB
, andMachQDB
) and recordsets (MainSet
andMachSets
). - If a new part name is provided, it creates two dynamic recordsets: one for the
MachineNames
table and one for theMachines
table. - It checks if a new part number or an empty string is provided. If so, it extracts the part number from the input string.
- If the part number is not empty, it creates a new record in the
Mains
table with default values for certain fields (e.g.,PartNumber
,PhantomNumber
, andCalculationStatus
). It then updates the existing record with the same part number if found. - The subroutine checks for duplicates based on the
PartNumber
field and performs additional actions if a duplicate is found:- If no duplicates are found, it adds new records to both the
Machines
table (Main2Set
) and theMachineNames
table (MachQSet
). - If a duplicate is found, it updates existing records in both tables with the same part number.
- If no duplicates are found, it adds new records to both the
- After adding or updating records, the subroutine calls three subroutines:
OPEN_Addnl
,OPEN_Machs
, andOPEN_Press
, which are not shown in this code snippet.
Notes
- The
Option Compare Database
line specifies that string comparisons should be performed using the database order (i.e., case-sensitive). - The
ErrMessage.Caption
line is used to clear an error message, indicating that no errors have occurred so far. - The
GoSub
statement is used to jump to a specified subroutine (Open_Mains
) at some point in the code.
Code Quality
The code is generally well-organized and follows standard VBA coding conventions. However, there are some areas for improvement:
- Comments: While there are some comments scattered throughout the code, they could be more comprehensive and provide clearer explanations of what each section does.
- Variable naming: Some variable names (e.g.,
MainDB
,MachNamesSet
) could be more descriptive or follow a consistent naming convention. - Error handling: The code uses a simple error handling mechanism (
On Error GoTo DUPLICATE_rec
), but it would be better to handle specific errors and exceptions in a more explicit way.