设为首页收藏本站Access中国

Office中国论坛/Access中国论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

123
返回列表 发新帖
楼主:
打印 上一主题 下一主题

[窗体] 很烦的数据输入问题,如何避免控件值为空

[复制链接]
21#
发表于 2012-1-13 09:20:47 | 只看该作者
简 发表于 2012-1-12 18:56
我就是采用你所说的采用保存按钮的方法来实现了该效果。经过折腾,我好像找到一个原因,就是如果记录没有 ...

Private Sub cmdnext_Click()
    On Error GoTo Err_cmdnext_Click

    Dim ctr As Control
    For Each ctr In Me.Controls
        If TypeOf ctr Is TextBox Or TypeOf ctr Is ComboBox Then
            If IsNull(ctr) Then
                MsgBox ctr.ControlSource & "不能为空。"
                ctr.SetFocus
                Exit Sub
            End If
            If ctr.ControlSource = "卡号" Then
                If Not IsNumeric(ctr) Or Len(ctr) <> 3 Then
                    MsgBox "请按照'***'格式输入卡号!"
                    ctr.SetFocus
                    Exit Sub
                End If
            End If
            
        End If
    Next

    DoCmd.RunCommand acCmdSaveRecord
    DoCmd.GoToRecord , , acNext

    Me.cbogongchang.SetFocus
End_cmdnext_Click:
    Exit Sub
Err_cmdnext_Click:
    MsgBox Error
    Resume End_cmdnext_Click
End Sub

用上面的代码置换你的代码试试。
22#
发表于 2012-1-13 16:19:00 | 只看该作者
怎么了,5楼的代码不行吗,或是不符合要求?
23#
发表于 2012-1-13 21:16:42 | 只看该作者
简 发表于 2012-1-12 18:52
老汉我觉得你说得不完全正确,即使表设计中字段的必填字段为是,也解决不了控件为空时焦点落在当前控件的 ...

呵呵,非要如此,也很简单。





Private Sub Form_Load()
Dim ctrls As Controls
Dim ctrl As Control
Set ctrls = Me.Controls
For Each ctrl In ctrls
    If ctrl.ControlType = acTextBox Or ctrl.ControlType = acComboBox Then
        ctrl.OnGotFocus = "=CtrlSetFocus()"
    End If
Next
End Sub

Function CtrlSetFocus()
Dim ctrls As Controls
Dim ctrl As Control
Dim i As Integer, m As Integer
m = 100
Set ctrls = Me.Controls

For i = 0 To ctrls.Count - 1
    If ctrls(i).ControlType = acTextBox Or ctrls(i).ControlType = acComboBox Then
        If IsNull(ctrls(i).Value) = True Then
            If ctrls(i).TabIndex < m Then
                Set ctrl = ctrls(i)
                m = ctrls(i).TabIndex
            End If
        End If
    End If
Next
If m <> 100 Then
    ctrl.SetFocus
End If
End Function

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
24#
 楼主| 发表于 2012-1-14 06:25:06 | 只看该作者
todaynew 发表于 2012-1-13 21:16
呵呵,非要如此,也很简单。

老汉,你这是窗体未绑定啊,窗体未绑定就好办,窗体绑定了就不好办喔~~~~
25#
 楼主| 发表于 2012-1-14 06:56:00 | 只看该作者
Henry D. Sy 发表于 2012-1-13 16:19
怎么了,5楼的代码不行吗,或是不符合要求?

我的窗体是绑定的,在退出窗体时,如果有控件为空,就不能退出,需要点击多次才能退出。
26#
发表于 2012-1-14 08:12:04 | 只看该作者
老汉的代码可以收藏以后有用的时候用上,不过我现在不折腾空值问题,只是我自己输入不会那么粗心,哈
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2025-2-25 20:29 , Processed in 0.093602 second(s), 29 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

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