Solved

calling a subform passing data

Posted on 2004-08-17
5
166 Views
Last Modified: 2010-04-23
I have a question that should be simple, but has me stumped.  I can probably find an ugly way to do this, but it seems to be something that should be able to be handled in a straightforward way.

I have a form with a listview control.  When the user doubleclicks a row, I want to call a separate form where the user can edit the various fields that make up the record the user clicked on the first form.

I have no problem with the handling of the listview and so on.  My question is simply:  How do I open another form, populating fields with data from the first form?

As a side question (but still related), I have an imagelist on the first form.  I would like to refer to the same images on my second form.  Is it better to recreate the imagelist (I'd think not), refer back to the original form, or pass the image list to the second form?  How would I go about doing this?

Thanks,
Chris
0
Comment
Question by:chlade
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 7

Expert Comment

by:natloz
ID: 11825724
Here is a sample where I am in form frmAFEMaintenance, and based on a DATAGRID selection I query and populate field in frmAFEDetail before I open it...

'********************************
    'User wants to edit an AFE Detail
    '********************************
    Private Sub btnEditItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEditItem.Click
        Dim bFail As Boolean = False
        Dim oDs As New DataSet("dsAFE") 'Dataset object
        Dim cDB As clsDBConn = New clsDBConn 'Connection Class
        Dim strConn As String = cDB.getStrConn 'Get connection string for database
        Dim oConn As New SqlConnection 'SQL connection object
        Dim oComm As SqlCommand 'SQL Command object

        'Connect to SQL server and validate login
        Try
            'Pass connection string to SqlConnection
            oConn.ConnectionString = strConn

            '*************************
            'Load the AFE Detail Table
            '*************************
            'SQL Database statements
            oComm = New SqlCommand
            oComm.CommandType = CommandType.StoredProcedure
            oComm.CommandText = "spGetAFEDetail"
            oComm.Connection = oConn

            'Send in parameters
            oComm.Parameters.Add(New SqlParameter("@pkAFEDetailID", SqlDbType.Int)).Value = 0

            'create and fill the Data Adapter
            Dim oDa As New SqlDataAdapter(oComm) 'SQL Data Adapter object
            oDa.Fill(oDs, "tblAFEDetail")
        Catch
            MsgBox("Error connecting to SQL Server")
        End Try

        If dgAFEDetailGrid.VisibleRowCount < 1 Then
            MsgBox("No AFE Detail is selected in the Grid.")
        Else
            Me.Cursor = System.Windows.Forms.Cursors.WaitCursor
            Dim oSelectedCell As DataGridCell
            Dim oSelectedItem As Object
            Dim intAFEDetailID As String

            oSelectedCell = dgAFEDetailGrid.CurrentCell
            oSelectedItem = dgAFEDetailGrid.Item(oSelectedCell.RowNumber, 0)
            intAFEDetailID = CStr(oSelectedItem)

            'All is well, get dteModifed
            If bFail = False Then
                Dim frmAFEDetail As frmAFEDetail = New frmAFEDetail

                'Associate the dataview to get Date Modified
                dvMaintenance.Table = oDs.Tables("tblAFEDetail")
                Dim drv As DataRowView 'Data Row View object to query DataView object

                dvMaintenance.RowFilter = "[pkAFEDetailID] = " & intAFEDetailID

                For Each drv In dvMaintenance
                    _dteDateModified2 = drv("dteDateModified")
                    frmAFEDetail.txtHiddenID.Text = CStr(drv("pkAFEDetailID")) 'Means it is an UPDATE
                Next

                If btnEditItem.Text = "View Item" Then
                    frmAFEDetail.lblEdit.Text = "Read Only"
                    frmAFEDetail.btnSaveItem.Enabled = False
                    frmAFEDetail.btnNewItem.Enabled = False
                Else
                    frmAFEDetail.lblEdit.Text = "Edit Mode"
                End If


                '**********************SETTING FIELDS ON SECOND FORM*****************
                'Open User form and initialize
                frmAFEDetail.txtHiddenID2.Text = txtHiddenID.Text 'Send the header ID for FK
                frmAFEDetail.btnClearTop.Visible = False
                frmAFEDetail.btnClearMiddle.Visible = False
                frmAFEDetail.btnClearBottom.Visible = False
                frmAFEDetail.ShowDialog()

                'Refresh Main screen data
                oDsGrid.Reset()
                LoadDataGrid()

                _bolRefreshMain = True 'Reset the refresh flag

                Me.Activate()
                Me.Cursor = System.Windows.Forms.Cursors.Default
            End If
        End If
    End Sub
0
 
LVL 18

Accepted Solution

by:
DotNetLover_Baan earned 500 total points
ID: 11825796
Hi there,
use module..., declare all the forms and instantiate them. Now you use any form from any other form.
** First add a module into the current project by right clicking on the project in solution explorer

Module MyModule
    Public F1 As New Form01()
    Public F2 As New Form02()
    Public F3 As New Form03()

    Sub main()              ' Now start the running sequence from this main.
        F1.ShowDialog()   'this is the starting form
    End Sub
End Module

Now, from Form01 to open form02, just use..
F2.ShowDialog()  or   F2.Show()

Pass the variable values...    F2.TextBoxInF2.Text = TextBoxInF1.Text   'TextBoxInF1 is in Form01
(Friend/Public variables can be shared)
-Baan
0
 

Author Comment

by:chlade
ID: 11885651
I haven't forgot about this question but I got pulled off onto a different project and probably won't get back to this for a week or two.
0
 

Author Comment

by:chlade
ID: 11916035
Thanks natloz, but that didn't help me a lot.  It may work, but it was a little too much code for me to make sense of.

The second response was exactly what I needed.  Thanks.
0
 
LVL 2

Expert Comment

by:chrisryhal
ID: 12928703
You may be able to help me on this one.  Kinda sounds like the same thing.  But I am not too sure.

http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/VB_DOT_NET/Q_21257743.html
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question