2016-09-30 24 views
0

AMTSelect是聲明的變量變型爲GetRows的陣列錯誤嘗試從各行拉頭字段值

Rcount時是整數

我試圖從每一行拉頭字段值但我不斷收到一個錯誤,說下標超出範圍。錯誤發生在for循環中。

代碼如下:

If Contractnum <> "" Then 
    CNTRecords = "Select Count(*) from [Manual_AINs] WHERE [Manual_AINs].[Contract_Number]= '" & Contractnum & "';" 
    Set rs = CurrentDb.OpenRecordset(CNTRecords) 
    rCount = rs.Fields(0) 
    Set rs = Nothing 

     If rCount > 1 Then 
      qAMT = "Select [Dollar Amount] from [Manual_AINs] WHERE ((([Manual_AINs].[Contract_Number])='" & Contractnum & "'));" 
      Set rs = CurrentDb.OpenRecordset(qAMT) 
      AMTSelect = rs.GetRows 
      AMTSelectString = "Choose appropriate dollar amount of AIN from the selection below:" & Chr(10) & Chr(10) 


      For i = 1 To rCount 
       AMTSelectString = AMTSelectString & i & ".) " & Format(AMTSelect(0, (i - 1)), "$#,##0.00") & Chr(10) 
      Next i 
+0

它在For循環中。從AMTSelectstring開始是的,我認爲它的一些被刪除,當我複製它,但AMTSelect是一個變量。本質上AMTSelect = rs.GetRows –

+0

你能解釋最後一行中的AMTSelect(0,(i-1))嗎?你想訪問數組「AMTSelect」中的'i-1'行嗎? –

+0

是的,我不知道它會返回多少行,但我有它將從我返回的行數,所以我試圖獲得每行的第一個字段值,如果這會返回感。 –

回答

0

你沒有請求與.GetRows任何行 - 這基本上只是調用.GetRows(0),這將不會返回任何到您的記錄。

改變這一行...

AMTSelect = rs.GetRows 

...到:

AMTSelect = rs.GetRows(rCount) 

這就是說,因爲你顯然是想利用每一行的記錄,這是簡單得多:

If Contractnum <> "" Then 
    qAMT = "Select [Dollar_Amount] from [Manual_AINs] WHERE [Manual_AINs].[Contract_Number]='" & Contractnum & "';" 
    With CurrentDb.OpenRecordset(qAMT) 
     If Not .EOF Then .MoveFirst 
     AMTSelectString = "Choose appropriate dollar amount of AIN from the selection below:" & Chr(10) & Chr(10) 
     Dim i As Long 
     Do While Not .EOF 
      i = i + 1 
      AMTSelectString = AMTSelectString & i & ".) " & Format$(.Fields(0), "$#,##0.00") & Chr(10) 
      .MoveNext 
     Loop 
    End With 
End If 
+0

我標記的答案,但我很想知道外面,如果它不是從任何行拉動rCount爲什麼不工作的事實? –

+0

@DanielBell - 如果你沒有指定'.GetRows'的行數來返回,默認值爲0。 – Comintern

相關問題