在访问中设置的验证会破坏程序

在访问中设置的验证会破坏程序

问题描述:

一旦我在文本框中输入了错误的数字并提交了表单,程序就会中断并显示此消息。在访问中设置的验证会破坏程序

类型“System.Data.OleDb.OleDbException” 未处理的异常出现在system.data.dll

其他信息:一个或多个值由 验证规则“禁止为空或像“###########”'设置为 'T_Jobs.Customer_Phone'。输入值,对于此 字段中的表达式可以接受“。

我已连接视觉基本访问使用连接向导的数据源。我希望程序显示一条消息‘输入11个数字’而不是将其打破。

+1

我会在发送数据之前验证数据*。 – Plutonix

+0

是否可以设置我想要的文本框的位数? –

您可以创建一个验证功能

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 
+0

可爱的想法,但现在我不能关闭程序没有把数字放进去。再加上即使当我输入数字的信息仍然弹出,我不能做anythig –

+0

@KamilLazarowicz你可以做需要的附加验证。这只是您可以验证该字段的一种方式的一个示例。有很多选择,你可以采取。无论如何,我更愿意提供帮助,但是我可以,但是我们还没有在您正在编写的软件上进行合作,除了上面列出的内容。我不确定软件的所有要求。我只是试图帮助停止发生错误,如果将11个数字输入到文本框中。如果你可以提供一些代码,也许我可以提供更好的帮助。 – Talsiter

+0

当然,但让我知道你通常需要哪个代码, –

这是添加数据的代码,这也验证了,如果一个文本框心不是空,如果日期是在日期格式

  If TextBox3.Text = "" Then 
     MsgBox("Please fill in Car Model", MsgBoxStyle.Information) 

    Else 

     'This checks if the Date Recieved and Date Due is in the correct format 
     If IsDate(TextBox2.Text) And IsDate(TextBox6.Text) Then 
      '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, Me.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 sucessfully") 

      '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 = "" 

     Else 
      MsgBox("Please ensure the Date Recieved and Date Due is in dd/mm/yyyy format", MsgBoxStyle.Information) 

     End If 

    End If 
+0

这是非常有用的谢谢你,工作应该。我希望这个数字只是11个数字;我限制了文本框只允许在key_press事件上的数字,现在我只需要它来检查是否有11个数字或不。 –