Pass Windows Form value from Module problem

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello, I have a form called FrmMain that calls a function(LoadAllFormData()) from its formload event. The function is in a module called ProcessFunctions.vb. The function loads all of the frmMain dropdowns with data from a DB. the function is below. The function runs, but no dropdowns are populating with any data. I even tried to set a basic propery like 'TEXT" and I still couldnt get it to work. No errors, just no changes on FRMMAIN. I would imagine it has something to do with active form or something, but I cant seem to get anything to work. Thanks AL

Imports System.Dat
Imports System.Data.OleD
Imports CrystalDecisions.CrystalReports.Engin
Imports System.Windows.Form

Module ProcessFunction
Dim loading As Boolea

Public Function LoadAllFormData(
Dim LitDatabase As New SqlClient.SqlConnectio
Dim SQadapterTime As New SqlClient.SqlDataAdapte
Dim SQadapterPhase As New SqlClient.SqlDataAdapte
Dim SQadapterTask As New SqlClient.SqlDataAdapte
Dim SQadapterActivity As New SqlClient.SqlDataAdapte
Dim SQadapterCLient As New SqlClient.SqlDataAdapte
Dim SQadapterMatter As New SqlClient.SqlDataAdapte
Dim SQLTime As New SqlClient.SqlComman
Dim SQLPhase As New SqlClient.SqlComman
Dim SQLTask As New SqlClient.SqlComman
Dim SQLActivity As New SqlClient.SqlComman
Dim SQLClient As New SqlClient.SqlComman
Dim SQLMatter As New SqlClient.SqlComman
Dim dreader As SqlClient.SqlDataReade
Dim dtTime As New DataTabl
Dim dtPhase As New DataTabl
Dim dtTask As New DataTabl
Dim dtActivity As New DataTabl
Dim dtCLient As New DataTabl
Dim dtMatter As New DataTabl
Dim MainF As New FrmMai

LitDatabase.ConnectionString = "user id=sa;data source=RC0101;persist security info=True;initial catalog=LitigationData;password=SECRET

SQLTime.CommandText = "SELECT Timekeeper, EmpName FROM V_LiTimekeeper UNION SELECT 'Littimedetail.timekeeper', '-ALL-' FROM V_LiTimekeeper ORDER BY EmpName
SQLPhase.CommandText = "SELECT Timekeeper, EmpName FROM V_LiTimekeeper UNION SELECT 'Littimedetail.timekeeper', '-ALL-' FROM V_LiTimekeeper ORDER BY EmpName
SQLTask.CommandText = "SELECT Timekeeper, EmpName FROM V_LiTimekeeper UNION SELECT 'Littimedetail.timekeeper', '-ALL-' FROM V_LiTimekeeper ORDER BY EmpName
SQLActivity.CommandText = "SELECT Timekeeper, EmpName FROM V_LiTimekeeper UNION SELECT 'Littimedetail.timekeeper', '-ALL-' FROM V_LiTimekeeper ORDER BY EmpName
SQLClient.CommandText = "SELECT Timekeeper, EmpName FROM V_LiTimekeeper UNION SELECT 'Littimedetail.timekeeper', '-ALL-' FROM V_LiTimekeeper ORDER BY EmpName
SQLMatter.CommandText = "SELECT Timekeeper, EmpName FROM V_LiTimekeeper UNION SELECT 'Littimedetail.timekeeper', '-ALL-' FROM V_LiTimekeeper ORDER BY EmpName

SQLTime.Connection = LitDatabas
SQLPhase.Connection = LitDatabas
SQLTask.Connection = LitDatabas
SQLActivity.Connection = LitDatabas
SQLClient.Connection = LitDatabas
SQLMatter.Connection = LitDatabas

SQadapterTime.SelectCommand = SQLTim
SQadapterPhase.SelectCommand = SQLPhas
SQadapterTask.SelectCommand = SQLTas
SQadapterActivity.SelectCommand = SQLActivit
SQadapterCLient.SelectCommand = SQLClien
SQadapterMatter.SelectCommand = SQLMatte

' Load Timekeeper Dropdown
SQadapterTime.Fill(dtTime

MainF.DrpTimekeeper().DataSource = dtTim
MainF.DrpTimekeeper().DisplayMember = dtTime.Columns(1).ToStrin
MainF.DrpTimekeeper().ValueMember = dtTime.Columns(0).ToStrin

MainF.DrpTimekeeper().Text = "A TEST
End Functio
 
You have a new form created in the subroutine. This form occurence is never
shown so the processing you do is not with your FrmMain. What you need to
do is add a parameter to the LoadAllFormData function to pass the actual
form that you want to populate.

Also do you really need a Function. It has no return type.

Lloyd Sheen

Eric said:
Hello, I have a form called FrmMain that calls a
function(LoadAllFormData()) from its formload event. The function is in a
module called ProcessFunctions.vb. The function loads all of the frmMain
dropdowns with data from a DB. the function is below. The function runs, but
no dropdowns are populating with any data. I even tried to set a basic
propery like 'TEXT" and I still couldnt get it to work. No errors, just no
changes on FRMMAIN. I would imagine it has something to do with active form
or something, but I cant seem to get anything to work. Thanks ALL
Imports System.Data
Imports System.Data.OleDb
Imports CrystalDecisions.CrystalReports.Engine
Imports System.Windows.Forms

Module ProcessFunctions
Dim loading As Boolean

Public Function LoadAllFormData()
Dim LitDatabase As New SqlClient.SqlConnection
Dim SQadapterTime As New SqlClient.SqlDataAdapter
Dim SQadapterPhase As New SqlClient.SqlDataAdapter
Dim SQadapterTask As New SqlClient.SqlDataAdapter
Dim SQadapterActivity As New SqlClient.SqlDataAdapter
Dim SQadapterCLient As New SqlClient.SqlDataAdapter
Dim SQadapterMatter As New SqlClient.SqlDataAdapter
Dim SQLTime As New SqlClient.SqlCommand
Dim SQLPhase As New SqlClient.SqlCommand
Dim SQLTask As New SqlClient.SqlCommand
Dim SQLActivity As New SqlClient.SqlCommand
Dim SQLClient As New SqlClient.SqlCommand
Dim SQLMatter As New SqlClient.SqlCommand
Dim dreader As SqlClient.SqlDataReader
Dim dtTime As New DataTable
Dim dtPhase As New DataTable
Dim dtTask As New DataTable
Dim dtActivity As New DataTable
Dim dtCLient As New DataTable
Dim dtMatter As New DataTable
Dim MainF As New FrmMain


LitDatabase.ConnectionString = "user id=sa;data
source=RC0101;persist security info=True;initial
catalog=LitigationData;password=SECRET"
SQLTime.CommandText = "SELECT Timekeeper, EmpName FROM
V_LiTimekeeper UNION SELECT 'Littimedetail.timekeeper', '-ALL-' FROM
V_LiTimekeeper ORDER BY EmpName"
SQLPhase.CommandText = "SELECT Timekeeper, EmpName FROM
V_LiTimekeeper UNION SELECT 'Littimedetail.timekeeper', '-ALL-' FROM
V_LiTimekeeper ORDER BY EmpName"
SQLTask.CommandText = "SELECT Timekeeper, EmpName FROM
V_LiTimekeeper UNION SELECT 'Littimedetail.timekeeper', '-ALL-' FROM
V_LiTimekeeper ORDER BY EmpName"
SQLActivity.CommandText = "SELECT Timekeeper, EmpName FROM
V_LiTimekeeper UNION SELECT 'Littimedetail.timekeeper', '-ALL-' FROM
V_LiTimekeeper ORDER BY EmpName"
SQLClient.CommandText = "SELECT Timekeeper, EmpName FROM
V_LiTimekeeper UNION SELECT 'Littimedetail.timekeeper', '-ALL-' FROM
V_LiTimekeeper ORDER BY EmpName"
SQLMatter.CommandText = "SELECT Timekeeper, EmpName FROM
V_LiTimekeeper UNION SELECT 'Littimedetail.timekeeper', '-ALL-' FROM
V_LiTimekeeper ORDER BY EmpName"
 
Thanks Lloyd for the reply. I think I understand what you mean by passing the active reference of the form to the procedure. Could you give me an example of the call from form load, and the function changes? I agree this function returns nothing, which is then considered a procedure rather than a function, but I always use procedures as functions and functions as procedures. I was always under the school of thought that a function may OR may not return a value, where a procedure never returns a value. SO I always use a function regardless. This may be not be a good idea since most developers always assume that if it is a function, then there must be a return value. Then again I was always taught to never assume
Thanks for the reply, and a sample of what you suggested would be much sppreciated. thanks Lloyd, Eric
 
Change your code as follows:

public function aFunction (roForm as FormName)
.....
end function

then to call the function

aFunction (formInstance) where formInstance is the main form you have
already created.

The comments about function vs sub more have to do with reading. The
majority of developer reading the code would assume that a function is code
that returns a value and a sub does not not. Therefore a function would be
created to return information about an operation and sub would just do the
operation and return no status. Since you are not getting compile errors I
can only imagine that you are not using the Option Strict and Option
Explicit in your projects. This can (and most often will) lead to runtime
errors. Using the above options can help to ensure that the code is
prepared to handle object is the correct manner.

Hope this helps.

Lloyd Sheen

Eric said:
Thanks Lloyd for the reply. I think I understand what you mean by passing
the active reference of the form to the procedure. Could you give me an
example of the call from form load, and the function changes? I agree this
function returns nothing, which is then considered a procedure rather than a
function, but I always use procedures as functions and functions as
procedures. I was always under the school of thought that a function may OR
may not return a value, where a procedure never returns a value. SO I always
use a function regardless. This may be not be a good idea since most
developers always assume that if it is a function, then there must be a
return value. Then again I was always taught to never assume !
Thanks for the reply, and a sample of what you suggested would be much
sppreciated. thanks Lloyd, Eric
 
Thanks for your time Lloyd. Im am getting the following error when trying your metho
Additional information: Object reference not set to an instance of an object
"the error occured right at the first reference to the form object in the function
MainF.txtTEST.Text = "not again

I called the function from formLoad like thi
Private Sub FrmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Loa
Dim ActiveForm As FrmMai
LoadAllFormData(ActiveForm
end su

I then setup the function like thi
Public Function LoadAllFormData(ByVal MainF As FrmMain
Dim LitDatabase As New SqlClient.SqlConnectio
Dim SQadapterTime As New SqlClient.SqlDataAdapte
Dim SQadapterPhase As New SqlClient.SqlDataAdapte
Dim SQadapterTask As New SqlClient.SqlDataAdapte
Dim SQadapterActivity As New SqlClient.SqlDataAdapte
Dim SQadapterCLient As New SqlClient.SqlDataAdapte
Dim SQadapterMatter As New SqlClient.SqlDataAdapte
Dim SQLTime As New SqlClient.SqlComman
Dim SQLPhase As New SqlClient.SqlComman
Dim SQLTask As New SqlClient.SqlComman
Dim SQLActivity As New SqlClient.SqlComman
Dim SQLClient As New SqlClient.SqlComman
Dim SQLMatter As New SqlClient.SqlComman
Dim dreader As SqlClient.SqlDataReade
Dim dtTime As New DataTabl
Dim dtPhase As New DataTabl
Dim dtTask As New DataTabl
Dim dtActivity As New DataTabl
Dim dtCLient As New DataTabl
Dim dtMatter As New DataTabl

LitDatabase.ConnectionString = "user id=sa;data source=RC0101;persist security info=True;initial catalog=LitigationData;password=rollcall

SQLTime.CommandText = "SELECT Timekeeper, EmpName FROM V_LiTimekeeper UNION SELECT 'Littimedetail.timekeeper', '-ALL-' FROM V_LiTimekeeper ORDER BY EmpName
SQLPhase.CommandText = "SELECT Timekeeper, EmpName FROM V_LiTimekeeper UNION SELECT 'Littimedetail.timekeeper', '-ALL-' FROM V_LiTimekeeper ORDER BY EmpName
SQLTask.CommandText = "SELECT Timekeeper, EmpName FROM V_LiTimekeeper UNION SELECT 'Littimedetail.timekeeper', '-ALL-' FROM V_LiTimekeeper ORDER BY EmpName
SQLActivity.CommandText = "SELECT Timekeeper, EmpName FROM V_LiTimekeeper UNION SELECT 'Littimedetail.timekeeper', '-ALL-' FROM V_LiTimekeeper ORDER BY EmpName
SQLClient.CommandText = "SELECT Timekeeper, EmpName FROM V_LiTimekeeper UNION SELECT 'Littimedetail.timekeeper', '-ALL-' FROM V_LiTimekeeper ORDER BY EmpName
SQLMatter.CommandText = "SELECT Timekeeper, EmpName FROM V_LiTimekeeper UNION SELECT 'Littimedetail.timekeeper', '-ALL-' FROM V_LiTimekeeper ORDER BY EmpName

SQLTime.Connection = LitDatabas
SQLPhase.Connection = LitDatabas
SQLTask.Connection = LitDatabas
SQLActivity.Connection = LitDatabas
SQLClient.Connection = LitDatabas
SQLMatter.Connection = LitDatabas

SQadapterTime.SelectCommand = SQLTim
SQadapterPhase.SelectCommand = SQLPhas
SQadapterTask.SelectCommand = SQLTas
SQadapterActivity.SelectCommand = SQLActivit
SQadapterCLient.SelectCommand = SQLClien
SQadapterMatter.SelectCommand = SQLMatte

' Load Timekeeper Dropdown
SQadapterTime.Fill(dtTime

MainF.txtTEST.Text = "Not again

'MainF.DrpTimekeeper().DataSource = dtTim
'MainF.DrpTimekeeper().DisplayMember = dtTime.Columns(1).ToStrin
'MainF.DrpTimekeeper().ValueMember = dtTime.Columns(0).ToStrin

MainF.txtTEST.Text = "CRAP
'RdOffice.Checked = Tru
'drpEType.Visible = Fals
End Functio
 
From what I see the following must happen to fix your problem.

1. The line -- LoadAllFormData(ActiveForm) is assuming that ActiveForm is
not nothing. You should check this since I have no idea what the ActiveForm
variable is. It would be better to use the following:

Private Sub FrmMain_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim ActiveForm As FrmMain = ctype(sender,FrmMain)
LoadAllFormData(ActiveForm)
end sub

Lloyd

Eric said:
Thanks for your time Lloyd. Im am getting the following error when trying your method
Additional information: Object reference not set to an instance of an object.
"the error occured right at the first reference to the form object in the function"
MainF.txtTEST.Text = "not again"

I called the function from formLoad like this
Private Sub FrmMain_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim ActiveForm As FrmMain
LoadAllFormData(ActiveForm)
end sub

I then setup the function like this
Public Function LoadAllFormData(ByVal MainF As FrmMain)
Dim LitDatabase As New SqlClient.SqlConnection
Dim SQadapterTime As New SqlClient.SqlDataAdapter
Dim SQadapterPhase As New SqlClient.SqlDataAdapter
Dim SQadapterTask As New SqlClient.SqlDataAdapter
Dim SQadapterActivity As New SqlClient.SqlDataAdapter
Dim SQadapterCLient As New SqlClient.SqlDataAdapter
Dim SQadapterMatter As New SqlClient.SqlDataAdapter
Dim SQLTime As New SqlClient.SqlCommand
Dim SQLPhase As New SqlClient.SqlCommand
Dim SQLTask As New SqlClient.SqlCommand
Dim SQLActivity As New SqlClient.SqlCommand
Dim SQLClient As New SqlClient.SqlCommand
Dim SQLMatter As New SqlClient.SqlCommand
Dim dreader As SqlClient.SqlDataReader
Dim dtTime As New DataTable
Dim dtPhase As New DataTable
Dim dtTask As New DataTable
Dim dtActivity As New DataTable
Dim dtCLient As New DataTable
Dim dtMatter As New DataTable

LitDatabase.ConnectionString = "user id=sa;data
source=RC0101;persist security info=True;initial
catalog=LitigationData;password=rollcall"
SQLTime.CommandText = "SELECT Timekeeper, EmpName FROM
V_LiTimekeeper UNION SELECT 'Littimedetail.timekeeper', '-ALL-' FROM
V_LiTimekeeper ORDER BY EmpName"
SQLPhase.CommandText = "SELECT Timekeeper, EmpName FROM
V_LiTimekeeper UNION SELECT 'Littimedetail.timekeeper', '-ALL-' FROM
V_LiTimekeeper ORDER BY EmpName"
SQLTask.CommandText = "SELECT Timekeeper, EmpName FROM
V_LiTimekeeper UNION SELECT 'Littimedetail.timekeeper', '-ALL-' FROM
V_LiTimekeeper ORDER BY EmpName"
SQLActivity.CommandText = "SELECT Timekeeper, EmpName FROM
V_LiTimekeeper UNION SELECT 'Littimedetail.timekeeper', '-ALL-' FROM
V_LiTimekeeper ORDER BY EmpName"
SQLClient.CommandText = "SELECT Timekeeper, EmpName FROM
V_LiTimekeeper UNION SELECT 'Littimedetail.timekeeper', '-ALL-' FROM
V_LiTimekeeper ORDER BY EmpName"
SQLMatter.CommandText = "SELECT Timekeeper, EmpName FROM
V_LiTimekeeper UNION SELECT 'Littimedetail.timekeeper', '-ALL-' FROM
V_LiTimekeeper ORDER BY EmpName"
 
Lloyd, thank you very much for the help. It is hard to believe that th
ctype(sender,FrmMain) was all that I was missing. Have a great da
-Eric
 
Back
Top