[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How to reference text on windows form while in public function

Posted on 2006-05-12
7
Medium Priority
?
231 Views
Last Modified: 2010-04-23
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
0
Comment
Question by:TSFLLC
7 Comments
 
LVL 34

Assisted Solution

by:Brian Crowe
Brian Crowe earned 1000 total points
ID: 16668135
I think you should throw the exception in the module but capture it in your form

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

        Try
            da = GetMainTableDA(True)
            da.Fill(dsMainTable, glTable)
        Catch ex As Exception
            throw new exception("Error Obtaining Table: " + ex.Message)
        End Try

        Return dsMainTable
    End Function

from inside the activated form:

try
        dtMainTable = GetMainTable().Tables("invoice_header")
catch ex as exception
   messagebox.show(ex.message, MsgBoxStyle.OKOnly Or MsgBoxStyle.Critical, Me.Text)
end try

Generally I would avoid producing display elements like a messagebox from within a utility module since you can't know how the calling object will want to handle it.
0
 
LVL 12

Expert Comment

by:gbzhhu
ID: 16668192
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")


0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16669087
What version VB.Net?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:TSFLLC
ID: 16670295
Version is 2003.
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 1000 total points
ID: 16670424
There are several ways to go about it...here is one using a Delegate to point to your main forms function:

(assuming Form1 was your main form)

    Module Module1

        Public Delegate Function GetMainTableDelegate() As DataSet
        Public MainTable As GetMainTableDelegate

    End Module

    Public Class Form1
        Inherits System.Windows.Forms.Form

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            MainTable = New GetMainTableDelegate(AddressOf Me.GetMainTable)
        End Sub

        Private Function GetMainTable() As DataSet
            ' ... your code ...
        End Function

    End Class

Now from ANYWHERE you can use "MainTable" to refer to the DataSet:

    MainTable.XXX = "something"
    MainTable.XXX(yyy)

0
 

Author Comment

by:TSFLLC
ID: 16671236
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.
0
 

Author Comment

by:TSFLLC
ID: 16679805
BriCrowe & Mike,

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

Thanks to both of you!

Phil
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Integration Management Part 2
Loops Section Overview
Suggested Courses

873 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