S
Steven M. Britton
I am trying to do something with VBA and I am not a professional programmer.
I know there are better ways to perform these functions as well as faster...
Can some of you offer direction or correction. Thanks.
Mostly has to do with the GoTo's they aren't the best. This needs to be as
clean and fast as possible because I'll be doing 10,000+ records at one
time...
Option Compare Database
Sub LifeCycleReport()
Dim strDate, strTime, strUser, strBefore, strMM877R5 As String
DoCmd.Hourglass (True)
DoCmd.SetWarnings (False)
'Removes prior information
DoCmd.OpenQuery "qryCleanUpMM887PF5"
DoCmd.OpenQuery "qryCleanUpLife"
DoCmd.TransferText acImportFixed, "MM877PF5IMPORT", "tblMM877R5",
"C:\FILE\File.txt"
Set db = CurrentDb()
Set rs = db.OpenRecordset("tblMM877R5")
'Checks for proper report to import
rs.MoveFirst
strMM877R5 = rs!Field1
If strMM877R5 <> "MM877R5" Then
MsgBox "Not MM877R5", vbExclamation, "NOT MM877R5"
Exit Sub
End If
'Removes Blanks and Headers
DoCmd.OpenQuery "qryRemoveLines"
rs.MoveFirst
Do While Not rs.EOF
strBefore = rs!Field1
If strBefore = "Before" Then
rs.MoveNext
strDate = rs!Field2
strTime = rs!Field3
strUser = rs!Field4
rs.MovePrevious
rs.edit
rs!Field2 = strDate
rs!Field3 = strTime
rs!Field4 = strUser
rs.Update
rs.MoveNext
Else
rs.MoveNext
End If
Loop
rs.Close
Set rs = Nothing
DoCmd.OpenQuery "qryAppendLifeCycle"
Set db = CurrentDb()
Set rs = db.OpenRecordset("tblLifeCycle")
Dim strChangeType As String
Dim strBfrPart, strBfrAccy, strBfrQty, strBfrUOM, strBfrCost As String
Dim strAftPart, strAftAccy, strAftQty, strAftUOM, strAftCost As String
rs.MoveFirst
Do While Not rs.EOF
strChangeType = rs!ChangeType
If strChangeType = "Before" Then
strBfrAccy = rs!Grp_Option & ""
strBfrPart = rs!OldPartNumber & ""
strBfrQty = rs!InitialQuantity & ""
strBfrUOM = rs!UOM & ""
strBfrCost = rs!CurrentCost & ""
rs.MoveNext
strAftAccy = rs!Grp_Option & ""
strAftPart = rs!OldPartNumber & ""
strAftQty = rs!InitialQuantity & ""
strAftUOM = rs!UOM & ""
strAftCost = rs!CurrentCost & ""
rs.MovePrevious
'did part change from basic to accy or vice-versa?
If strBfrAccy <> strAftAccy Then
rs.edit: rs!ChangeType = "DELETED": rs.Update
rs.MoveNext
rs.edit: rs!ChangeType = "ADDED": rs.Update
rs.MoveNext
GoTo 400
End If
'did the part get changed?
If strBfrPart <> strAftPart Then
'did UOM not match the New UOM for this part switch?
If strBfrUOM <> strAftUOM Then
rs.edit: rs!ChangeType = "DELETED"
rs!NewPartNumber = ""
rs.MoveNext
rs.edit: rs!ChangeType = "ADDED"
rs.MoveNext
GoTo 400
End If
rs.edit: rs!ChangeType = "SWITCHED PART"
rs!NewPartNumber = strAftPart
rs!RevisedQuantity = strAftQty
rs!RevisedCost = sftAftCost
rs.MoveNext
rs.Delete
GoTo 400
End If
End If
Dim strCurrentCost, strInitialQty As String
'Copy current cost to rev cost column "DELETED" line
If strChangeType = "DELETED" Then
strCurrentCost = rs!CurrentCost
rs.edit: rs!RevisedCost = strCurrentCost
GoTo 400
End If
'Move qty to new qty and copy current cost to rev cost column
"ADDED" line
If strChangeType = "ADDED" Then
strInitialQty = rs!InitialQty
strCurrentCost = rs!CurrentCost
rs.edit: rs!InitialQty = 0
rs!RevisiedQty = strInitialQty
rs!RevisiedCost = strCurrentCost
GoTo 400
End If
400
Loop
DoCmd.Hourglass (False)
DoCmd.SetWarnings (True)
End Sub
I know there are better ways to perform these functions as well as faster...
Can some of you offer direction or correction. Thanks.
Mostly has to do with the GoTo's they aren't the best. This needs to be as
clean and fast as possible because I'll be doing 10,000+ records at one
time...
Option Compare Database
Sub LifeCycleReport()
Dim strDate, strTime, strUser, strBefore, strMM877R5 As String
DoCmd.Hourglass (True)
DoCmd.SetWarnings (False)
'Removes prior information
DoCmd.OpenQuery "qryCleanUpMM887PF5"
DoCmd.OpenQuery "qryCleanUpLife"
DoCmd.TransferText acImportFixed, "MM877PF5IMPORT", "tblMM877R5",
"C:\FILE\File.txt"
Set db = CurrentDb()
Set rs = db.OpenRecordset("tblMM877R5")
'Checks for proper report to import
rs.MoveFirst
strMM877R5 = rs!Field1
If strMM877R5 <> "MM877R5" Then
MsgBox "Not MM877R5", vbExclamation, "NOT MM877R5"
Exit Sub
End If
'Removes Blanks and Headers
DoCmd.OpenQuery "qryRemoveLines"
rs.MoveFirst
Do While Not rs.EOF
strBefore = rs!Field1
If strBefore = "Before" Then
rs.MoveNext
strDate = rs!Field2
strTime = rs!Field3
strUser = rs!Field4
rs.MovePrevious
rs.edit
rs!Field2 = strDate
rs!Field3 = strTime
rs!Field4 = strUser
rs.Update
rs.MoveNext
Else
rs.MoveNext
End If
Loop
rs.Close
Set rs = Nothing
DoCmd.OpenQuery "qryAppendLifeCycle"
Set db = CurrentDb()
Set rs = db.OpenRecordset("tblLifeCycle")
Dim strChangeType As String
Dim strBfrPart, strBfrAccy, strBfrQty, strBfrUOM, strBfrCost As String
Dim strAftPart, strAftAccy, strAftQty, strAftUOM, strAftCost As String
rs.MoveFirst
Do While Not rs.EOF
strChangeType = rs!ChangeType
If strChangeType = "Before" Then
strBfrAccy = rs!Grp_Option & ""
strBfrPart = rs!OldPartNumber & ""
strBfrQty = rs!InitialQuantity & ""
strBfrUOM = rs!UOM & ""
strBfrCost = rs!CurrentCost & ""
rs.MoveNext
strAftAccy = rs!Grp_Option & ""
strAftPart = rs!OldPartNumber & ""
strAftQty = rs!InitialQuantity & ""
strAftUOM = rs!UOM & ""
strAftCost = rs!CurrentCost & ""
rs.MovePrevious
'did part change from basic to accy or vice-versa?
If strBfrAccy <> strAftAccy Then
rs.edit: rs!ChangeType = "DELETED": rs.Update
rs.MoveNext
rs.edit: rs!ChangeType = "ADDED": rs.Update
rs.MoveNext
GoTo 400
End If
'did the part get changed?
If strBfrPart <> strAftPart Then
'did UOM not match the New UOM for this part switch?
If strBfrUOM <> strAftUOM Then
rs.edit: rs!ChangeType = "DELETED"
rs!NewPartNumber = ""
rs.MoveNext
rs.edit: rs!ChangeType = "ADDED"
rs.MoveNext
GoTo 400
End If
rs.edit: rs!ChangeType = "SWITCHED PART"
rs!NewPartNumber = strAftPart
rs!RevisedQuantity = strAftQty
rs!RevisedCost = sftAftCost
rs.MoveNext
rs.Delete
GoTo 400
End If
End If
Dim strCurrentCost, strInitialQty As String
'Copy current cost to rev cost column "DELETED" line
If strChangeType = "DELETED" Then
strCurrentCost = rs!CurrentCost
rs.edit: rs!RevisedCost = strCurrentCost
GoTo 400
End If
'Move qty to new qty and copy current cost to rev cost column
"ADDED" line
If strChangeType = "ADDED" Then
strInitialQty = rs!InitialQty
strCurrentCost = rs!CurrentCost
rs.edit: rs!InitialQty = 0
rs!RevisiedQty = strInitialQty
rs!RevisiedCost = strCurrentCost
GoTo 400
End If
400
Loop
DoCmd.Hourglass (False)
DoCmd.SetWarnings (True)
End Sub