Link to home
Start Free TrialLog in
Avatar of TSFLLC
TSFLLC

asked on

How to reference text on windows form while in public function

Below is a function I use in multiple forms to define my main table's dataset.

    Public Function GetMainTable() As DataSet
        Dim da As New SqlDataAdapter

        Try
            da = GetMainTableDA(True)
            da.Fill(dsMainTable, glTable)
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OKOnly Or MsgBoxStyle.Critical, Me.Text)
        End Try

        Return dsMainTable
    End Function

Also, from inside the activated form:

        dtMainTable = GetMainTable().Tables("invoice_header")


The first piece of code was previously contained in each form....very redundant and I wanted to clean it up.

I pasted it into my Module1.vb.  Needless to say it does not like the 'Me' reference when attempting to capture an error.

How would I add to/modify the code from within the original form and the code from my public function to get the appropriate form name in order to resolve my problem? I pray that it is as simple as it was in MS Access.....xText = Forms![formname].txtfieldname for example.


Thanks for any assistance you can give.

Phil
SOLUTION
Avatar of Brian Crowe
Brian Crowe
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Pass form name to the function like this

    Public Function GetMainTable(formName as String) As DataSet
        Dim da As New SqlDataAdapter

        Try
            da = GetMainTableDA(True)
            da.Fill(dsMainTable, glTable)
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OKOnly Or MsgBoxStyle.Critical, formName)
        End Try

        Return dsMainTable
    End Function

Modify form code

        dtMainTable = GetMainTable(Me.Text).Tables("invoice_header")


What version VB.Net?
Avatar of TSFLLC
TSFLLC

ASKER

Version is 2003.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of TSFLLC

ASKER

BriCrowe,

I think I agree with you (even with my limited knowledge of dealing with errors in VB.NET) about messagebox elements and will go with it.  You get half the points.

Mike,

You already posted about 'Singleton' on my other question but I want to give you half the points here too.  Thanks....regarding your approach here, I have tried it but I have one glitch and I just realized the problem.  The GetMainTable() is not a function within the subsequent form but is a Public function in my Module.vb.

From Module1.....

Public Function GetMainTable() As DataSet
    ...DataAdapter
    ...Fill it
Return dataset
End Function

Public daMainTable as New SqlDataAdapter
Public dsMainTable as New DataSet

From InvoiceForm.....

Private dtMainTable as New DataTable
Private dvMainTable as New DataView

dtMainTable = GetMainTable().Tables("invoice_header")

This da/dsMainTable function is used for approximately 20 tables to populate a datagrid on windows form BrowseForm.  Once BrowseForm is populated the user can click on a specific row and the appropriate data entry form activates.  Once they edit/add/delete from data entry form I need to refresh the dataset/view/grid.

It is critical to my app.  Can we use Delegate Function even in a public function?  In fact 9-10 of these 20 tables are tables included in many combo boxes within many of my forms.  Is it programmatically appropriate to set up functions like the one above as public functions for all of my tables that are use in multiple locations?  It seems redundant to have the same code to fill datasets over and over instead of having them in one central location.
Avatar of TSFLLC

ASKER

BriCrowe & Mike,

The postings regarding errors/exceptions and use of Delegate have been implemented.  Both help tremendously.

Thanks to both of you!

Phil