2015-09-24 53 views
0

我有下面的過程,我得到一個對象變量與塊網絡設置調試。我已經審查過它,只是可以找到如何解決它。有什麼建議麼?Microsoft Acess VBA調試對象變量未設置塊

它調試了一半的地方,我把註釋'代碼調試直到rstMaster.EOF。

非常感謝您的幫助。

Sub AllocateImmediately() 
Dim x As Long 
Dim y As Long 
Dim z As Long 
Dim sMonths(1 To 12) As String 
Dim lAllocation As Long 
Dim iMonth As Integer 
Dim iCurrentMonth As Integer 
Dim sCurrentDLRNumber As String 
Dim iAlreadyAllocated As Integer 
Dim j As Integer 
Dim sDealerID As String 
Dim iPriorProduction As Integer 
Dim iAllocation As Integer 

'Assign Month names to be used when writing to the master table 
sMonths(1) = "January" 
sMonths(2) = "February" 
sMonths(3) = "March" 
sMonths(4) = "April" 
sMonths(5) = "May" 
sMonths(6) = "June" 
sMonths(7) = "July" 
sMonths(8) = "August" 
sMonths(9) = "September" 
sMonths(10) = "October" 
sMonths(11) = "November" 
sMonths(12) = "December" 
Dim iTotalProduction As Long 
Dim iLastMonth As Integer 
'Dim iCurrentMonth As Integer 
'Dim iLastMonth As Integer 

Dim iStartingMonth As Integer 
Dim rstAllocations As Recordset 
Dim rstMaster As Recordset 

Dim db As DAO.Database 
Dim tdf As DAO.TableDef 
Dim Fld As DAO.Field 
Dim strField As String 
Dim iProduction As Integer 
Dim k As Integer 
Dim o As Integer 
Dim i As Integer 
'Dim iAllocation As Integer 
Dim sCurrentModel As String 
Dim sSpec As String 
Dim sFilter As String 
Dim sPreviousModel As String 
Dim sPreviousSpec As String 
Dim iMonthTry As Integer 



If Not IsNull(cboStartingProductionMonth.Value) Then 'check to see starting month was selected. 
    iStartingMonth = cboStartingProductionMonth.Value 
    Else 
    MsgBox "Please use the drop down box and select the starting month.", vbCritical 
    End 
End If 

Set rstAllocations = CurrentDb.OpenRecordset("select * from Allocations order by [Model Name],[Spec]") 'Loop through allocations table for each model 



    Do Until rstAllocations.EOF 


     sCurrentModel = rstAllocations("Model Name") 
     sSpec = IIf(IsNull(rstAllocations("Spec")) Or rstAllocations("Spec") = "", "", rstAllocations("Spec")) 
     sFilter = "" 

     'At the last record 
     rstAllocations.MoveNext 
     If rstAllocations.EOF = True Then 
      rstAllocations.MovePrevious 
      Else 
      rstAllocations.MovePrevious 
     End If 

     If Not (rstAllocations.EOF) Then 
      rstAllocations.MoveNext 

      If rstAllocations.EOF Then 
       rstAllocations.MovePrevious 
       'at the last record it is either "CA" or "All" due to sort order. 
       If sPreviousModel = sCurrentModel And sSpec = "CA" Then sFilter = "CA" 
       If sFilter <> "" Then GoTo sSQLStatements 
       GoTo NextTry: 
      End If 


      If sCurrentModel = rstAllocations("Model Name") Then sFilter = "AllButCA" 'Going to be a CA model next round 
      rstAllocations.MovePrevious 
      If sFilter <> "" Then GoTo sSQLStatements 
     End If 

NextTry: 
     If Not (rstAllocations.EOF) Then 
      rstAllocations.MoveNext 

      If rstAllocations.EOF Then 
       rstAllocations.MovePrevious 
       If sPreviousModel <> sCurrentModel And sSpec = "CA" Then sFilter = "CA" 
       If sFilter <> "" Then GoTo sSQLStatements 
       GoTo NextTry2: 
      End If 

      If sCurrentModel <> rstAllocations("Model Name") And sSpec = "CA" Then sFilter = "CA" 'only a CA model needs to be filtered 
      rstAllocations.MovePrevious 
      If sFilter <> "" Then GoTo sSQLStatements 
     End If 

