Results 1 to 9 of 9

Thread: Loading a Dataset from another form

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2006
    Posts
    27

    Loading a Dataset from another form

    Hi,

    I apologise if this is a little basic however my knowledge of programming is fairly none existent. Basically what im doing is selecting some search critera with one form and with a second form i want the results to load in a datagrid when the button is selected on the first form. I have managed to get the data to load on the first form in a datagrid as a test so i know that it all works. The problem i am having is passing the dataset to form 2 to pick up and load in the datagrid. Would anybody know a way of doing this that is fairly simple to implement. I have included to code below to show how i have initiated the data etc.

    thanks for any help

    johnuk4

    Code:
                   SQLStr = "SELECT Hardware_Problems.Hardware_Problem_Id, Hardware.Hardware_Type, Hardware.Hardware_Make, Hardware.Hardware_Model, Hardware_Problems.Date_Time, Hardware_Problems.Staff_Reported, Hardware_Problems.Problem_Description, Hardware_Problems.Location FROM Hardware INNER JOIN Hardware_Problems ON Hardware.Hardware_Id = Hardware_Problems.Hardware_Id;"
    
                    DatabaseTable = "Hardware_Problems"
    
                Case Is = "All Other Problems"
    
                Case Is = "In Location"
    
                    'msgbox for if user hits search without selecting an input
                Case Else
                    MsgBox("Please select search criteria", MsgBoxStyle.OKOnly, "Error")
            End Select
    
    
            With dbAdaptr
                .TableMappings.Add("Table", DatabaseTable)
    
                SQLCode = SQLStr
                SQLStr = SQLCode
    
                cmd = New System.Data.OleDb.OleDbCommand(SQLStr, DBCon)
    
                cmd.CommandType = CommandType.Text
                .SelectCommand = cmd
                .Fill(DSet)
                .Dispose()
    
            End With
    
            DSet.AcceptChanges()
    
            DSet.Dispose()
    
            DBCon.Close()
    
            DataGrid1.SetDataBinding(DSet, DatabaseTable)
    
            ProgressBar1.Value = 100
        End Sub

  2. #2
    Addicted Member bgard68's Avatar
    Join Date
    Mar 2006
    Location
    Arkansas
    Posts
    164

    Re: Loading a Dataset from another form

    Here is how you pass variables from one form to another:

    Form1:
    Private dsForm1 as new DataSet

    Form2:
    Private dsForm2 as new DataSet

    Form1:
    Dim frm2 as new Form2
    dsForm2 = dsForm1
    frm2.ShowDialog
    'Do some more stuff maybe ?
    frm2.close
    Using Framework 1.1, VB.Net 2003 unless I
    state otherwise

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Mar 2006
    Posts
    27

    Re: Loading a Dataset from another form

    Hi thanks for the reply,

    I have tried wot you recommended, however it does not actually pass the variables to form 2. When i call the variable dsForm2 in form 2 it says it has not been declared, obviously as i declared it as a variable in form 1. Do you know if i can pass the value without this problem

    thanks again

    johnuk4

  4. #4
    Addicted Member bgard68's Avatar
    Join Date
    Mar 2006
    Location
    Arkansas
    Posts
    164

    Re: Loading a Dataset from another form

    After the Windows Generated Code Section,

    Do you have Private dsForm2 as new dataset ?


    there is another way, but its a little more complex...

    1. Create a seperate class file, say clsVars.vb
    2. Inside clsVars.Vb: Declare a public property dataset
    3. Inside of whatever form: Private clsMyVars as new clsVars after Windows Generated Code
    4. clsMyVars.dsMyDataSet = dsForm1
    5. Form 2: Private clsVarsForm2 as new clsVars
    6. Form 2: you have access to clsVarsForm2
    Using Framework 1.1, VB.Net 2003 unless I
    state otherwise

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Mar 2006
    Posts
    27

    Re: Loading a Dataset from another form

    yeah, do i have to declare DataSetForm2 on form 1 as well as form 2? sorry i got the above the wrong way round, Its actually on form 1 that it says variable not been declared.

    thanks

  6. #6
    Addicted Member bgard68's Avatar
    Join Date
    Mar 2006
    Location
    Arkansas
    Posts
    164

    Re: Loading a Dataset from another form

    Check out this link,

    it might clear some things up for you...

    http://www.devcity.net/Articles/94/1...#FeedbackPanel
    Using Framework 1.1, VB.Net 2003 unless I
    state otherwise

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Mar 2006
    Posts
    27

    Re: Loading a Dataset from another form

    Hi,

    Thanks for the link it was helpful. Iv tried to set up the class method that u stated but unable to get it to pick up the dataset and another variable i passed in form 2. Could you possibly have a quick look at my code below to see if i have implemented this properly. (Iv Highlighted all of the class areas)

    thanks again

    Form 1 Code:

    Code:
    Public Class MainScreenForm
        Inherits System.Windows.Forms.Form
        Public frmHardwareProblems As New HardwareType
        Public frmSoftwareProblems As New ReportSoftwareProblem
        Public frmOtherProblems As New ReportOtherProblem
        Public SearchResultsForm As New SearchResults
        Dim tRow As DataRow, tTbl As DataTable
        Dim ConString As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=""ProblemDatabase.mdb"";"
        Dim DBCon As New System.Data.OleDb.OleDbConnection(ConString)
    
    #Region " Windows Form Designer generated code "
    
        Public Sub New()
            MyBase.New()
    
            'This call is required by the Windows Form Designer.
            InitializeComponent()
    
            'Add any initialization after the InitializeComponent() call
    
        End Sub
    
        'Form overrides dispose to clean up the component list.
        Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
            If disposing Then
                If Not (components Is Nothing) Then
                    components.Dispose()
                End If
            End If
            MyBase.Dispose(disposing)
        End Sub
    
         Private clsMyVars As New clsVars
    
        Private Sub MainScreenForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            With cboSelectOption
                .Items.Add("View All Problems")
                .Items.Add("All Software Problems")
                .Items.Add("All Hardware Problems")
                .Items.Add("All Other Problems")
                .Items.Add("In Location")
                .SelectedIndex = 0 'Set first item as selected item. 
            End With
        End Sub
    
        Private Sub btnHardwareProb_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHardwareProb.Click
            Me.Close()
            frmHardwareProblems.ShowDialog()
        End Sub
    
        Private Sub btnSoftwareProb_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSoftwareProb.Click
            Me.Close()
            frmSoftwareProblems.ShowDialog()
        End Sub
    
        Private Sub btnOtherProb_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOtherProb.Click
            Me.Close()
            frmOtherProblems.ShowDialog()
        End Sub
    
        Private Sub MenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem2.Click
            Me.Close()
        End Sub
    
    
        Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
    
    
            Dim SelectedOption As String
            Dim SQLCode As String
    
            'variable for selecting what table to be used in the database
            Dim DatabaseTable As String
    
            ProgressBar1.Value = 0
    
            DBCon.Open()
    
            Dim cmd As System.Data.OleDb.OleDbCommand
    
            Dim dbAdaptr As System.Data.OleDb.OleDbDataAdapter = New System.Data.OleDb.OleDbDataAdapter
            Dim DataSet1 As New DataSet, SQLStr As String
    
            SelectedOption = cboSelectOption.Text
    
            'select the correct sql query string depending on what user selects
    
            Select Case SelectedOption
    
                Case Is = "View All Problems"
    
    
                Case Is = "All Software Problems"
    
                    SQLStr = "SELECT Software_Problems.Software_Problem_Id, Software.Software_Company, Software.Software_Name, Software.Software_Version, Hardware.Hardware_Make, Hardware.Hardware_Model, Software_Problems.Date_Time, Software_Problems.Staff_Reported, Software_Problems.Problem_Description, Software_Problems.Location FROM Hardware INNER JOIN (Software INNER JOIN Software_Problems ON Software.Software_Id = Software_Problems.Software_Id) ON Hardware.Hardware_Id = Software_Problems.Hardware_Id;"
    
                    DatabaseTable = "Software_Problems"
    
                Case Is = "All Hardware Problems"
    
                    SQLStr = "SELECT Hardware_Problems.Hardware_Problem_Id, Hardware.Hardware_Type, Hardware.Hardware_Make, Hardware.Hardware_Model, Hardware_Problems.Date_Time, Hardware_Problems.Staff_Reported, Hardware_Problems.Problem_Description, Hardware_Problems.Location FROM Hardware INNER JOIN Hardware_Problems ON Hardware.Hardware_Id = Hardware_Problems.Hardware_Id;"
    
                    DatabaseTable = "Hardware_Problems"
    
                Case Is = "All Other Problems"
    
                Case Is = "In Location"
    
                    GroupBox3.Visible = True
    
                    'msgbox for if user hits search without selecting an input
                Case Else
                    MsgBox("Please select search criteria", MsgBoxStyle.OKOnly, "Error")
            End Select
    
    
            With dbAdaptr
                .TableMappings.Add("Table", DatabaseTable)
    
                SQLCode = SQLStr
                SQLStr = SQLCode
    
                cmd = New System.Data.OleDb.OleDbCommand(SQLStr, DBCon)
    
                cmd.CommandType = CommandType.Text
                .SelectCommand = cmd
                .Fill(DataSet1)
                .Dispose()
    
            End With
    
            DataSet1.AcceptChanges()
    
            DataSet1.Dispose()
    
            DBCon.Close()
    
            'DataGrid1.SetDataBinding(DataSet1, DatabaseTable)
    
            ProgressBar1.Value = 100
    
            'setting dataset to pass variable to results form
            clsMyVars.DatabaseTableSearch = DatabaseTable
            clsMyVars.DataSet1 = DataSet1
    
            SearchResultsForm.ShowDialog()
        End Sub
    
    
    End Class


    Form 2 code:

    Code:
    Public Class SearchResults
        Inherits System.Windows.Forms.Form
    
    
    #Region " Windows Form Designer generated code "
    
        Public Sub New()
            MyBase.New()
    
            'This call is required by the Windows Form Designer.
            InitializeComponent()
    
            'Add any initialization after the InitializeComponent() call
    
        End Sub
    
        'Form overrides dispose to clean up the component list.
        Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
            If disposing Then
                If Not (components Is Nothing) Then
                    components.Dispose()
                End If
            End If
            MyBase.Dispose(disposing)
        End Sub
    
            Private clsVarsForm2 As New clsVars
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Me.Close()
        End Sub
    
    
        Private Sub SearchResults_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            DataGrid1.SetDataBinding(clsVarsForm2.DataSet1, clsVarsForm2.DatabaseTableSearch)
        End Sub
    End Class
    Class File code:

    Code:
    Public Class clsVars
        Public DataSet1 As New DataSet
        Public DatabaseTableSearch As String
    End Class

    Sorry about all the system code

    thanks john
    Last edited by johnuk4; Mar 10th, 2006 at 02:13 PM.

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Mar 2006
    Posts
    27

    Re: Loading a Dataset from another form

    Hi, Thanks for all your help, i managed to crack it in the end using the first method you showed me

    thanks

    johnuk4

  9. #9
    Addicted Member bgard68's Avatar
    Join Date
    Mar 2006
    Location
    Arkansas
    Posts
    164

    Re: Loading a Dataset from another form

    Here is a example...

    Make sure you put the database in your c:.
    Attached Files Attached Files
    Using Framework 1.1, VB.Net 2003 unless I
    state otherwise

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width