表School Year
有(按顺序):
ID
作为主键
Description
作为xxxx-xxxx格式的短文
Current Year
为布尔值(一次只检查一次)
我正在创建一个按钮,通过将年份增加一个来将新年周期添加到表中。例如:2009-2010 - > 2010-2011
然而,使用我当前的代码,我正在进行字符串到int转换并返回字符串,但它不会留下来。该表附加了新数据,但数据错误。 ID
和Current Year
是准确的,但Description
实际上进行了数学计算并变为-1
。 (如2010-2011 = -1)。然而! msgBox显示实际上显示了全年字符串。
我将如何解决此问题?提前谢谢。
这是我的代码:
Private Sub NewYear_Click()
Dim iYear As Integer
Dim sDesc As String
Dim iDesc1 As Integer
Dim iDesc2 As Integer
iYear = DCount("ID", "School Year") 'row count for primary ID number
sDesc = DLookup("Description", "School Year", "[ID] =" & iYear) 'grabbing string
iDesc1 = CInt(Right$(Left(sDesc, 4), 4)) + 1 'increasing year by one
iDesc2 = CInt(Right$(Right(sDesc, 4), 4)) + 1
sDesc = iDesc1 & "-" & iDesc2
CurrentDb.Execute "UPDATE [School Year] SET [Current Year] = False" 'turning off all current year
Dim TestString As String 'did this in hope it would fix the problem, it didn't
TestString = "INSERT INTO [School Year] VALUES (" & iYear + 1 & ", " & sDesc & ", True)"
CurrentDb.Execute TestString
MsgBox "School Year is set to " & sDesc
End Sub