title: Visual Studio 2015 VB.net How to find record in MS Access database and disable button in form?
body: <p>i am trying to load a form, but when loading must go to a access database and check if a record with the button text and today date exist, if it exist then disable the button. I have about 50 button in the form. NOTE: check my codes below, they works for 4 or 5 but the other ones not working. i have no error message. Maybe if someone have a better way to do this i will be very thanks full .<a href="http://i.stack.imgur.com/SdBZm.png" rel="nofollow">This is my loading form</a></p>
  
  <pre><code>Imports System.Data.OleDb
  </code></pre>
  
  <p>Public Class Wells
      Dim provider As String
      Dim dataFile As String
      Dim connString As String
      Public myConnection As OleDbConnection = New OleDbConnection
      Public dr As OleDbDataReader
      Public da As OleDbDataAdapter
      Dim Errors As String</p>
  
  <pre><code>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 &amp; dataFile
      myConnection.ConnectionString = connString
      Errors = ""
  
      Try
          If Button19.Enabled = True Then
  
              myConnection.Open()
              Dim str As String
              str = "SELECT * FROM maintable WHERE location='" &amp; Button19.Text &amp; "' AND pumpingdate=#" &amp; dateActive &amp; "#"
              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='" &amp; Button23.Text &amp; "' AND pumpingdate=#" &amp; dateActive &amp; "#"
              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='" &amp; Button17.Text &amp; "' AND pumpingdate=#" &amp; dateActive &amp; "#"
              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='" &amp; Button3.Text &amp; "' AND pumpingdate=#" &amp; dateActive &amp; "#"
              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='" &amp; Button20.Text &amp; "' AND pumpingdate=#" &amp; dateActive &amp; "#"
              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='" &amp; Button1.Text &amp; "' AND pumpingdate=#" &amp; dateActive &amp; "#"
              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='" &amp; Button2.Text &amp; "' AND pumpingdate=#" &amp; dateActive &amp; "#"
              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='" &amp; Button18.Text &amp; "' AND pumpingdate=#" &amp; dateActive &amp; "#"
              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='" &amp; Button7.Text &amp; "' AND pumpingdate=#" &amp; dateActive &amp; "#"
              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='" &amp; Button16.Text &amp; "' AND pumpingdate=#" &amp; dateActive &amp; "#"
              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='" &amp; Button15.Text &amp; "' AND pumpingdate=#" &amp; dateActive &amp; "#"
              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='" &amp; Button14.Text &amp; "' AND pumpingdate=#" &amp; dateActive &amp; "#"
              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='" &amp; Button13.Text &amp; "' AND pumpingdate=#" &amp; dateActive &amp; "#"
              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='" &amp; Button12.Text &amp; "' AND pumpingdate=#" &amp; dateActive &amp; "#"
              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='" &amp; Button11.Text &amp; "' AND pumpingdate=#" &amp; dateActive &amp; "#"
              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='" &amp; Button10.Text &amp; "' AND pumpingdate=#" &amp; dateActive &amp; "#"
              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='" &amp; Button9.Text &amp; "' AND pumpingdate=#" &amp; dateActive &amp; "#"
              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='" &amp; Button8.Text &amp; "' AND pumpingdate=#" &amp; dateActive &amp; "#"
              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='" &amp; Button6.Text &amp; "' AND pumpingdate=#" &amp; dateActive &amp; "#"
              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='" &amp; Button5.Text &amp; "' AND pumpingdate=#" &amp; dateActive &amp; "#"
              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='" &amp; Button4.Text &amp; "' AND pumpingdate=#" &amp; dateActive &amp; "#"
              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
  </code></pre>