NextTry2: 

     If Not (rstAllocations.EOF) Then 
      rstAllocations.MoveNext 

      If rstAllocations.EOF Then 
       rstAllocations.MovePrevious 
       If sPreviousModel <> sCurrentModel And sSpec = "" Then sFilter = "ALL" 
       If sFilter <> "" Then GoTo sSQLStatements 
      End If 


      If sCurrentModel <> rstAllocations("Model Name") And (IsNull(sSpec) Or sSpec = "") Then sFilter = "ALL" 'only a CA model needs to be filtered 
      rstAllocations.MovePrevious 
      If sFilter <> "" Then GoTo sSQLStatements 
     End If 

sSQLStatements: 
     sDealerID = "" 'Reset Dealer 
     iProduction = 0 'Reset Production 
     iTotalProduction = 0 
     k = 0 
     iMonth = 0 



     sPreviousModel = sCurrentModel 
     sPreviousSpec = sSpec 


    'create recordset based on if the recordset should filter on CA, All states, or All But CA depending on Specs for a particular model. 
    Select Case UCase(sFilter) 
     Case "ALL" 
      Set rstMaster = CurrentDb.OpenRecordset("Select * from tblMaster where [New Model] = " & "'" & rstAllocations("Model Name") & "'" & " AND [Alloc Calculation]>0 order by [Alloc Calculation] desc, [Months Supply Model] ASC") 
     Case "CA" 
      Set rstMaster = CurrentDb.OpenRecordset("Select * from tblMaster where [New Model] = " & "'" & rstAllocations("Model Name") & "'" & " AND [Alloc Calculation]>0 and [STATE_NAME] ='CA' order by [Alloc Calculation] desc, [Months Supply Model] ASC") 
     Case "ALLBUTCA" 
      Set rstMaster = CurrentDb.OpenRecordset("Select * from tblMaster where [New Model] = " & "'" & rstAllocations("Model Name") & "'" & " AND [Alloc Calculation]>0 and [STATE_NAME] <>'CA' order by [Alloc Calculation] desc, [Months Supply Model] ASC") 
    End Select 


     iCurrentMonth = iStartingMonth - 1 


     'calculate total production in the allocations table for a model for all months. 
     For o = 1 To 12 
      iTotalProduction = IIf(IsNull(rstAllocations(sMonths(o))), 0, rstAllocations(sMonths(o))) + iTotalProduction 
      ''debug.print iTotalProduction, sMonths(o), rstMaster("New Model") 
     Next o 


GetAnotherMonth: 
     iMonthTry = iMonthTry + 1 
     '---Handle Months---------------Loops through months 
     iMonth = iMonth + 1 

     If iMonth = 13 And rstAllocations.EOF Then 
      Exit Do 
     ElseIf iMonth = 13 Then 
      GoTo kIsOver12: 
     End If 

     iCurrentMonth = iCurrentMonth + 1 
     If iCurrentMonth = 13 Then iCurrentMonth = 1 'Month can be greater than 
     '---Handle Months--------------- 

     k = iCurrentMonth 
     'If k > 12 Then GoTo kIsOver12: 
     iProduction = IIf(IsNull(rstAllocations(sMonths(k))), 0, rstAllocations(sMonths(k))) + iProduction 'add production that can be allocated. 

     If sDealerID <> "" Then 'move to the previous dealership that recived an allocation and move to the next dealer in line. 
      rstMaster.MoveFirst 
      rstMaster.FindFirst "[DLR_NO]= " & "'" & sDealerID & "'" 
      rstMaster.MoveNext 
      If rstMaster.EOF Then 
      rstMaster.MoveFirst 
'    Else 
'   rstMaster.MoveNext 
      End If 
     End If 

     'If iProduction = 0 Then GoTo GetAnotherMonth 


     'The Code Debugs Here 
     Do Until rstMaster.EOF 
