donbolinger
MIS
In conjunction with MAS 200 (Sage) and Access 97, we have a script that generates sales order numbers for a batch of orders that have been imported into the Access database. The code runs very slow and we are hoping to see if anything can be removed / changed to make it faster. Here is the code (followed by the Module named GetNextSONum):
=====================================================
From Access Module1:
Code:
Private Sub cmdGenerate_Click()
Dim rst As DAO.Recordset, db As DAO.Database, rstSO As DAO.Recordset, rstCust As DAO.Recordset
Dim Skipped As Boolean, SONum As String, CustNum As String, TooLong As Boolean
Dim curOrderNum As String
Skipped = False
TooLong = False
curOrderNum = vbNullString
cmdCancel.SetFocus
cmdGenerate.Enabled = False
chkYahoo.Enabled = False
chkShopAmex.Enabled = False
chkComerxia.Enabled = False
chkAmazon.Enabled = True
Set db = CurrentDb
Screen.MousePointer = 11
If chkAmazon Then
db.Execute "UPDATE [Orders-Amazon] SET [Orders-Amazon].[buyer-name] = xg_ReplaceAllWith([buyer-name],"""""""",""'"") WHERE ((([Orders-Amazon].[buyer-name]) Like ""*""""*""))"
DoEvents
Set rst = db.OpenRecordset("Orders-Amazon")
If rst.RecordCount > 0 Then
rst.MoveFirst
Do
StartOfAmazonLoop:
If curOrderNum = vbNullString Then
curOrderNum = rst![order-id]
Else
If rst![order-id] = curOrderNum Then
rst.MoveNext
GoTo StartOfAmazonLoop
Else
curOrderNum = rst![order-id]
End If
End If
Set rstSO = db.OpenRecordset("SELECT * FROM [Order Links] WHERE [Order_num] = '" & rst![order-id] & "'")
If rstSO.RecordCount <= 0 Then
SONum = GetNextSONum
'Set rstCust = db.OpenRecordset("SELECT MAS_Customers.CustomerNumber FROM MAS_Customers WHERE (((Ucase(MAS_Customers.CustomerName))=""" & UCase(rst![buyer-name]) & """) AND ((Ucase(MAS_Customers.AddressLine1))=""" & UCase(rst![ship-address-1]) & """) AND ((MAS_Customers.ZipCode)='" & rst![ship-postal-code] & "')" & IIf(IsNull(rst![ship-address-2]), vbNullString, " AND((ucase(MAS_Customers.AddressLine2))=""" & UCase(rst![ship-address-2]) & """)") & IIf(IsNull(rst![ship-address-3]), vbNullString, " AND ((Ucase(MAS_Customers.AddressLine3))=""" & UCase(rst![ship-address-3]) & """)") & ")")
'If Not rstCust.EOF Then
'CustNum = rstCust!Customernumber
'Else
'rstCust.Close
'Set rstCust = db.OpenRecordset("SELECT AR1_CustomerMaster.CustomerNumber FROM AR1_CustomerMaster WHERE (((Ucase(AR1_CustomerMaster.CustomerName))=""" & UCase(rst![buyer-name]) & """) AND ((Ucase(AR1_CustomerMaster.AddressLine1))=""" & UCase(rst![ship-address-1]) & """) AND ((AR1_CustomerMaster.ZipCode)='" & rst![ship-postal-code] & "')" & IIf(IsNull(rst![ship-address-2]), vbNullString, " AND((Ucase(AR1_CustomerMaster.AddressLine2))=""" & UCase(rst![ship-address-2]) & """)") & IIf(IsNull(rst![ship-address-3]), vbNullString, " AND ((Ucase(AR1_CustomerMaster.AddressLine3))=""" & UCase(rst![ship-address-3]) & """)") & ")")
'If Not rstCust.EOF Then
'CustNum = rstCust!Customernumber
'Else
'CustNum = GetNextCustNum
'NewCustomer = True
'End If
'End If
'rstCust.Close
db.Execute "INSERT INTO [Order Links] (MAS_num, Order_num) VALUES ('" & SONum & "','" & rst![order-id] & "')"
'If NewCustomer Then
' Set rstCust = db.OpenRecordset("MAS_Customers")
' rstCust.AddNew
' rstCust!Customernumber = CustNum
' rstCust!customername = Left(rst![buyer-name], 30)
' rstCust!addressline1 = Left(rst![ship-address-1], 30)
' rstCust!addressline2 = Left(rst![ship-address-2], 30)
' rstCust!addressline3 = Left(rst![ship-address-3], 30)
' rstCust!Zipcode = rst![ship-postal-code]
' rstCust.Update
' rstCust.Close
' DoEvents
' NewCustomer = False
' If TooLong = False Then
' If Len(rst![buyer-name]) > 30 Or Len(rst![ship-address-1]) > 30 Or Len(rst![ship-address-2]) > 30 Or Len(rst![ship-address-3]) > 30 Then
' TooLong = True
' End If
' End If
'End If
Else
Skipped = True
End If
rstSO.Close
rst.MoveNext
Loop While Not rst.EOF
End If
rst.Close
curOrderNum = vbNullString
End If
cmdGenerate.Enabled = True
chkYahoo.Enabled = False
chkShopAmex.Enabled = False
chkComerxia.Enabled = False
chkAmazon.Enabled = True
Screen.MousePointer = 0
Dim FinishedMessage As String
FinishedMessage = "Finished Generating Numbers."
If Skipped Then
FinishedMessage = FinishedMessage & " Some Orders appeared to have already had Numbers Generated for them and were skipped."
End If
If TooLong Then
FinishedMessage = FinishedMessage & " One or more orders had fields that were too long for MAS, please run the Address Manipulation Form to truncate these fields."
End If
If TooLong Or Skipped Then
MsgBox FinishedMessage, vbExclamation, "Generating Complete"
Else
MsgBox FinishedMessage, vbOKOnly, "Generating Complete"
End If
DoCmd.Close acForm, Me.Name, acSaveNo
End Sub
From Access Module1:
Code:
Public Function GetNextSONum() As String
Dim db As DAO.Database, rst1 As DAO.Recordset
Dim strNum As String
GetNextSONum = "*ERROR*"
Set db = CurrentDb
Set rst1 = db.OpenRecordset("SELECT MAX([MAS_Num]) FROM [Order Links]")
strNum = rst1.Fields(0).Value
strNum = CStr(Hex(CLng("&H" & strNum) + 1))
rst1.Close
Set rst1 = db.OpenRecordset("SELECT * FROM [Order Links] WHERE [MAS_Num] LIKE '*" & strNum & "'")
If rst1.RecordCount > 0 Then
Do
strNum = CStr(Hex(CLng("&H" & strNum) + 1))
rst1.Close
Set rst1 = db.OpenRecordset("SELECT * FROM [Order Links] WHERE [MAS_Num] LIKE '*" & strNum & "'")
Loop While rst1.RecordCount > 0
End If
rst1.Close
Set rst1 = db.OpenRecordset("SELECT * FROM [SO_03SOHistoryHeader] WHERE [SalesOrderNumber] LIKE '*" & strNum & "'")
If rst1.RecordCount > 0 Then
Do
strNum = CStr(Hex(CLng("&H" & strNum) + 1))
rst1.Close
Set rst1 = db.OpenRecordset("SELECT * FROM [SO_03SOHistoryHeader] WHERE [SalesOrderNumber] LIKE '*" & strNum & "'")
Loop While rst1.RecordCount > 0
End If
rst1.Close
If Len(strNum) < 7 Then
strNum = String(7 - Len(strNum), "0") & strNum
End If
GetNextSONum = strNum
End Function