Debugging 未设置块的Microsoft Access VBA调试对象变量

Debugging 未设置块的Microsoft Access VBA调试对象变量,debugging,ms-access,vba,Debugging,Ms Access,Vba,我有下面的过程,我得到一个对象变量与块网设置调试。我已经复习过了,只知道如何解决它。有什么建议吗 它调试到一半多一点,我把注释“代码调试在Do直到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 iCur

我有下面的过程,我得到一个对象变量与块网设置调试。我已经复习过了,只知道如何解决它。有什么建议吗

它调试到一半多一点,我把注释“代码调试在Do直到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

这是一个奇怪的错误,即使你会说英语。也许这会有帮助:

对象变量或未设置块变量

我想那是对的。这意味着需要一个以“with”开头的块。希望这会有所帮助。可能是rstMaster…以


错误很可能是由goto语句引起的。乙二醇

这条线

GoTo TryToAllocateAgain
使下一个可执行语句位于循环内。这是不允许的

同样地

GoTo TryToAllocateAgain
跳出一个循环

您需要重新思考代码的结构。尝试创建一个mani过程,根据需要调用其他函数,并将函数所需的所有数据作为参数传递给它

还请注意,您可以在一行中指定月份名称,如下所示

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

管理程序流的GoTo语句通常被认为是不好的,并且使调试变得困难。我建议你把你的程序分解成更小的独立函数。你觉得上面的方法有用吗?如果是,请标记为答案。