一旦我在文本框中输入错误数量的数字并提交表单,程序就会中断并显示此消息。
未处理的类型' System.Data.OleDb.OleDbException' 发生在System.Data.dll
中附加信息:禁止一个或多个值 验证规则是否为空或类似#34; ###########"'为...设置 ' T_Jobs.Customer_Phone&#39 ;.输入表达式的值 字段可以接受。"
我已使用数据源连接向导将visual basic连接到访问。我希望程序显示一条消息,说明" 输入11个数字"而不是打破它。
答案 0 :(得分:1)
您可以创建验证功能
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If IsValidForm() Then
' Save the Record
' This line takes all the input from the boxes from adding a New job form, And puts it into
' corresponding boxes in the main menu = because only those boxes
' are connected to the database
' It uses the data adapter from form1
Form1.T_JobsTableAdapter.Insert(Me.TextBox2.Text, M, e.TextBox3.Text, Me.TextBox4.Text,
Me.TextBox5.Text, Me.TextBox6.Text, Me.TextBox7.Text, Me.TextBox8.Text)
'This line updates the table Jobs dataset with the information in the boxes in main menu
'The DataSet passes on the collected data to the database
Form1.T_JobsTableAdapter.Fill(Form1.JobsDBDataSet.T_Jobs)
MsgBox("Record added successfully")
' These lines clear the textboxes so that next job can be added
TextBox2.Text = ""
TextBox3.Text = ""
TextBox4.Text = ""
TextBox5.Text = ""
TextBox6.Text = ""
TextBox7.Text = ""
TextBox8.Text = ""
End If
End Sub
Private Function IsValidForm() As Boolean
Dim msg As String = String.Empty
' Ensure that a Valid Vehicle Model was entered
If TextBox3.Text = "" Then
' MsgBox("Please fill in Car Model", MsgBoxStyle.Information)
msg += "Enter a Car Model" & vbCr
End If
' Validate Date Received
If Not IsDate(TextBox2.Text) Then
msg += "Enter a valid Date Received" & vbCr
End If
' Validate Date Due
If Not IsDate(TextBox2.Text) Then
msg += "Enter a valid Date Due" & vbCr
End If
' Validate Phone Number
If Trim(TextBox8.Text) = "" Then
' NOTE I am not sure how you want to validate this phone number.
' You can do it with RegEx
' The Regular Expression tells VB to make sure that TextBox8 contains only
' Numbers 1 - 9 and only a length of 11. If it does not match, then
' display the validation message
If Not System.Text.RegularExpressions.Regex.IsMatch(TextBox8.Text, "^[0-9]{11}$") Then
msg += "Enter a Phone Number" & vbCr
End If
End If
If msg.Length > 0 Then
MessageBox.Show("Please fix the following errors before continuing:" & Microsoft.VisualBasic.ControlChars.CrLf & msg,
Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information)
Return False
Else
Return True
End If
End Function
答案 1 :(得分:0)
这是添加数据的代码,它还验证文本框是否为空以及日期是否为日期格式
{{1}}