vba 检查用户表单中的空白文本框以使用消息框发出警告
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4301165/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check blank textboxes in the userform to warn with Message box
提问by jwala
I am using a worksheet, as a database table. I have a userform to fill data.
我正在使用工作表作为数据库表。我有一个用户表单来填充数据。
I want to code the following scenario.
我想编写以下场景。
When I click the save button on the userform, if there is any blank textbox in the userform, a message box with “YES” “NO” buttons has to be pop up, with a message like “ Customer name was not entered Do you want to enter ”.
当我点击用户表单上的保存按钮时,如果用户表单中有任何空白文本框,则必须弹出一个带有“是”“否”按钮的消息框,并显示“未输入客户名称您想要吗”这样的消息进入 ”。
If I click “YES”, it has to go to the blank field. If I click “NO”, it has to exit the message box and go to the next blank field if any. It has to check all blank fields, and then only save the data into the worksheet table.
如果我单击“是”,它必须转到空白字段。如果我单击“否”,它必须退出消息框并转到下一个空白字段(如果有)。它必须检查所有空白字段,然后才将数据保存到工作表中。
I have the following code which saves the date from the userform to the first available blank row in the worksheet table, but doesn't check any blank text boxes in the userform.
我有以下代码将用户表单中的日期保存到工作表表中的第一个可用空白行,但不检查用户表单中的任何空白文本框。
Private Sub cmdsave_Click()
Dim irow As Long
Dim ws As Worksheet
Set ws = Worksheets("customerDetails")
'find first empty row in database
irow = ws.Cells(Rows.Count, 3) _
.End(xlUp).Offset(1, 0).Row
'save the data to the database
ws.Cells(irow, 2).Value = Me.txtcustname.Value
ws.Cells(irow, 3).Value = Me.txtinvad1.Value
ws.Cells(irow, 4).Value = Me.txtinvad2.Value
ws.Cells(irow, 5).Value = Me.txtinvad3.Value
ws.Cells(irow, 6).Value = Me.txtdelyad1.Value
ws.Cells(irow, 7).Value = Me.txtdelyad2.Value
ws.Cells(irow, 8).Value = Me.txtdelyad3.Value
ws.Cells(irow, 9).Value = Me.txtcstno.Value
ws.Cells(irow, 10).Value = Me.txttinno.Value
ws.Cells(irow, 11).Value = Me.txteccno.Value
ws.Cells(irow, 12).Value = Me.txtdlno1.Value
ws.Cells(irow, 13).Value = Me.txtdlno2.Value
ws.Cells(irow, 14).Value = Me.txtstno.Value
ws.Cells(irow, 15).Value = Me.txtcsttinno.Value
ws.Cells(irow, 16).Value = Me.txtpanno.Value
ws.Cells(irow, 17).Value = Me.txtins.Value
'clear the data
Me.txtcustname.Value = ""
Me.txtinvad1.Value = ""
Me.txtinvad2.Value = ""
Me.txtinvad3.Value = ""
Me.txtdelyad1.Value = ""
Me.txtdelyad2.Value = ""
Me.txtdelyad3.Value = ""
Me.txtcstno.Value = ""
Me.txttinno.Value = ""
Me.txteccno.Value = ""
Me.txtdlno1.Value = ""
Me.txtdlno2.Value = ""
Me.txtstno.Value = ""
Me.txtcsttinno.Value = ""
Me.txtpanno.Value = ""
Me.txtins.Value = ""
End Sub
回答by Steve Homer
If you store the narrative name of your textbox ("Customer Address" for example) in the tag property of the control then you could use something like the below.
如果您将文本框的叙述名称(例如“客户地址”)存储在控件的标记属性中,那么您可以使用如下所示的内容。
Private Sub CommandButton1_Click()
Dim t As Control, res As VbMsgBoxResult
For Each t In Me.Controls
If TypeName(t) = "TextBox" Then ' Make sure we're only looking at textboxes.
If t.Text = vbNullString And t.Tag <> vbNullString Then
res = MsgBox("You've not completed the " + t.Tag + " field. Would you like to complete it now?", vbYesNo + vbQuestion)
If res = vbYes Then Exit Sub
End If
Next
End Sub