单击“保存”按钮,检查用户是否在文本框中输入值 [英] Check if user enter a value in a text box when click on save button

查看:49
本文介绍了单击“保存”按钮,检查用户是否在文本框中输入值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


提前感谢您的帮助。我正在处理一个表格,其中包含预算信息的文本框,位置的组合框和与位置组合项目相关的人口列表框。


我想确保在单击保存记录按钮之前已选择所有信息。如果没有,提示用户根据提供的信息做出正确的选择。


当我检查是否已选择组合框项目时,一切正常,列表框也正常。唯一的问题是检查文本框是否输入了数据。下面是我检查这个的代码。请让我知道这个问题的最佳解决方案是什么。


Private Sub cmdSaveDWData_Click()


如果Me.cboLocationID.ListIndex =" -1"然后

MsgBox选择是否需要位置和预算信息,vbCritical,EnterLocation

Me.cboLocationID.SetFocus

退出Sub

结束如果



如果Len(Me.txtBudgetDW&"")= 0那么

MsgBox 输入Buget信息,vbInformation,BudgetInfoDW

Me.txtBudgetDW.SetFocus

退出Sub

结束如果



如果Me.lstTargetPopDW.ListIndex< 0然后

MsgBox目标人口需要选择,vbInformation,USLocation

Me.lstTargetPopDW.SetFocus

退出Sub

结束如果


结束sub

Hi All,

Thanks you in advance for your help. I am working on a form that has text box for Budget information, combo box for location and list box for population which is related to locations combo items.

I would like to make sure that all information has been selected before clicking the save record button. If not, prompt users to make the proper selection based on the information provided.

When I check if the combo box item has been selected, everything works fine as well as for the list box. The only issue is checking on the text box whether data has been entered or not. Below is my code to check this one out. Please let me know what the best solution to this problem is.

Private Sub cmdSaveDWData_Click()

If Me.cboLocationID.ListIndex = "-1" Then
MsgBox "A selection is requiered for location and Budget information", vbCritical, EnterLocation
Me.cboLocationID.SetFocus
Exit Sub
End If


If Len(Me.txtBudgetDW & "") = 0 Then
MsgBox "Enter Buget Information", vbInformation, BudgetInfoDW
Me.txtBudgetDW.SetFocus
Exit Sub
End If


If Me.lstTargetPopDW.ListIndex < 0 Then
MsgBox "A selection is required for Target Population", vbInformation, USLocation
Me.lstTargetPopDW.SetFocus
Exit Sub
End If

End sub

推荐答案

你好Joe,


我认为在尝试确定文本框中是否有值时,最好使用IsNull()函数。通过使用Len()函数,您假设实际上有一个值可供使用的函数。


我不是100%就此,但我想Len()函数需要SOMETHING才能返回值...即使值为零。在检查条件后,可以通过在代码中放入一个msgbox来检查我的理论:
Hi Joe,

I think you would be better off using the IsNull() function when trying to determine if there''s a value in the textbox. By using the Len() function, you are assuming that there is actually a value in there for the function to work with.

I''m not 100% on this, but I imagine that the Len() function requires SOMETHING to be there in order for it to return a value...even if the value is zero. You could check my theory by putting a msgbox in the code after you check the condition:
展开 | 选择 | Wrap | 行号


您应该将验证放入更新事件之前的表单,因为访问将自动更新您的记录,无论是否单击了保存按钮,当您浏览到新记录,关闭表单,关闭访问时。


因此,在您的表单'的BeforeUpdate中,它可能如下所示:
You should put validation in the form''s before update event, as access will automatically update your record regardless of whether the save button was clicked or not, when you browse to a new record, close the form, close access.

So in your form''s BeforeUpdate it could look like this:
展开 | 选择 | Wrap | 行号


@Joe - 你的表格是绑定还是未绑定?我得到的印象是它是未绑定的,因为您使用命令按钮来调用保存过程。在看到笑脸的帖子之后,我应该提出一个类似于笑脸的回复。


好​​的是你有两面硬币我们的回答。如果表单未绑定且您使用DAO,ADO或SQL命令作为保存过程将数据输入表中,则可以使用命令按钮提交数据。在用户准备提交数据之前,此方法不会检查数据。


如果您的表单已绑定,那么您可以在BeforeUpdate事件中使用Smiley的代码一旦用户输入数据,立即检查数据。你也可以在未绑定的形式中使用它......一旦他/她在集体输入所有数据后或在输入一个数据之后立即通知用户,这是一个优先问题。一个控件。


@Smiley - 为你收拾我的懈怠欢呼。
@Joe - Is your form bound or unbound? I got the impression that it was unbound since you are using a command button to invoke a save procedure. After seeing Smiley''s post, I should have offered up a response similar to Smiley''s.

The good thing is that you''ve got two sides of the coin with our responses. If your form is unbound and you are using DAO, ADO, or a SQL command as your save procedure to input data into your table, then you could use a command button to submit your data. This method wouldn''t check the data until the user was ready to submit the data.

If your form is bound, then you can use Smiley''s code in the BeforeUpdate event to check the data immediately once it''s input by the user. You could also use this in an unbound form too...it''s all a matter of preference whether or not you want to notify the user once he/she has entered all the data collectively or immediately after entering one piece of data in a single control.

@Smiley - Cheers to you for picking up my slack.


这篇关于单击“保存”按钮,检查用户是否在文本框中输入值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