Office中国论坛/Access中国论坛

标题: 〔已解决〕代码优化 [打印本页]

作者: xuwenning    时间: 2010-8-17 00:17
标题: 〔已解决〕代码优化
本帖最后由 xuwenning 于 2010-8-17 09:06 编辑

下面检查是文本数据是否正确
文本数据——必须是以0开头的最长为(0.0000)小数,或数据值为“-”号—表示没有

求代码优化统一使用一个模块来检查数据

还有一个条件是数据最长为(0.0000)小数点后四位的长度不知如何写了,请高手一并搞定

Private Sub txt1_AfterUpdate()

    If Left(Me. txt1, 1) <> "0" Or Left(Me. txt1, 1) <> "-" Then
   
        MsgBox "无效数字首位必须是0"
        Me. txt1 = ""
        Me. txt1.SetFocus
   
    Exit Sub
    End If

End Sub

Private Sub Txt2_AfterUpdate()

    If Left(Me.Txt2, 1) <> "0" Or Left(Me.Txt2, 1) <> "-" Then
   
        MsgBox "无效数字首位必须是0"
        Me.Txt2 = ""
        Me.Txt2.SetFocus
   
    Exit Sub
    End If

End Sub

Private Sub Txt3_AfterUpdate()

    If Left(Me.Txt3, 1) <> "0" Or Left(Me.Txt3, 1) <> "-" Then
   
        MsgBox "无效数字首位必须是0"
        Me.Txt3 = ""
        Me.Txt3.SetFocus
   
    Exit Sub
    End If

End Sub

Private Sub Txt4_AfterUpdate()

    If Left(Me.Txt4, 1) <> "0" Or Left(Me.Txt4, 1) <> "-" Then
   
        MsgBox "无效数字首位必须是0"
        Me.Txt4 = ""
        Me.Txt4.SetFocus
   
    Exit Sub
    End If

End Sub

Private Sub Txt5_AfterUpdate()

    If Left(Me.Txt5, 1) <> "0" Or Left(Me.Txt5, 1) <> "-" Then
   
        MsgBox "无效数字首位必须是0"
        Me.Txt5 = ""
        Me.Txt5.SetFocus
   
    Exit Sub
    End If

End Sub

Private Sub Txt6_AfterUpdate()

    If Left(Me.Txt6, 1) <> "0" Or Left(Me.Txt6, 1) <> "-" Then
   
        MsgBox "无效数字首位必须是0"
        Me.Txt6 = ""
        Me.Txt6.SetFocus
   
    Exit Sub
    End If

End Sub

Private Sub Txt7_AfterUpdate()

    If Left(Me.Txt7, 1) <> "0" Or Left(Me.Txt7, 1) <> "-" Then
   
        MsgBox "无效数字首位必须是0"
        Me.Txt7 = ""
        Me.Txt7.SetFocus
   
    Exit Sub
    End If
End Sub

Private Sub Txt8_AfterUpdate()

    If Left(Me.Txt8, 1) <> "0" Or Left(Me.Txt8, 1) <> "-" Then
   
        MsgBox "无效数字首位必须是0"
        Me.Txt8 = ""
        Me.Txt8.SetFocus
   
    Exit Sub
    End If

End Sub
作者: todaynew    时间: 2010-8-17 06:19
Sub 格式(ctl As Control)
Dim B As Boolean
B = Left(Me.txt1, 1) = "0" Or Left(Me.txt1, 1) = "-"
B = B And Len(Mid(ctl.Value, InStr(ctl.Value, "."))) <= 4
If B = False Then
    ctl.Value = "格式错误!请重新输入!"
    ctl.SetFocus
End If
End Sub


Private Sub txt1_AfterUpdate()
     Call 格式(me.txt1)
End Sub

Private Sub Txt2_AfterUpdate()
     Call 格式(me.txt2)
End Sub





作者: tmtony    时间: 2010-8-17 07:52
很简洁
作者: xuwenning    时间: 2010-8-17 08:11
Sub 格式(ctl As Control)
Dim B As Boolean
B = Left(ctlValue , 1) = "0" Or Left(ctl Value, 1) = "-"
B = B And Len(Mid(ctl.Value, InStr(ctl.Value, "."))) <= 4
If B = False Then
    ctl.Value = "格式错误!请重新输入!"
    ctl.SetFocus
End If
End Sub
谢谢老汉






欢迎光临 Office中国论坛/Access中国论坛 (http://www.office-cn.net/) Powered by Discuz! X3.3