【问题标题】:Make textbox null if empty如果为空,则使文本框为空
【发布时间】:2016-09-02 13:52:25
【问题描述】:

你好,我有一个连接到 mysql 服务器的 excel 用户表单。 一切正常,除非我将日期的文本框留空,我收到一个错误,即日期的值不正确

在进一步的研究中,我发现文本框在为空时返回零​​长度字符串,而 mysql 只允许空值。 当我点击保存按钮时,无论如何将文本框设置为空。

【问题讨论】:

标签: mysql vba excel


【解决方案1】:

这是我对解决方案的尝试,希望这会有所帮助。

基本上它是快速检查有问题的文本框是否为空,如果是,则将其设为空:

If textbox1.value = "" then
    textbox1.value = null
end if

【讨论】:

  • 我想过,我应该把这段代码加到哪里..我应该把它加到按钮点击事件中吗??
【解决方案2】:

最后一个解决方案,但如果您有 20 个日期列,这将是一个痛苦的 b。不幸的是,我有 20 个日期列..:(

这是对我来说的代码,希望这对将来的人有所帮助

Private Sub CommandButton1_Click()

设置 cn = CreateObject("ADODB.Connection") 设置 rs = CreateObject("ADODB.Recordset")

strCon = "Driver={MySQL ODBC 5.3 Unicode Driver};Server=localhost;Database=sample;" _ & "用户=root;密码=密码;选项=3;"

cn.Open strCon

如果 txt5.Value = "" 那么

SQLStr = "insert into sample.details (ID,Name,Age,Gender,Date_of_Birth) values ('" & txt1.Value & "', '" & txt2.Value & "','" & txt3.Value & "','" & txt4.Value & "',Null);"

cn.Execute SQLStr

否则

SQLStr = "insert into sample.details (ID,Name,Age,Gender,Date_of_Birth) values ('" & txt1.Value & "', '" & txt2.Value & "','" & txt3.Value & "','" & txt4.Value & "','" & Format(txt5.Value, "yyyy-mm-dd") & "');"

cn.Execute SQLStr

如果结束 结束子

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-31
    • 1970-01-01
    • 1970-01-01
    • 2014-10-03
    • 2021-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多