I have two forms and I am running a sql query to provide a set of variables in my first form that I need to pass to my second form. Here is the code that I have:
| Imports System.Data.SqlClient Public Class Main Dim instForm2 As New Exceptions Public payrollstartdate As Date = Nothing Public payrollenddate As Date = Nothing Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles startpayrollButton.Click Dim ssql As String = "select MAX(payrolldate) AS [payrolldate], " & _ "dateadd(dd, ((datediff(dd, '17530107', MAX(payrolldate))/7)*7)+7, '17530107') AS [Sunday]" & _ "from dbo.payroll" & _ " where payrollran = 'no'" Dim oCmd As System.Data.SqlClient.SqlCommand Dim oDr As System.Data.SqlClient.SqlDataReader oCmd = New System.Data.SqlClient.SqlCommand Try With oCmd .Connection = New System.Data.SqlClient.SqlConnection("Initial Catalog=mdr;Data Source=xxxxx;uid=xxxxx;password=xxxxx") .Connection.Open() .CommandType = CommandType.Text .CommandText = ssql oDr = .ExecuteReader() End With If oDr.Read Then payPeriodStartDate = oDr.GetDateTime(1) payPeriodEndDate = payPeriodStartDate.AddDays(7) Dim ButtonDialogResult As DialogResult ButtonDialogResult = MessageBox.Show(" The Next Payroll Start Date is: " & payPeriodStartDate.ToString() & System.Environment.NewLine & " Through End Date: " & payPeriodEndDate.ToString()) If ButtonDialogResult = Windows.Forms.DialogResult.OK Then exceptionsButton.Enabled = True startpayrollButton.Enabled = False End If End If oDr.Close() oCmd.Connection.Close() Catch ex As Exception MessageBox.Show(ex.Message) oCmd.Connection.Close() End Try End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles exceptionsButton.Click Dim sql As String = "SELECT [Exceptions].Employeenumber,[Exceptions].exceptiondate, [Exceptions].starttime, [exceptions].endtime, [Exceptions].code, datediff(minute, starttime, endtime) as duration INTO scratchpad3" & _ " FROM Employees INNER JOIN Exceptions ON [Exceptions].EmployeeNumber = [Exceptions].Employeenumber" & _ " where [Exceptions].exceptiondate between @payperiodstartdate and @payperiodenddate" & _ " GROUP BY [Exceptions].Employeenumber, [Exceptions].Exceptiondate, [Exceptions].starttime, [exceptions].endtime," & _ " [Exceptions].code, [Exceptions].exceptiondate" Dim oCmd As System.Data.SqlClient.SqlCommand Dim oDr As System.Data.SqlClient.SqlDataReader oCmd = New System.Data.SqlClient.SqlCommand Try With oCmd .Connection = New System.Data.SqlClient.SqlConnection("Initial Catalog=mdr;Data Source=xxxxx;uid=xxxxx;password=xxxxx") .Connection.Open() .CommandType = CommandType.Text .CommandText = sql .Parameters.AddWithValue("@payperiodstartdate", payPeriodStartDate) .Parameters.AddWithValue("@payperiodenddate", payPeriodEndDate) oDr = .ExecuteReader() End With oDr.Close() oCmd.Connection.Close() Catch ex As Exception MessageBox.Show(ex.Message) oCmd.Connection.Close() End Try Exceptions.Show() End Sub Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load End Sub End Class |
The values that I'm declaring are payrollstartdate and payrollenddate and then I need to pass the values that you can see on these two lines:
payPeriodStartDate = oDr.GetDateTime(1)
payPeriodEndDate = payPeriodStartDate.AddDays(7)
to form 2 which then shows the data retrieved from a sql query in the load event to a datagrid view. Can anyone offer any kind of assistance with my code on how to properly do this?
Thank you
Doug
Suthish NairPosted Jan 19, 2011, 3:36 PM
Save your values to hidden labels and access control value from other form.
How to Pass Control Values between Windows FormsAlso refer Our recommended articles section.
Doug AncilPosted Jan 19, 2011, 3:21 PM
Actually what I did was to create a module:
Module Module1
Public payperiodstartdate As Date
Public payperiodenddate As Date
End Module
Sam HobbsPosted Jan 19, 2011, 3:16 PM
Also, there needs to be code to put the data from Variablename to the form and then get it from the form if needed. All that needs to be coordinated to occur at the right times. It is easy to do for those of us that know how to do it but for beginners it helps to have samples and there are ample samples and articles available so that is what I suggest that Doug use.
Sriram RamaniPosted Jan 19, 2011, 3:53 AM
Sam HobbsPosted Jan 18, 2011, 9:51 PM