在文本框中输入内容并按下按钮时,如何显示消息框?

时间:2018-11-27 17:50:01

标签: ms-access access-vba

是否有可能在文本框中写入内容时显示弹出消息框?对我而言,文本框实际上有什么无关紧要,我只希望在按下按钮并且文本框中有文本时出现一条消息。

以下是我目前拥有的:

Private Sub cmdReg_Click()

CurrentDb.Execute "INSERT INTO qryShowAll(Student_FirstName, Student_LastName, Parent_FirstName, Parent_LastName, AddressLine, City, State, Zip, PhoneNumber) VALUES ('" & Me.txtStudentFirst & " ',' " & Me.txtStudentLast & "','" & Me.txtParentFirst & " ',' " & Me.txtParentLast & "','" & Me.txtAddress & " ',' " & Me.txtCity & "',' " & Me.txtState & "','" & Me.txtZip & "','" & Me.txtNumber & "')"

If (txtStudentFirst,txtStudentLast,txtParentFirst,txtParentLast,txtAddress ,txtCity,txtState,txtZip,txtNumber) = " "

MsgBox "The student was successfully registered.", vbOKOnly, "Student Registered!"

Else
MsgBox "Student was not registered, please complete the form.", vbOKOnly, ""

End If

1 个答案:

答案 0 :(得分:0)

一种方法是使用+符号连接文本框。即使一个为Null,也将导致Null。这种方法需要在表格中设置文本字段,以不允许使用空字符串。

If  IsNull(txtStudentFirst + txtStudentLast + txtParentFirst + txtParentLast + txtAddress + txtCity + txtState + txtZip + txtNumber) Then
    MsgBox "Student was not registered, please complete the form.", vbOKOnly
Else
    MsgBox "Student was successfully registered.", vbOKOnly, ""
End If

或者,根据需要在表中设置字段,然后让Access禁止用户填写。也可以使用ValidationRule和ValidationText属性来帮助确保输入数据。