Results 1 to 11 of 11

Thread: [2005] - Help With VB Module

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    111

    Question [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.

  2. #2
    PowerPoster Deepak Sakpal's Avatar
    Join Date
    Mar 2002
    Location
    Mumbai, India
    Posts
    2,424

    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:
    1. Module Module1
    2.  
    3.     Public Sub DoStuff()
    4.         ' You code goes here
    5.     End Sub
    6.  
    7. End Module
    8.  
    9. Public Class Form2
    10.  
    11.     Private Sub Form2_Load( _
    12.         ByVal sender As System.Object, _
    13.         ByVal e As System.EventArgs _
    14.     ) Handles MyBase.Load
    15.  
    16.         DoStuff()
    17.     End Sub
    18.    
    19. End Class

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    111

    Question 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.

  4. #4

  5. #5
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    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 -

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    111

    Question 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

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    Re: [2005] - Help With VB Module

    What you need is:

    <From Instance>.Label.Text = dr(0).ToString()
    My usual boring signature: Nothing

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    111

    Question 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()

  9. #9
    PowerPoster Deepak Sakpal's Avatar
    Join Date
    Mar 2002
    Location
    Mumbai, India
    Posts
    2,424

    Re: [2005] - Help With VB Module

    Try this
    vb.net Code:
    1. ' Form code
    2.  
    3. Global.CRM.NET.CRMNETHome.lblLoggedInUser.Text = ExtractUserInfo(Global.CRM.NET.CRMLoginForm.UsernameTextBox.Text)
    4.  
    5.  
    6. ' Module code
    7.  
    8. Imports System.Data.SqlClient
    9. Imports System.Configuration
    10.  
    11. Module UserInfo
    12.     Public Function ExtractUserInfo(ByVal userID As String) As String
    13.         Dim myConnection As SqlConnection
    14.         Dim myCommand As SqlCommand
    15.         Dim dr As SqlDataReader
    16.         Dim userName As String = ""
    17.  
    18.         Dim connStr As String = ConfigurationManager.ConnectionStrings("CRM.NET.My.MySettings.CRMConnectionString").ConnectionString
    19.         Dim SQLSelect As String = "SELECT FirstName, LastName FROM tblUsers WHERE UserID = '" & userID & "'"
    20.  
    21.         Try
    22.             connStr = ConfigurationManager.ConnectionStrings("CRM.NET.My.MySettings.CRMConnectionString").ConnectionString
    23.             SQLSelect = "SELECT FirstName, LastName FROM tblUsers WHERE UserID = '" & userID & "'"
    24.  
    25.             myConnection = New SqlConnection(connStr)
    26.  
    27.             myConnection.Open()
    28.             myCommand = New SqlCommand(SQLSelect, myConnection)
    29.             dr = myCommand.ExecuteReader()
    30.             While dr.Read()
    31.                 userName = dr(0).ToString()
    32.             End While
    33.             dr.Close()
    34.             myConnection.Close()
    35.  
    36.             Return userName
    37.         Catch ex As Exception
    38.             Return userName
    39.         End Try
    40.     End Function
    41. End Module

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    111

    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?

  11. #11

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width