[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.
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
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.
Re: [2005] - Help With VB Module
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
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
Re: [2005] - Help With VB Module
What you need is:
<From Instance>.Label.Text = dr(0).ToString()
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()
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
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. :thumb:
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?
Re: [2005] - Help With VB Module
I am not getting that at my end. Did you copied the code as it is or not.