vb6中datagrib如何在录入数据的时候做到检查数据的正确性

来自外太空的你 2014-02-07 10:23:40
如题我要在datagrib更新数据库中的数据的时候,当我要更新的瞬间,如何去检查我所输入的内容的正确性,例如我对这列的限制是数字,但是我录入的是a,就会提示出错,并清空,!
谢谢大师指点!
...全文
194 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
Private Sub DataGrid1_BeforeColUpdate(ByVal ColIndex As Integer, OldValue As Variant, Cancel As Integer) 时间 = DataGrid1.Columns("入账日期").Value If 时间 = 1 Or 时间 = 2 Or 时间 = 3 Or 时间 = 4 Or 时间 = 5 Or 时间 = 6 Or 时间 = 7 Or 时间 = 8 Or 时间 = 9 Or 时间 = 10 Or 时间 = 11 Or 时间 = 12 Then If (时间 < 服务器月 And 时间 >= 服务器月 - 1) Or (时间 = 12 And 服务器月 = 1) Then 核对日期 = 服务器年 & "-" & 时间 & "-" & "1" 单号 = DataGrid1.Columns("订单编号").Value sql3 = "update 财务应收 set 入账日期 ='" & 核对日期 & "' where 订单编号='" & 单号 & "'" cn.Open strCn rs.Open sql3, cn cn.Close MsgBox "已经完成【" & Trim(单号) & "】对账,账目进入月份为" & 服务器年 & "." & 时间 & "月", vbOKOnly, "提示信息" Cancel = True Adodc1.Refresh Adodc1.Recordset.MoveFirst Call 单号金额对账初始化列宽修改限制 Else MsgBox "对不起,你所填写的对账日期超过了对账月份的限制!" & Chr(13) & "这个月只能入上个月的账目,无法人其他时间账目。", vbOKOnly, "错误提示" Cancel = True Exit Sub End If Else MsgBox "1年中只有12个月!请重新输入", vbOKOnly, "错误提示" Cancel = True End If End Sub 问题已解决,先给出解决代码。。。 用beforecolupdate事件来解决。。。。
u010574425 2014-02-09
  • 打赏
  • 举报
回复
时间控件得了
u010574425 2014-02-09
  • 打赏
  • 举报
回复
willchangerecord
of123 2014-02-08
  • 打赏
  • 举报
回复
不要事后检查并提示拒绝,用户体验不好。直接在 KeyPress 事件中拦截“非法”字符(输入不进去):
Private Sub DataGrid1_KeyPress(KeyAscii As Integer)
    If DataGrid1.Col = 2 Then
        If KeyAscii = Asc(".") And InStr(DataGrid1.Text, ".") Then
            KeyAscii = 0
        Else
            KeyAscii = Numeric_Only(KeyAscii)
        End If
    End If
End Sub

Private Function Numeric_Only(KeyAscii As Integer)
    Select Case KeyAscii
        Case Asc("0") To Asc("9"), Asc(".")
            Numeric_Only = KeyAscii
        Case Else
            Numeric_Only = 0
    End Select
End Function
  • 打赏
  • 举报
回复
引用 1 楼 of123 的回复:
不要事后检查并提示拒绝,用户体验不好。直接在 KeyPress 事件中拦截“非法”字符(输入不进去):
Private Sub DataGrid1_KeyPress(KeyAscii As Integer)
    If DataGrid1.Col = 2 Then
        If KeyAscii = Asc(".") And InStr(DataGrid1.Text, ".") Then
            KeyAscii = 0
        Else
            KeyAscii = Numeric_Only(KeyAscii)
        End If
    End If
End Sub

Private Function Numeric_Only(KeyAscii As Integer)
    Select Case KeyAscii
        Case Asc("0") To Asc("9"), Asc(".")
            Numeric_Only = KeyAscii
        Case Else
            Numeric_Only = 0
    End Select
End Function
首先感谢大师的指点,但这并没有达到我的理想状态,因为我想在全部内容录入完毕后,去验证我所输入内容的正确性和合理性,例如我这个表格要求填写的是时间格式:如2013-1-1,我需和服务器返回的时间做对比,例如服务器时间为2013-4-1,所填写的不能小于服务器时间,我想达到这样的效果,还请大师个指点一个明路!谢谢

7,763

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