【发布时间】:2015-08-12 13:33:45
【问题描述】:
我有以下代码循环通过查询生成的记录集,有时,查询中的几行将返回 (0/0)。当循环通过记录集写出到 excel 时,如果查询中的行确实返回 (0/0),我在尝试访问它时收到溢出错误。我试图捕捉这个溢出错误,并将字符串“0%”分配给我的变量,而不是溢出值。有谁知道一种方法来捕捉和解决这些溢出错误?
Set qdf = CurrentDb.CreateQueryDef("Latest Estimate", sSQL)
Set dbs = CurrentDb
Set rstAnswer = dbs.OpenRecordset("Latest Estimate")
If Not (rstAnswer.EOF And rstAnswer.BOF) Then
rstAnswer.MoveFirst
Do Until rstAnswer.EOF
tempString = CStr(rstAnswer!BU)
xlSheet.Range("BA" & CStr(tempRow)).Value = tempString
tempString = CStr(rstAnswer!Program)
xlSheet.Range("BB" & CStr(tempRow)).Value = tempString
tempString = CStr(rstAnswer![EIS Date])
xlSheet.Range("BC" & CStr(tempRow)).Value = tempString
tempString = CStr(rstAnswer![Part Count])
xlSheet.Range("BD" & CStr(tempRow)).Value = tempString
tempString = CStr(rstAnswer![Current Actual Cost Index])
xlSheet.Range("BE" & CStr(tempRow)).Value = tempString
tempString = CStr(rstAnswer![LTA Index ($)])
xlSheet.Range("BF" & CStr(tempRow)).Value = tempString
tempString = CStr(rstAnswer![LTA Index (part count)])
xlSheet.Range("BG" & CStr(tempRow)).Value = tempString
tempString = CStr(rstAnswer![LCB Index])
xlSheet.Range("BH" & CStr(tempRow)).Value = tempString
tempString = CStr(rstAnswer![Drawings Released by Need Date])
xlSheet.Range("BI" & CStr(tempRow)).Value = tempString
tempString = CStr(rstAnswer![Total Drawings released vs Needed])
xlSheet.Range("BJ" & CStr(tempRow)).Value = tempString
tempString = CStr(rstAnswer![% Of Parts With Suppliers Selected])
xlSheet.Range("BK" & CStr(tempRow)).Value = tempString
tempString = CStr(rstAnswer![% POs placed vs needed])
xlSheet.Range("BL" & CStr(tempRow)).Value = tempString
'tempString = CStr(rstAnswer![UPPAP Requirement])
xlSheet.Range("BM" & CStr(tempRow)).Value = tempString
tempString = CStr(rstAnswer![Number of parts identified for UPPAP])
xlSheet.Range("BN" & CStr(tempRow)).Value = tempString
rstAnswer.MoveNext
tempRow = tempRow + 1
Loop
Else
MsgBox "There are no records in this recordset"
End If
programsAnswer.MoveNext
Loop
我尝试使用 GoTo 捕获溢出错误并将新值分配给我的 tempString 变量,但这不起作用,即使它起作用了,我实现它的方式也会很麻烦。
【问题讨论】:
-
从记录集中读取字段时是否遇到错误? tempString = CStr(rstAnswer!Program)
-
你试过 Str(rstAnswer!BU) 吗?
-
是的,特别是在可能生成 (0/0) 的行上。第一个这样做是[需要日期发布的图纸]
-
使用 Str(rstAnswer!BU) 给我一个类型不匹配错误
标签: excel vba ms-access recordset