2016-03-02 46 views
0

一旦我在文本框中輸入了錯誤的數字並提交了表單,程序就會中斷並顯示此消息。在訪問中設置的驗證會破壞程序

類型「System.Data.OleDb.OleDbException」 未處理的異常出現在system.data.dll

其他信息:一個或多個值由 驗證規則「禁止爲空或像「###########」'設置爲 'T_Jobs.Customer_Phone'。輸入值,對於此 字段中的表達式可以接受「。

我已連接視覺基本訪問使用連接嚮導的數據源。我希望程序顯示一條消息‘輸入11個數字’而不是將其打破。

+1

我會在發送數據之前驗證數據*。 – Plutonix

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

可愛的想法,但現在我不能關閉程序沒有把數字放進去。再加上即使當我輸入數字的信息仍然彈出,我不能做anythig –

+0

@KamilLazarowicz你可以做需要的附加驗證。這只是您可以驗證該字段的一種方式的一個示例。有很多選擇,你可以採取。無論如何,我更願意提供幫助,但是我可以,但是我們還沒有在您正在編寫的軟件上進行合作,除了上面列出的內容。我不確定軟件的所有要求。我只是試圖幫助停止發生錯誤,如果將11個數字輸入到文本框中。如果你可以提供一些代碼,也許我可以提供更好的幫助。 – Talsiter

+0

當然,但讓我知道你通常需要哪個代碼, –

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個數字或不。 –