我正在使用VB.net并将数据从一个表单传递到另一个表单,同时它打开以将数据加载到第二个表单上的标题。传递的数据是在SQL语句中使用的JobID键,该SQL语句将从数据库调用数据,然后该数据库用于填充表单2上的静态标头。
所以这就是问题,我可以传输数据,但是,除非我在填充数据集之前在代码中有一个msgbox,否则不会填充标题。这是代码,我很乐意在没有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
表格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
结束班
答案 0 :(得分:0)
好吧,我想通了,我只需要在dbinsert.executenonquery之后关闭form1上的dbconnection。我的预感是db永远不会失去焦点,所以程序无法获取jobnumber值,因为它仍然是打开并被写入。当消息箱被放置在那里时,它从数据库获得了焦点。所以这是更新的代码。
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