|
-
May 13th, 2008, 11:18 PM
#1
Thread Starter
Lively Member
[2005] - Help With VB Module
Hi All,
I have created a module that will look up a user's FirstName / Surname from a SQL Database. The information returned will be displayed in a message box as a temporary measure.
I have called the module from the "MAINForm_Load", but nothing occurs. If I place the code from the module into the "MAINForm_Load", it works as expected.
-
May 13th, 2008, 11:50 PM
#2
Re: [2005] - Help With VB Module
That means there is something wrong in the way you are doing it. You just need to create a public function/sub routine in module and call it from the form u want. e.g.
vb.net Code:
Module Module1 Public Sub DoStuff() ' You code goes here End Sub End Module Public Class Form2 Private Sub Form2_Load( _ ByVal sender As System.Object, _ ByVal e As System.EventArgs _ ) Handles MyBase.Load DoStuff() End Sub End Class
-
May 14th, 2008, 03:43 AM
#3
Thread Starter
Lively Member
Re: [2005] - Help With VB Module
Thats exactly what I am doing. I know the code works because when I place it in the Form_Load, it updates a label showing the user who is currently logged in.
There is either something I am going wrong / incorrect configuration because I tried moving the code which handles the tree view select into a module and calling it from the Form_Load failed to work. I can post my code if needed.
-
May 14th, 2008, 03:50 AM
#4
Re: [2005] - Help With VB Module
-
May 14th, 2008, 05:23 AM
#5
Re: [2005] - Help With VB Module
If in your code you're updating a label's text and it works when place that code in the form but not in the module as you said, you must be referring to the label incorrectly.
For example, the code that works in form1
Code:
Private Sub DoStuff()
Me.Label1.Text = "Do Stuff"
End Sub
When you move the DoStuff sub to a module, Me now refers to the module and not form1, so in order to make that code works in a module, you have to modify it by passing a reference of the calling form to it.
This is the DoStuff sub put in a module
Code:
Private Sub DoStuff(ByVal callingForm As Form1)
callingForm.Label1.Text = "Do Stuff"
End Sub
Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
- Abraham Lincoln -
-
May 14th, 2008, 08:44 AM
#6
Thread Starter
Lively Member
Re: [2005] - Help With VB Module
I will post the code when I am at work tomorrow but from memory, this is how I am passing the reference.
Code:
Private Sub ExtractUserInfo()
'Connect to SQL and grab the record.....
Global.CRM.NET.CRMHOME.UserLabel.Text = dr(0).ToString()
End Sub
-
May 14th, 2008, 08:58 AM
#7
Re: [2005] - Help With VB Module
What you need is:
<From Instance>.Label.Text = dr(0).ToString()
My usual boring signature: Nothing
 
-
May 14th, 2008, 10:20 PM
#8
Thread Starter
Lively Member
Re: [2005] - Help With VB Module
Here is the code I am using in the module:
Code:
Imports System.Data.SqlClient
Imports System.Configuration
Module UserInfo
Public Sub ExtractUserInfo()
Try
Dim Username As String = Global.CRM.NET.CRMLoginForm.UsernameTextBox.Text
Dim myConnection As SqlConnection
Dim myCommand As SqlCommand
Dim dr As SqlDataReader
Dim connStr As String = ConfigurationManager.ConnectionStrings("CRM.NET.My.MySettings.CRMConnectionString").ConnectionString
Dim SQLSelect As String = "SELECT FirstName, LastName FROM tblUsers WHERE UserID = '" & Username & "'"
myConnection = New SqlConnection(connStr)
myConnection.Open()
'opening the connection
myCommand = New SqlCommand(SQLSelect, myConnection)
'executing the command and assigning it to connection
dr = myCommand.ExecuteReader()
While dr.Read()
'reading from the datareader
Global.CRM.NET.CRMNETHome.lblLoggedInUser.Text = dr(0).ToString()
End While
dr.Close()
myConnection.Close()
Catch ex As Exception
End Try
End Sub
End Module
On the "Main_Form", I had placed ExtractUserInfo()
-
May 14th, 2008, 11:27 PM
#9
Re: [2005] - Help With VB Module
Try this
vb.net Code:
' Form code Global.CRM.NET.CRMNETHome.lblLoggedInUser.Text = ExtractUserInfo(Global.CRM.NET.CRMLoginForm.UsernameTextBox.Text) ' Module code Imports System.Data.SqlClient Imports System.Configuration Module UserInfo Public Function ExtractUserInfo(ByVal userID As String) As String Dim myConnection As SqlConnection Dim myCommand As SqlCommand Dim dr As SqlDataReader Dim userName As String = "" Dim connStr As String = ConfigurationManager.ConnectionStrings("CRM.NET.My.MySettings.CRMConnectionString").ConnectionString Dim SQLSelect As String = "SELECT FirstName, LastName FROM tblUsers WHERE UserID = '" & userID & "'" Try connStr = ConfigurationManager.ConnectionStrings("CRM.NET.My.MySettings.CRMConnectionString").ConnectionString SQLSelect = "SELECT FirstName, LastName FROM tblUsers WHERE UserID = '" & userID & "'" myConnection = New SqlConnection(connStr) myConnection.Open() myCommand = New SqlCommand(SQLSelect, myConnection) dr = myCommand.ExecuteReader() While dr.Read() userName = dr(0).ToString() End While dr.Close() myConnection.Close() Return userName Catch ex As Exception Return userName End Try End Function End Module
-
May 15th, 2008, 12:11 AM
#10
Thread Starter
Lively Member
Re: [2005] - Help With VB Module
Cheers Deepak Sakpal for the code. It is working as expected and I can understand how it actually works.
On a side note, Visual Studio is reporting that the function could return a null value on all code paths. How do I handle that?
-
May 15th, 2008, 12:44 AM
#11
Re: [2005] - Help With VB Module
I am not getting that at my end. Did you copied the code as it is or not.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|