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

时间:2015-09-24 23:47:19

标签: vba debugging ms-access access-vba

我有以下程序,我得到一个带有块网络调试的对象变量。我已经对它进行了审核,并且可以找到解决方法。有什么建议吗?

它调试了一半以上,我把评论'The Code Debugs at Do Until 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

2 个答案:

答案 0 :(得分:0)

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

&#34;对象变量或 未设置 块变量&#34;

我认为是对的。这意味着需要一个with-block(一个以&#39;以&#39;开头的块)。希望这会有所帮助。可能With rstMaster...End With

error (msdn)

with...end statement (msdn)

答案 1 :(得分:0)

错误很可能是由goto语句引起的。例如

这一行

GoTo TryToAllocateAgain

使下一个可执行语句成为循环内的一个。这是不允许的

同样

GoTo TryToAllocateAgain

跳出一个循环。

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

另请注意,您可以像这样分配一行中的月份名称

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

还要考虑阻止。 &#34;嵌套&#34; 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