[英]How do I get my If block in visual basic database program to comple all task?
我有一個編碼為on_click操作的函數,它並沒有一直循環。 我已經多次移動了各種“ If”塊控件以及代碼本身,但我無法弄清楚如何使其一路過關斬將。
到目前為止,它將檢查數據庫中是否包含公司名稱,然后檢查該字段是否為空白,然后檢查以確保郵政編碼框為數字。 到代碼為止。 我不能讓它循環,而實際上將行添加到數據庫中。 如果四處移動代碼行,則可以添加該行,但它會允許出現諸如錯誤的zip格式以及空白值之類的錯誤。 我需要代碼來執行檢查,然后如果所有條目都有效,它將添加該行。
到目前為止,我只使用一種形式,但是這個確切的問題將應用於整個項目中的其他幾種形式。 但是,如果我無法使用一種表格,那么我也將無法使用其他表格。
這是有問題的代碼:
Public Sub AddNewButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddNewButton.Click
Dim CompanyNameCheck = FinancialAgenciesTableAdapter1.CompanyNameCheck(TextBox1.Text)
Dim i As Integer
i = 0
If CompanyNameCheck Is Nothing Then
If TextBox1.Text = vbNullString Then
MsgBox("Company name field blank.", MessageBoxButtons.OK, MessageBoxIcon.Error)
TextBox1.Focus()
ElseIf TextBox2.Text = vbNullString Then
MsgBox("Street field blank.", MessageBoxButtons.OK, MessageBoxIcon.Error)
TextBox2.Focus()
ElseIf TextBox4.Text = vbNullString Then
MsgBox("City field blank.", MessageBoxButtons.OK, MessageBoxIcon.Error)
TextBox4.Focus()
ElseIf TextBox5.Text = vbNullString Then
MsgBox("State field blank.", MessageBoxButtons.OK, MessageBoxIcon.Error)
TextBox5.Focus()
ElseIf TextBox6.Text = vbNullString Then
MsgBox("Zip field blank.", MessageBoxButtons.OK, MessageBoxIcon.Error)
TextBox6.Focus()
ElseIf i < 1 Then
Try
Integer.Parse(TextBox6.Text)
Catch ex As Exception
MsgBox("Zip field not correct. Please enter only numbers.", MessageBoxButtons.OK, MessageBoxIcon.Error)
TextBox6.Focus()
End Try
Else
FinancialDepartmentForm.FinancialAgenciesTableAdapter.Insert(Me.TextBox1.Text, Me.TextBox2.Text, Me.TextBox3.Text, _
Me.TextBox4.Text, Me.TextBox5.Text, Me.TextBox6.Text, _
Me.TextBox7.Text)
'Me.Validate()
'refresh the listbox of companies.
FinancialDepartmentForm.FinancialAgenciesTableAdapter.Fill(FinancialDepartmentForm.FinancialAgenciesDataSet.FinancialAgencies)
'show message of task completion.
MsgBox("Addition complete.", MessageBoxButtons.OK)
Me.Hide()
FinancialDepartmentForm.Show()
End If
Else
MsgBox("Company already exist!")
TextBox1.Focus()
End If
End Sub
該項目的截止日期正在迅速臨近,我確實需要一些幫助。 我非常感謝您的幫助,因為這不應該那么復雜。 我只是無法弄清楚。
與朋友交談后,他將我與能夠幫助他的朋友聯系起來。 他添加了另一個變量來跟蹤錯誤計數,然后在單獨的If語句中使用了它來控制驗證和插入代碼。 正常工作的代碼如下:
Public Sub AddNewButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddNewButton.Click
Dim CompanyNameCheck = FinancialAgenciesTableAdapter1.CompanyNameCheck(TextBox1.Text)
Dim i As Integer
i = 0
Dim errorCount As Integer
If CompanyNameCheck Is Nothing Then
errorCount = 0
If TextBox1.Text = vbNullString Then
MsgBox("Company name field blank.", MessageBoxButtons.OK, MessageBoxIcon.Error)
TextBox1.Focus()
errorCount = errorCount + 1
ElseIf TextBox2.Text = vbNullString Then
MsgBox("Street field blank.", MessageBoxButtons.OK, MessageBoxIcon.Error)
TextBox2.Focus()
errorCount = errorCount + 1
ElseIf TextBox4.Text = vbNullString Then
MsgBox("City field blank.", MessageBoxButtons.OK, MessageBoxIcon.Error)
TextBox4.Focus()
errorCount = errorCount + 1
ElseIf TextBox5.Text = vbNullString Then
MsgBox("State field blank.", MessageBoxButtons.OK, MessageBoxIcon.Error)
TextBox5.Focus()
errorCount = errorCount + 1
ElseIf TextBox6.Text = vbNullString Then
MsgBox("Zip field blank.", MessageBoxButtons.OK, MessageBoxIcon.Error)
TextBox6.Focus()
errorCount = errorCount + 1
ElseIf i < 1 Then
Try
Integer.Parse(TextBox6.Text)
Catch ex As Exception
MsgBox("Zip field not correct. Please enter only numbers.", MessageBoxButtons.OK, MessageBoxIcon.Error)
TextBox6.Focus()
errorCount = errorCount + 1
End Try
End If
If errorCount < 1 Then
FinancialDepartmentForm.FinancialAgenciesTableAdapter.Insert(Me.TextBox1.Text, Me.TextBox2.Text, Me.TextBox3.Text, _
Me.TextBox4.Text, Me.TextBox5.Text, Me.TextBox6.Text, _
Me.TextBox7.Text)
Me.Validate()
'refresh the listbox of companies.
FinancialDepartmentForm.FinancialAgenciesTableAdapter.Fill(FinancialDepartmentForm.FinancialAgenciesDataSet.FinancialAgencies)
'show message of task completion.
MsgBox("Addition complete.", MessageBoxButtons.OK)
Me.Hide()
FinancialDepartmentForm.Show()
End If
Else
MsgBox("Company already exist!")
TextBox1.Focus()
End If
End Sub
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.