![](/img/trans.png)
[英]How to Create a New Record in an Access Database using Visual Studio 2019 (VB.NET) Button Click
[英]Visual Studio 2015 VB.net How to find record in MS Access database and disable button in form?
我正在嘗試加載表單,但是在加載時必須轉到Access數據庫,並檢查是否存在帶有按鈕文本和今天日期的記錄,如果存在,則禁用按鈕。 我的表單中有大約50個按鈕。 注意:請檢查以下我的代碼,它們適用於4或5,但其他代碼不起作用。 我沒有錯誤信息。 也許如果有人有更好的方法可以做到這一點,我將非常感謝。 這是我的加載表格
Imports System.Data.OleDb
公共類Wells Dim提供程序作為字符串Dim數據文件作為字符串Dim connString作為字符串公共myConnection作為OleDbConnection = New OleDbConnection公共dr作為OleDbDataReader公共da As OleDbDataAdapter Dim錯誤作為字符串
Private Sub Wells_Load(sender As Object, e As EventArgs) Handles MyBase.Load
lblUserActive.Text = UserActive
ZoneActive = Nothing
WellActive = Nothing
provider = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source ="
dataFile = "f:\wortendyke\WortendykeTables.accdb"
connString = provider & dataFile
myConnection.ConnectionString = connString
Errors = ""
Try
If Button19.Enabled = True Then
myConnection.Open()
Dim str As String
str = "SELECT * FROM maintable WHERE location='" & Button19.Text & "' AND pumpingdate=#" & dateActive & "#"
Dim cmd As OleDbCommand = New OleDbCommand(str, myConnection)
dr = cmd.ExecuteReader
dr.Read()
If Button19.Text = dr("location").ToString And dateActive = dr("pumpingdate").ToString Then
Button19.Enabled = False
myConnection.Close()
Me.Refresh()
End If
End If
Catch ex As Exception
End Try
Try
If Button23.Enabled = True Then
myConnection.Open()
Dim str As String
str = "SELECT * FROM maintable WHERE location='" & Button23.Text & "' AND pumpingdate=#" & dateActive & "#"
Dim cmd As OleDbCommand = New OleDbCommand(str, myConnection)
dr = cmd.ExecuteReader
dr.Read()
If Button23.Text = dr("location").ToString And dateActive = dr("pumpingdate").ToString Then
Button23.Enabled = False
myConnection.Close()
Me.Refresh()
End If
End If
Catch ex As Exception
End Try
Try
If Button17.Enabled = True Then
myConnection.Open()
Dim str As String
str = "SELECT * FROM maintable WHERE location='" & Button17.Text & "' AND pumpingdate=#" & dateActive & "#"
Dim cmd As OleDbCommand = New OleDbCommand(str, myConnection)
dr = cmd.ExecuteReader
dr.Read()
If Button17.Text = dr("location").ToString And dateActive = dr("pumpingdate").ToString Then
Button17.Enabled = False
myConnection.Close()
Me.Refresh()
End If
End If
Catch ex As Exception
End Try
Try
If Button3.Enabled = True Then
myConnection.Open()
Dim str As String
str = "SELECT * FROM maintable WHERE location='" & Button3.Text & "' AND pumpingdate=#" & dateActive & "#"
Dim cmd As OleDbCommand = New OleDbCommand(str, myConnection)
dr = cmd.ExecuteReader
dr.Read()
If Button3.Text = dr("location").ToString And dateActive = dr("pumpingdate").ToString Then
Button3.Enabled = False
myConnection.Close()
Me.Refresh()
End If
End If
Catch ex As Exception
End Try
Try
If Button20.Enabled = True Then
myConnection.Open()
Dim str As String
str = "SELECT * FROM maintable WHERE location='" & Button20.Text & "' AND pumpingdate=#" & dateActive & "#"
Dim cmd As OleDbCommand = New OleDbCommand(str, myConnection)
dr = cmd.ExecuteReader
dr.Read()
If Button20.Text = dr("location").ToString And dateActive = dr("pumpingdate").ToString Then
Button20.Enabled = False
myConnection.Close()
Me.Refresh()
End If
End If
Catch ex As Exception
End Try
Try
If Button1.Enabled = True Then
myConnection.Open()
Dim str As String
str = "SELECT * FROM maintable WHERE location='" & Button1.Text & "' AND pumpingdate=#" & dateActive & "#"
Dim cmd As OleDbCommand = New OleDbCommand(str, myConnection)
dr = cmd.ExecuteReader
dr.Read()
If Button1.Text = dr("location").ToString And dateActive = dr("pumpingdate").ToString Then
Button1.Enabled = False
myConnection.Close()
Me.Refresh()
End If
End If
Catch ex As Exception
End Try
Try
If Button2.Enabled = True Then
myConnection.Open()
Dim str As String
str = "SELECT * FROM maintable WHERE location='" & Button2.Text & "' AND pumpingdate=#" & dateActive & "#"
Dim cmd As OleDbCommand = New OleDbCommand(str, myConnection)
dr = cmd.ExecuteReader
dr.Read()
If Button2.Text = dr("location").ToString And dateActive = dr("pumpingdate").ToString Then
Button2.Enabled = False
myConnection.Close()
Me.Refresh()
End If
End If
Catch ex As Exception
End Try
Try
If Button18.Enabled = True Then
myConnection.Open()
Dim str As String
str = "SELECT * FROM maintable WHERE location='" & Button18.Text & "' AND pumpingdate=#" & dateActive & "#"
Dim cmd As OleDbCommand = New OleDbCommand(str, myConnection)
dr = cmd.ExecuteReader
dr.Read()
If Button18.Text = dr("location").ToString And dateActive = dr("pumpingdate").ToString Then
Button18.Enabled = False
myConnection.Close()
Me.Refresh()
End If
End If
Catch ex As Exception
End Try
Try
If Button7.Enabled = True Then
myConnection.Open()
Dim str As String
str = "SELECT * FROM maintable WHERE location='" & Button7.Text & "' AND pumpingdate=#" & dateActive & "#"
Dim cmd As OleDbCommand = New OleDbCommand(str, myConnection)
dr = cmd.ExecuteReader
dr.Read()
If Button7.Text = dr("location").ToString And dateActive = dr("pumpingdate").ToString Then
Button7.Enabled = False
myConnection.Close()
Me.Refresh()
End If
End If
Catch ex As Exception
End Try
Try
If Button16.Enabled = True Then
myConnection.Open()
Dim str As String
str = "SELECT * FROM maintable WHERE location='" & Button16.Text & "' AND pumpingdate=#" & dateActive & "#"
Dim cmd As OleDbCommand = New OleDbCommand(str, myConnection)
dr = cmd.ExecuteReader
dr.Read()
If Button16.Text = dr("location").ToString And dateActive = dr("pumpingdate").ToString Then
Button16.Enabled = False
myConnection.Close()
Me.Refresh()
End If
End If
Catch ex As Exception
End Try
Try
If Button15.Enabled = True Then
myConnection.Open()
Dim str As String
str = "SELECT * FROM maintable WHERE location='" & Button15.Text & "' AND pumpingdate=#" & dateActive & "#"
Dim cmd As OleDbCommand = New OleDbCommand(str, myConnection)
dr = cmd.ExecuteReader
dr.Read()
If Button15.Text = dr("location").ToString And dateActive = dr("pumpingdate").ToString Then
Button15.Enabled = False
myConnection.Close()
Me.Refresh()
End If
End If
Catch ex As Exception
End Try
Try
If Button14.Enabled = True Then
myConnection.Open()
Dim str As String
str = "SELECT * FROM maintable WHERE location='" & Button14.Text & "' AND pumpingdate=#" & dateActive & "#"
Dim cmd As OleDbCommand = New OleDbCommand(str, myConnection)
dr = cmd.ExecuteReader
dr.Read()
If Button14.Text = dr("location").ToString And dateActive = dr("pumpingdate").ToString Then
Button14.Enabled = False
myConnection.Close()
Me.Refresh()
End If
End If
Catch ex As Exception
End Try
Try
If Button13.Enabled = True Then
myConnection.Open()
Dim str As String
str = "SELECT * FROM maintable WHERE location='" & Button13.Text & "' AND pumpingdate=#" & dateActive & "#"
Dim cmd As OleDbCommand = New OleDbCommand(str, myConnection)
dr = cmd.ExecuteReader
dr.Read()
If Button13.Text = dr("location").ToString And dateActive = dr("pumpingdate").ToString Then
Button13.Enabled = False
myConnection.Close()
Me.Refresh()
End If
End If
Catch ex As Exception
End Try
Try
If Button12.Enabled = True Then
myConnection.Open()
Dim str As String
str = "SELECT * FROM maintable WHERE location='" & Button12.Text & "' AND pumpingdate=#" & dateActive & "#"
Dim cmd As OleDbCommand = New OleDbCommand(str, myConnection)
dr = cmd.ExecuteReader
dr.Read()
If Button12.Text = dr("location").ToString And dateActive = dr("pumpingdate").ToString Then
Button12.Enabled = False
myConnection.Close()
Me.Refresh()
End If
End If
Catch ex As Exception
End Try
Try
If Button11.Enabled = True Then
myConnection.Open()
Dim str As String
str = "SELECT * FROM maintable WHERE location='" & Button11.Text & "' AND pumpingdate=#" & dateActive & "#"
Dim cmd As OleDbCommand = New OleDbCommand(str, myConnection)
dr = cmd.ExecuteReader
dr.Read()
If Button11.Text = dr("location").ToString And dateActive = dr("pumpingdate").ToString Then
Button11.Enabled = False
myConnection.Close()
Me.Refresh()
End If
End If
Catch ex As Exception
End Try
Try
If Button10.Enabled = True Then
myConnection.Open()
Dim str As String
str = "SELECT * FROM maintable WHERE location='" & Button10.Text & "' AND pumpingdate=#" & dateActive & "#"
Dim cmd As OleDbCommand = New OleDbCommand(str, myConnection)
dr = cmd.ExecuteReader
dr.Read()
If Button10.Text = dr("location").ToString And dateActive = dr("pumpingdate").ToString Then
Button10.Enabled = False
myConnection.Close()
Me.Refresh()
End If
End If
Catch ex As Exception
End Try
Try
If Button9.Enabled = True Then
myConnection.Open()
Dim str As String
str = "SELECT * FROM maintable WHERE location='" & Button9.Text & "' AND pumpingdate=#" & dateActive & "#"
Dim cmd As OleDbCommand = New OleDbCommand(str, myConnection)
dr = cmd.ExecuteReader
dr.Read()
If Button9.Text = dr("location").ToString And dateActive = dr("pumpingdate").ToString Then
Button9.Enabled = False
myConnection.Close()
Me.Refresh()
End If
End If
Catch ex As Exception
End Try
Try
If Button8.Enabled = True Then
myConnection.Open()
Dim str As String
str = "SELECT * FROM maintable WHERE location='" & Button8.Text & "' AND pumpingdate=#" & dateActive & "#"
Dim cmd As OleDbCommand = New OleDbCommand(str, myConnection)
dr = cmd.ExecuteReader
dr.Read()
If Button8.Text = dr("location").ToString And dateActive = dr("pumpingdate").ToString Then
Button8.Enabled = False
myConnection.Close()
Me.Refresh()
End If
End If
Catch ex As Exception
End Try
Try
If Button6.Enabled = True Then
myConnection.Open()
Dim str As String
str = "SELECT * FROM maintable WHERE location='" & Button6.Text & "' AND pumpingdate=#" & dateActive & "#"
Dim cmd As OleDbCommand = New OleDbCommand(str, myConnection)
dr = cmd.ExecuteReader
dr.Read()
If Button6.Text = dr("location").ToString And dateActive = dr("pumpingdate").ToString Then
Button6.Enabled = False
myConnection.Close()
Me.Refresh()
End If
End If
Catch ex As Exception
End Try
Try
If Button5.Enabled = True Then
myConnection.Open()
Dim str As String
str = "SELECT * FROM maintable WHERE location='" & Button5.Text & "' AND pumpingdate=#" & dateActive & "#"
Dim cmd As OleDbCommand = New OleDbCommand(str, myConnection)
dr = cmd.ExecuteReader
dr.Read()
If Button5.Text = dr("location").ToString And dateActive = dr("pumpingdate").ToString Then
Button5.Enabled = False
myConnection.Close()
Me.Refresh()
End If
End If
Catch ex As Exception
End Try
Try
If Button4.Enabled = True Then
myConnection.Open()
Dim str As String
str = "SELECT * FROM maintable WHERE location='" & Button4.Text & "' AND pumpingdate=#" & dateActive & "#"
Dim cmd As OleDbCommand = New OleDbCommand(str, myConnection)
dr = cmd.ExecuteReader
dr.Read()
If Button4.Text = dr("location").ToString And dateActive = dr("pumpingdate").ToString Then
Button4.Enabled = False
myConnection.Close()
Me.Refresh()
End If
End If
Catch ex As Exception
End Try
End Sub
您需要在catch塊中添加一些內容,以便查看錯誤消息
Try
If Button4.Enabled = True Then
myConnection.Open()
Dim str As String
str = "SELECT * FROM maintable WHERE location='" & Button4.Text & "' AND pumpingdate=#" & dateActive & "#"
Dim cmd As OleDbCommand = New OleDbCommand(str, myConnection)
dr = cmd.ExecuteReader
dr.Read()
If Button4.Text = dr("location").ToString And dateActive = dr("pumpingdate").ToString Then
Button4.Enabled = False
myConnection.Close()
Me.Refresh()
End If
End If
Catch ex As Exception
msgbox(ex.tostring)
End Try
這樣就可以看到問題所在
就更好的方法而言,您可以通過幾種方法做到這一點,其中大多數方法將更加高效且易於維護。 但是我建議的第一件事是將數據邏輯與顯示邏輯分開,例如,創建一個函數,如果應該禁用按鈕,則該函數返回一個布爾值
Public function isDisabled(ButtonName as string, dateActive as date) as boolean
Dim retval as boolean = false
myConnection.Open()
Dim str As String
str = "SELECT * FROM maintable WHERE location='" & ButtonName & "' AND pumpingdate=#" & dateActive & "#"
Dim cmd As OleDbCommand = New OleDbCommand(str, myConnection)
dr = cmd.ExecuteReader
dr.Read()
if dr.rows.count > 0 then
retval = true
else
retval = false
end if
myConnection.Close()
return retval
end function
然后,您可以在負載上執行類似的操作
try
if button4.enabled =true then
button4.enabled = isdisabled(button4.text, activedate)
end if
catch exp as exception
msgbox(exp.tostring)
end if
還有很多,但這只是一個開始
*某些語法可能是錯誤的,我是從手機的內存中執行此操作的
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.