【问题标题】:Insert data into MS Access table via VBA inputbox通过 VBA 输入框将数据插入 MS Access 表
【发布时间】:2020-12-18 01:54:51
【问题描述】:

我刚接触并尝试使用输入框功能将日期插入名为“Data l Business Date”的表中。下面是我的代码,但我不断收到插入语句的语法错误。

Function BusinessDate()

Dim StrSQL As String
StrSQL = ("INSERT INTO Data l Business Date ([T_Date]) SELECT '" & InputBox("Enter todays date") & "';")
DoCmd.RunSQL StrSQL

End Function

【问题讨论】:

标签: vba ms-access


【解决方案1】:

您必须验证和格式化日期值:

Function BusinessDate()

    Dim StrSQL As String
    Dim InputText As String
    Dim InputDate As Date
   
    InputText = InputBox("Enter todays date")
    If IsDate(InputText) Then
        InputDate = DateValue(InputText)
        StrSQL = "INSERT INTO [Data l Business Date] ([T_Date]) VALUES (#" & Format(InputDate, "yyyy\/mm\/dd") & "#);"
        CurrentDb.Execute StrSQL
    End If

End Function

【讨论】:

    【解决方案2】:

    在访问中你应该使用类似的东西

    "INSERT INTO tblName (fldName) VALUES ( '" & InputBox("Enter todays date") & "' )"
    

    你的函数应该是这样的

    Function BusinessDate()
    
    Dim StrSQL As String
    StrSQL = "INSERT INTO tblName (fldName) VALUES ( '" & InputBox("Enter todays date") & "' )"
    DoCmd.RunSQL StrSQL
    
    End Function
    

    延伸阅读Insert Into

    PS如果表名中有空格,需要使用[...],即

    "INSERT INTO [tbl name] (fldName) VALUES ( '" & InputBox("Enter todays date") & "' )"
    

    【讨论】:

    • 谢谢!我一直收到相同的错误消息,但是当我更改表名时它起作用了。感谢您的帮助 Function BusinessDate() Dim StrSQL As String StrSQL = "INSERT INTO Business_Date (T_Date) VALUES ( '" & InputBox("Enter todays date") & "' )" DoCmd.SetWarnings False DoCmd.RunSQL StrSQL DoCmd.SetWarnings True结束函数
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-05
    相关资源
    最近更新 更多