我有一个奇怪的错误,我的货币字段为每个条目加了$ 1。我将值存储在表中,并使用查找来获取这些值
ReducedLunch = DLookup("[Cost]", "LunchCost", "[ID]=2")
NormalLunch = DLookup("[Cost]", "LunchCost", "[ID]=1")
Milk = DLookup("[Cost]", "LunchCost", "[ID]=4")
NoLunch = DLookup("[Cost]", "LunchCost", "[ID]=5")
然后我有一些VB代码将适当的值写入数据库字段...
If (rs!TodaysLunch = "Lunch" And rs!FreeLunch = False And rs!ReducedLunch = False) Then
DailyCost = NormalLunch
rs.Edit
rs!TodaysCost = DailyCost
rs!Balance = rs!Balance - DailyCost
rs.Update
End If
如果我在Endif之前放置msgbox命令以显示DailyCost值,则此时它显示正确的值,但是当我实际将数据写回到表中时
DoCmd.RunSQL "INSERT INTO Lunch (StudentID, DateOfLunch, TypeOfLunch, Cost) SELECT [ID],[TodaysDate],[TodaysLunch],[TodaysCost] FROM Students"
TodaysCost已增加了1美元。我已经检查并仔细检查了所有内容,但找不到从何处获得此神秘值。我尝试将DailyCost设置为零(定义为货币),但它仍会写回值+ 1。
这是我完整的代码,结尾处的msgbox命令是在添加1的位置...
Dim DailyCost As Currency
Dim ReducedLunch, NormalLunch, Milk, NoLunch As Variant
Dim rs As Recordset
Dim db As Database
DailyCost = 0
Set db = CurrentDb()
Set rs = db.OpenRecordset("SELECT * FROM Students")
ReducedLunch = DLookup("[Cost]", "LunchCost", "[ID]=2")
NormalLunch = DLookup("[Cost]", "LunchCost", "[ID]=1")
Milk = DLookup("[Cost]", "LunchCost", "[ID]=4")
NoLunch = DLookup("[Cost]", "LunchCost", "[ID]=5")
Do Until rs.EOF = True
'Check for free or reduced and get price
If (rs!TodaysLunch = "Lunch" And rs!FreeLunch = True) Then
DailyCost = 0
rs.Edit
rs!TodaysCost = DailyCost
rs!Balance = rs!Balance - DailyCost
rs.Update
End If
If (rs!TodaysLunch = "Lunch" And rs!ReducedLunch = True) Then
DailyCost = ReducedLunch
rs.Edit
rs!TodaysCost = DailyCost
rs!Balance = rs!Balance - DailyCost
rs.Update
End If
If (rs!TodaysLunch = "Lunch" And rs!FreeLunch = False And rs!ReducedLunch = False) Then
DailyCost = NormalLunch
rs.Edit
rs!TodaysCost = DailyCost
rs!Balance = rs!Balance - DailyCost
rs.Update
End If
If (rs!TodaysLunch = "Milk") Then
DailyCost = Milk
rs.Edit
rs!TodaysCost = DailyCost
rs!Balance = rs!Balance - DailyCost
rs.Update
End If
If (rs!TodaysLunch = "Lunch XtraMilk") Then
ElseIf (rs!ReducedLunch = True) Then
DailyCost = ReducedLunch + Milk * 2
ElseIf (rs!FreeLunch = True) Then
DailyCost = FreeLunch + Milk * 2
ElseIf (rs!FreeLunch = False And rs!ReducedLunch = False) Then
DailyCost = NormalLunch + Milk * 2
rs.Edit
rs!TodaysCost = DailyCost
rs!Balance = rs!Balance - DailyCost
rs.Update
End If
If (rs!TodaysLunch = "No Lunch") Then
DailyCost = 0
rs.Edit
rs!TodaysCost = DailyCost
rs!Balance = rs!Balance - DailyCost
rs.Update
End If
'Set date to today
rs.Edit
rs!TodaysDate = Date
MsgBox (rs!TodaysCost) 'Point where 1 is added
rs.Update
rs.MoveNext
Loop
rs.Close
Set rs = Nothing
DoCmd.RunSQL "INSERT INTO Lunch (StudentID, DateOfLunch, TypeOfLunch, Cost) SELECT [ID],[TodaysDate],[TodaysLunch],[TodaysCost] FROM Students"
答案 0 :(得分:0)
确保“ TodaysCost”不是“长整数”。