TryToAllocateAgain: 


      If iProduction = 0 Then 'production for that month has ran out. 
       GoTo GetAnotherMonth: 
      End If 

      '--Does Dealer already have his allocated amount?-- 
      iAlreadyAllocated = 0 
      For j = 1 To 12 
       iAlreadyAllocated = rstMaster(sMonths(j) & " Allocation") + iAlreadyAllocated 
      Next j 

      If iAlreadyAllocated + iNumberPerOrder(i) > rstMaster("Alloc Calculation") Then 
       ''debug.print iAlreadyAllocated, rstMaster("Alloc Calculation") 
       GoTo NextRecord 'Only assign upto their alloc calc, don't assign another 
      End If 
      '--Does Dealer already have his allocated amount?-- 


      With rstMaster 
       sDealerID = rstMaster("DLR_No") 

       For i = 0 To iTotalNumberofModels - 1 'Match Model being assigned with the number per order 
       If rstMaster("New Model") = sBaseModel(i) Or rstMaster("New Model") = sLimitedEdition(i) Then 
        iAllocation = iNumberPerOrder(i):: Exit For 
       End If 
       Next i 


       'remove one from both iProduction and iTotalProduction 
       iProduction = iProduction - iNumberPerOrder(i) 
       iTotalProduction = iTotalProduction - iNumberPerOrder(i) 

       If iProduction < 0 Then 'if that month's production is out, add back to the itotalproduction 
       iTotalProduction = iTotalProduction + iNumberPerOrder(i) 'since inumberperorder(i) was subtracted above. 
       GoTo GetAnotherMonth 
       End If 

       .Edit 

       rstMaster(sMonths(iCurrentMonth) & " Allocation") = rstMaster(sMonths(iCurrentMonth) & " Allocation") + iNumberPerOrder(i) 
       .Update 

'    If iMonthTry <= 12 And iTotalProduction > 0 Then GoTo GetAnotherMonth 'added to loop through to make sure all months have been allocated 
'    If iMonthTry >= 12 Then iMonthTry = 0 
       '.MoveNext 
      End With 

NextRecord: 
       rstMaster.MoveNext 
       If iTotalProduction > 0 And rstMaster.EOF And iMonthTry < 12 Then 
        sDealerID = "" 
        rstMaster.MoveFirst 
        GoTo GetAnotherMonth 
       End If 
     Loop 

     'If there are left over models during a month, attempt to allocate them again until the iProduction is equal to production 
     'meaning no more could be allocated. 
     If iProduction > 0 And rstMaster.EOF And iPriorProduction <> iProduction Then 
     iPriorProduction = iProduction 
     rstMaster.MoveFirst 
     GoTo TryToAllocateAgain 
     End If 
kIsOver12: 
     ''debug.print rstAllocations("Model Name") & " " & Trim(Str(iTotalProduction)), iPriorProduction 
     iPriorProduction = 0 
     iMonthTry = 0 
     rstAllocations.MoveNext 

    Loop 


End Sub 
+3

GoTo語句來管理程序流通常被認爲不好,使調試困難。我建議你把你的例程分成更小的單獨函數。 – AVG

回答

0

這是一個奇怪的錯誤,即使你說英語。也許這將幫助:

「對象變量或隨着塊變量未設置」

,我認爲這是正確的。這意味着需要一個with-block(一個以'with'開頭的塊)。希望這會有所幫助。可能With rstMaster...End With

error (msdn)

with...end statement (msdn)

0

的錯誤很可能你的goto語句是原因。例如

此行

GoTo TryToAllocateAgain 

導致下一個可執行語句是一個在循環中。這是不允許的

同樣

GoTo TryToAllocateAgain 

跳出一個循環。

你需要重新考慮你的代碼結構。嘗試並創建一個調用所需的其他功能(與功能所需的所有數據被傳遞給它的參數),阿瑪尼過程

另外請注意,你可以在一個行中指定月份的名字,像這樣

sMonths = Split("Jan,Feb,Mar,etc", ",") 

還要考慮塊。 if語句的「嵌套」可以非常簡單。

If rstAllocations.EOF = True Then  ''' MovePrevious always done! 
     rstAllocations.MovePrevious 
     Else 
     rstAllocations.MovePrevious 
    End If 

    If Not (rstAllocations.EOF) Then  '''OPPOSITE - use else! 
     rstAllocations.MoveNext 

     If rstAllocations.EOF Then  
      rstAllocations.MovePrevious 
      'at the last record it is either "CA" or "All" due to sort order. 
      If sPreviousModel = sCurrentModel And sSpec = "CA" Then sFilter = "CA" 
      If sFilter <> "" Then GoTo sSQLStatements 
      GoTo NextTry: 
     End If 


     If sCurrentModel = rstAllocations("Model Name") Then sFilter = "AllButCA" 'Going to be a CA model next round 
     rstAllocations.MovePrevious 
     If sFilter <> "" Then GoTo sSQLStatements 
    End If 
+0

您是否覺得上述有用?如果是這樣,請舉出答案。 – HarveyFrench

相關問題