Pergunta

I'm using VB.net and passing data from one form to another while it is opening to load data to a header on the second form. The data that is passed is a JobID key that is used in an SQL statment that will call the data from a database that is then used to populate the static header on form 2.

So here is the problem, I can transfer the data, however, the header will not populate unless I have a msgbox in the code right before I fill the dataset. Here is the code, I'd love to get this to work without the msgbox.

Form1

Option Explicit On
Option Strict On
Imports System.Data.OleDb

Public Class assemblyForm
   Inherits System.Windows.Forms.Form
   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, ds3 As New DataSet
   Dim da As OleDb.OleDbDataAdapter, da2 As OleDb.OleDbDataAdapter, da3 As OleDb.OleDbDataAdapter
   Dim run As Integer, pcr As Integer, JobId2 As Integer, jobNum As Integer

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

    Try
        'Open DB connection
        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()

        'Autopopulate PartNo. textbox
        da = New OleDb.OleDbDataAdapter("SELECT PartNumber FROM PCR_INFO", dbConnect)

        da.Fill(ds, "list")

        Dim col As New AutoCompleteStringCollection
        Dim i As Integer
        For i = 0 To ds.Tables(0).Rows.Count - 1
            col.Add(ds.Tables(0).Rows(i)("PartNumber").ToString())
        Next

        txtPart.AutoCompleteSource = AutoCompleteSource.CustomSource
        txtPart.AutoCompleteCustomSource = col
        txtPart.AutoCompleteMode = AutoCompleteMode.Suggest

    Catch ex As Exception
        MessageBox.Show(ex.Message + Line + "Main Database Not Found" + Line + "Check form_AccessMaintenance source code" + Line + "Database Path", "Critical Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Me.Close()
    End Try
End Sub

Private Sub btnSubmit_Click(sender As System.Object, e As System.EventArgs) Handles btnSubmit.Click
    'Increment the Run
    dbInsert.CommandText = "UPDATE PART_LIST SET Run = Run + 1 WHERE PartNumber='" & txtPart.Text & "'"
    dbInsert.CommandType = CommandType.Text
    dbInsert.Connection = dbConnect
    dbInsert.ExecuteNonQuery()

    'Gather Run from PART_LIST DB, put into variable
    da = New OleDb.OleDbDataAdapter("SELECT Run FROM PART_LIST WHERE PartNumber ='" & txtPart.Text & "'", dbConnect)

    da.Fill(ds, "Run")

    run = CInt(ds.Tables("Run").Rows(0).Item(0))

    'Gather PCR from PCR_INFO DB, put into variable

    da2 = New OleDb.OleDbDataAdapter("SELECT PCRNumber FROM PCR_INFO WHERE PartNumber ='" & txtPart.Text & "'", dbConnect)

    da2.Fill(ds2, "PCRNumber")

    pcr = CInt(ds2.Tables("PCRNumber").Rows(0).Item(0))



    'Set parameters for fields
    dbInsert.Parameters.Add(dbInsert.CreateParameter).ParameterName = "PartNumber"
    dbInsert.Parameters.Item("PartNumber").Value = txtPart.Text
    dbInsert.Parameters.Add(dbInsert.CreateParameter).ParameterName = "PCRNumber"
    dbInsert.Parameters.Item("PCRNumber").Value = pcr
    dbInsert.Parameters.Add(dbInsert.CreateParameter).ParameterName = "Run"
    dbInsert.Parameters.Item("Run").Value = run
    dbInsert.Parameters.Add(dbInsert.CreateParameter).ParameterName = "JobNo"
    dbInsert.Parameters.Item("JobNo").Value = txtJobNo.Text



    'Add Values into Assembly DB
    dbInsert.CommandText = "INSERT INTO Assembly(PartNumber,PCRNumber,Run,JobNo) VALUES(txtPart.Text,pcr,run,txtJobNo.Text);"
    dbInsert.CommandType = CommandType.Text
    dbInsert.Connection = dbConnect
    dbInsert.ExecuteNonQuery()
    MessageBox.Show("Job has been successfully submitted" + Line + txtPart.Text)


    'Gather JobID from new job

    da3 = New OleDb.OleDbDataAdapter("SELECT JobID FROM Assembly WHERE PartNumber ='" & txtPart.Text & "' AND JobNo ='" & txtJobNo.Text & "'", dbConnect)

    da3.Fill(ds3, "JobID")

    JobId2 = CInt(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
    Dim anotherForm As Vinyl
    anotherForm = New Vinyl(JobId2)
    anotherForm.Show()


End Sub

Form 2

Option Explicit On
Option Strict On
Public Class Vinyl

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, jobnumber As Integer
Dim Pdescription As String

' Bring data from previous form and load the header
Public Sub New(ByVal JobNum As Integer)
    MyBase.New()
    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)
    'Have to have a msgbox right here in order for this cod
    'MsgBox(jobnumber)
    da.Fill(ds, "Molding")


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

    lblPart.Text = PartNumber
    lblPCR.Text = CStr(PCR)
    lblRun.Text = CStr(run)

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

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

    lblPartDescription.Text = CStr(Pdescription)
End Sub

End Class

Foi útil?

Solução

Well I figured it out, I just needed to close the dbconnection on form1 after the dbinsert.executenonquery. My hunch is that the db never lost focus, so the program couldn't grab the jobnumber value because it was still open and being written to. When the messagebox was placed there it took focus from the db. so here is the updated code.

 Option Explicit On
Option Strict On
Imports System.Data.OleDb

Public Class assemblyForm
   Inherits System.Windows.Forms.Form
   Dim dbInsert As New OleDb.OleDbCommand
   Dim dbConnect As New OleDb.enter code here`
   Dim Line As String = Environment.NewLine
   Dim ds As New DataSet, ds2 As New DataSet, ds3 As New DataSet
   Dim da As OleDb.OleDbDataAdapter, da2 As OleDb.OleDbDataAdapter, da3 As OleDb.OleDbDataAdapter
   Dim run As Integer, pcr As Integer, JobId2 As Integer, jobNum As Integer

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

    Try
        'Open DB connection
        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()

        'Autopopulate PartNo. textbox
        da = New OleDb.OleDbDataAdapter("SELECT PartNumber FROM PCR_INFO", dbConnect)

        da.Fill(ds, "list")

        Dim col As New AutoCompleteStringCollection
        Dim i As Integer
        For i = 0 To ds.Tables(0).Rows.Count - 1
            col.Add(ds.Tables(0).Rows(i)("PartNumber").ToString())
        Next

        txtPart.AutoCompleteSource = AutoCompleteSource.CustomSource
        txtPart.AutoCompleteCustomSource = col
        txtPart.AutoCompleteMode = AutoCompleteMode.Suggest

    Catch ex As Exception
        MessageBox.Show(ex.Message + Line + "Main Database Not Found" + Line + "Check form_AccessMaintenance source code" + Line + "Database Path", "Critical Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Me.Close()
    End Try
End Sub

Private Sub btnSubmit_Click(sender As System.Object, e As System.EventArgs) Handles btnSubmit.Click
    'Increment the Run
    dbInsert.CommandText = "UPDATE PART_LIST SET Run = Run + 1 WHERE PartNumber='" & txtPart.Text & "'"
    dbInsert.CommandType = CommandType.Text
    dbInsert.Connection = dbConnect
    dbInsert.ExecuteNonQuery()

    'Gather Run from PART_LIST DB, put into variable
    da = New OleDb.OleDbDataAdapter("SELECT Run FROM PART_LIST WHERE PartNumber ='" & txtPart.Text & "'", dbConnect)

    da.Fill(ds, "Run")

    run = CInt(ds.Tables("Run").Rows(0).Item(0))

    'Gather PCR from PCR_INFO DB, put into variable

    da2 = New OleDb.OleDbDataAdapter("SELECT PCRNumber FROM PCR_INFO WHERE PartNumber ='" & txtPart.Text & "'", dbConnect)

    da2.Fill(ds2, "PCRNumber")

    pcr = CInt(ds2.Tables("PCRNumber").Rows(0).Item(0))



    'Set parameters for fields
    dbInsert.Parameters.Add(dbInsert.CreateParameter).ParameterName = "PartNumber"
    dbInsert.Parameters.Item("PartNumber").Value = txtPart.Text
    dbInsert.Parameters.Add(dbInsert.CreateParameter).ParameterName = "PCRNumber"
    dbInsert.Parameters.Item("PCRNumber").Value = pcr
    dbInsert.Parameters.Add(dbInsert.CreateParameter).ParameterName = "Run"
    dbInsert.Parameters.Item("Run").Value = run
    dbInsert.Parameters.Add(dbInsert.CreateParameter).ParameterName = "JobNo"
    dbInsert.Parameters.Item("JobNo").Value = txtJobNo.Text



    'Add Values into Assembly DB
    dbInsert.CommandText = "INSERT INTO Assembly(PartNumber,PCRNumber,Run,JobNo) VALUES(txtPart.Text,pcr,run,txtJobNo.Text);"
    dbInsert.CommandType = CommandType.Text
    dbInsert.Connection = dbConnect
    dbInsert.ExecuteNonQuery()
    MessageBox.Show("Job has been successfully submitted" + Line + txtPart.Text)


    'Gather JobID from new job

    da3 = New OleDb.OleDbDataAdapter("SELECT JobID FROM Assembly WHERE PartNumber ='" & txtPart.Text & "' AND JobNo ='" & txtJobNo.Text & "'", dbConnect)

    da3.Fill(ds3, "JobID")

    JobId2 = CInt(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()
    dbconnect.Close()

    'Loads the newly created jobid into the vinyl form
    Dim anotherForm As Vinyl
    anotherForm = New Vinyl(JobId2)
    anotherForm.Show()


End Sub
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top