设为首页收藏本站Access中国

Office中国论坛/Access中国论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

返回列表 发新帖
查看: 2740|回复: 3
打印 上一主题 下一主题

[模块/函数] 〔已解决〕代码优化

[复制链接]
跳转到指定楼层
1#
发表于 2010-8-17 00:17:45 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 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
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 分享淘帖 订阅订阅
2#
发表于 2010-8-17 06:19:31 | 只看该作者
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




点击这里给我发消息

3#
发表于 2010-8-17 07:52:54 | 只看该作者
很简洁
4#
 楼主| 发表于 2010-8-17 08:11:54 | 只看该作者
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
谢谢老汉

您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|站长邮箱|小黑屋|手机版|Office中国/Access中国 ( 粤ICP备10043721号-1 )  

GMT+8, 2024-4-25 22:22 , Processed in 0.115400 second(s), 27 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表