如何分辨“取消"与“取消"之间的区别?和“确定"输入框为空的文本框? [英] How to tell the difference between "cancel" and "OK" with an empty textbox of an InputBox?

查看:70
本文介绍了如何分辨“取消"与“取消"之间的区别?和“确定"输入框为空的文本框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

按下取消按钮时,VBA inputBox返回一个空字符串.
按下确定"后,它将在其文本框中显示文本.
inputBox的第三个位置参数是其文本框中的初始文本.此参数的默认值为".

A VBA inputBox returns an empty string when the cancel button is pressed.
When OK is pressed, it the text in its textbox.
The inputBox's third positional argument is the initial text in its textbox. The default value for this argument is "".

当用户单击添加记录"时,我使用inputBox要求用户为新记录指定名称.按钮.如果他按取消",则退出子菜单.

I use an inputBox to ask a user to specify the name for a new record when he clicks an "Add Record" button. If he presses "cancel", I exit the sub.

如果他无法输入名称,或者输入一个名称并将其删除,我想让msgBox告诉他必须指定一个唯一的记录名称.

If he fails to enter a name, or enters one and deletes it, I want a msgBox to tell him he must specify a unique record name.

我如何分辨取消"与取消"之间的区别?和确定"一个空的文本框?

How do I tell the difference between "cancel" and "OK" with an empty textbox?

我发现了几个类似的问题,但都没有解决我的问题.

I found several similar questions but none solved my problem.

推荐答案

有一种方法可以检查用户是否单击了取消"或只是输入了空字符串.试试这个:

There is a way to check if user clicked "Cancel" or just entered empty string. Try this:

test = InputBox("Enter a value")
If StrPtr(test) = 0 Then
    MsgBox "Cancel"
ElseIf Len(test) = 0 Then
    MsgBox "No entry"
Else
    MsgBox "Correct"
End If

但是,这是一个非常粗略的解决方案.您可以在此处阅读有关StrPtr函数的更多信息:有哪些好处和风险在VBA中使用StrPtr函数?

However it is quite sketchy solution. You can read more about StrPtr function here: What are the benefits and risks of using the StrPtr function in VBA?

这篇关于如何分辨“取消"与“取消"之间的区别?和“确定"输入框为空的文本框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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