为什么这个代码不在Form Load上运行,只在按钮点击时运行?

时间:2014-02-28 19:53:21

标签: vb.net winforms ms-access

我试过表单show,activate,load none work。我可以让这个代码工作的唯一方法是点击一下按钮。反正有自动化btnclick吗?可能我们可以弄清楚为什么这段代码不能用于表单加载。

这里有一个小上下文,我有一个用户输入一个新工作,我已经加载了输入到上一个表单的值,显示在当前表单的顶部。只需按一下按钮,它就可以工作。为什么?有任何想法吗?下面的BTW激活将其抛入无限循环。

    Public Class Vinyl
    Dim jobnumber As Integer

    Sub New()
        InitializeComponent()
    End Sub

    'Collects the newely created job id from previous form
    Public Property JobID() As Integer
        Get
            Return jobnumber
        End Get
        Set(ByVal Value As Integer)
            jobnumber = Value
        End Set
    End Property
    Dim dbInsert As New OleDb.OleDbCommand
    Dim dbConnect As New OleDb.OleDbConnection
    Dim Line As String = Environment.NewLine
    Dim ds As New DataSet, ds2 As New DataSet
    Dim da As OleDb.OleDbDataAdapter, da2 As OleDb.OleDbDataAdapter
    Dim PartNumber As String, PartDescription As String
    Dim PCR As Integer, run As Integer
    Dim Pdescription As Object

    Public Sub New(jobNum As Integer)
        InitializeComponent()
        jobnumber = jobNum
    End Sub

    Private Sub Vinyl_Load(sender As Object, e As System.EventArgs) Handles Me.Load

        dbConnect.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\crabara\Desktop\Project Alpha 3\MDB.accdb;Persist Security Info=False;"
        dbConnect.Open()


        da = New OleDb.OleDbDataAdapter("SELECT PartNumber,PCRNumber,Run FROM Molding WHERE JobID ='" & jobnumber & "'", dbConnect)

        da.Fill(ds, "Molding")

        PartNumber = ds.Tables("Molding").Rows(0).Item(0)
        PCR = ds.Tables("Molding").Rows(0).Item(1)
        run = ds.Tables("Molding").Rows(0).Item(2)

        lblPartNumber.Text = PartNumber
        lblPCR.Text = PCR
        lblRun.Text = run

        da2 = New OleDb.OleDbDataAdapter("SELECT PartDescription FROM PART_LIST WHERE PartNumber ='" & PartNumber & "'", dbConnect)

        da2.Fill(ds2, "PartDescription")

        PartDescription = ds2.Tables("PartDescription").Rows(0).Item(0)

        lblPartDescription.Text = Pdescription

    End Sub

    Private Sub InitializeComponents()
        Throw New NotImplementedException
    End Sub

End Class

这是上一个表单中与jobnumber变量相关的代码片段。

 JobId2 = ds3.Tables("JobID").Rows(0).Item(0)

    dbInsert.Parameters.Add(dbInsert.CreateParameter).ParameterName = "JobID"
    dbInsert.Parameters.Item("JobID").Value = JobId2


    'Add Values into Vinyl DB
    dbInsert.CommandText = "INSERT INTO Molding(PartNumber,PCRNumber,Run,JobNo,JobID) VALUES(txtPart.Text,pcr,run,txtJobNo.Text,JobId2);"
    dbInsert.CommandType = CommandType.Text
    dbInsert.Connection = dbConnect
    dbInsert.ExecuteNonQuery()

    'Loads the newly created jobid into the vinyl form
    myForm2.JobID = JobId2
    myForm2.Show()

1 个答案:

答案 0 :(得分:3)

我想我想出来了。您依赖于代码中的JobNumber变量,但看起来直到加载表单后才设置它。由于它为零,您的查询将返回空白,因为您的代码只是假设有记录,当您尝试访问不存在的行或列时会出现异常。

如果JobNumber变量对表单至关重要,请尝试将其传递给构造函数:

Public Sub New(jobNum As Integer)
  InitializeComponent()
  jobnumber = jobNum