繁体   English   中英

Visual Studio 2015 VB.net如何在MS Access数据库中查找记录并禁用表单中的按钮?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM