您可以創建一個驗證功能
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
我會在發送數據之前驗證數據*。 – Plutonix
是否可以設置我想要的文本框的位數? –