在CurrentDB.Execute或DoCmd.RunSQL中使用转换后的字符串?

时间:2015-08-31 02:36:16

标签: vba ms-access access-vba ms-access-2010 ms-access-2013

School Year有(按顺序):

ID作为主键

Description作为xxxx-xxxx格式的短文

Current Year为布尔值(一次只检查一次)

我正在创建一个按钮,通过将年份增加一个来将新年周期添加到表中。例如:2009-2010 - > 2010-2011

然而,使用我当前的代码,我正在进行字符串到int转换并返回字符串,但它不会留下来。该表附加了新数据,但数据错误。 IDCurrent 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

0 个答案:

没有答案