Example : How to Create and Call Module function in a Form.

Module1.vb

Public Module Module1
    Public m As String = "Mohan"
    Public n As Integer

    Sub Main()
        MsgBox("Welcome U in your site Codershelpline")
    End Sub
    
    Sub calc()
        Dim x, y, z As Integer
        x = 9
        y = 8
        z = x + y
        MsgBox(z)
    End Sub
End Module
'------------------------------------------------------------------------------
Form1.vb

Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        MsgBox(m)

        n = 200
        MsgBox(n)

    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Main()    'Call of Module Function    
        calc()    'Call of Module Function    
    End Sub

End Class

NB : Module variables may available to all the form of same project. 

Example : How to transfer different values from one vb form to another without using vb                             module.

Public Class Form1
    Dim gen1 As String
    Dim co As String
    Dim qm As String
    Dim qi As String

    Public Uname As String = ""
    Public Dob As String = ""
    Public Gender As String = ""
    Public QMatric As String = ""
    Public QInter As String = ""
    Public Mobile As Integer = 0
    Public National As String = ""
    Public UId As String = ""
    Public Pass As String = ""
    Public Address As String = ""

    Private Sub BtnSave_Click(sender As Object, e As EventArgs) Handles BtnSave.Click
        
        If RdbGender1.Checked = True Then
            gen1 = "Male"
        ElseIf RdbGender2.Checked = True Then
            gen1 = "Female"
        End If
        
        If ChkMatric.Checked = True Then
            qm = "Matric"
        End If
        If ChkInter.Checked = True Then
            qi = "Inter"
        End If

        Uname = TxtName.Text
        Dob = DtDob.Text
        Gender = gen1
        QMatric = qm
        QInter = qi
        Mobile = NumMobile.Text
        National = CmbNational.Text
        UId = TxtUname.Text
        Pass = TxtPass.Text
        Address = RtbAddress.Text

        Form2.Show()
    End Sub
End Class
---------------------------------------
Public Class Form2

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        MsgBox(Form1.Uname)
        MsgBox(Form1.Dob)
        MsgBox(Form1.Gender)
        MsgBox(Form1.QMatric)
        MsgBox(Form1.QInter)
        MsgBox(Form1.Mobile)
        MsgBox(Form1.National)
        MsgBox(Form1.UId)
        MsgBox(Form1.Pass)
        MsgBox(Form1.Address)
    End Sub

End Class

Example : How to use connectivity code universally in another vb form via/using vb module.

Module1.vb

   Imports System
   Imports System.Data
   Imports System.Data.SqlClient
   Imports System.Data.SqlClient.SqlException
   Imports System.IO

Public Module Module1

    Public cn As New SqlConnection
    Public da As New SqlDataAdapter
    Public ds As New DataSet
    Public cm As New SqlCommand
    Public dt As New DataTable
    Public rdr As SqlDataReader
    Public bnd As BindingSource = New BindingSource()
    Public cs As String

    Sub Connect()

        cs = "Server= localhost; Database=databasename;Integrated Security=SSPI;"

        cn = New SqlConnection(cs)
        cm = New SqlCommand(cs, cn)
        da = New SqlDataAdapter("Select * from tablename", cs)
        ds = New DataSet
        da.Fill(ds, "tablename")
        MsgBox("Database Connected")
    End Sub

End Module
'------------------------------------------------------------
Form1.vb

Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Connect()       'Calling of Connectivity code
    End Sub
End Class

Example : How to transfer different values from one form to another form via vb module.

Module1.vb

Public Module connect1
    Public n1 As String
    Public dob As String
    Public g As String
    Public m1 As String
    Public i1 As String
    Public mob As Integer
    Public na As String
    Public ui As String
    Public upass As String
    Public ad As String
End Module
'-------------------------------------------
Form1.vb

Public Class DataTransfer

    Dim gen1 As String
    Dim co As String
    Dim qm As String
    Dim qi As String

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
       SendData()
    End Sub
    
    Sub SendData()
        If RdbGender1.Checked = True Then
            gen1 = "Male"
        ElseIf RdbGender2.Checked = True Then
            gen1 = "Female"
        End If

        If ChkMatric.Checked = True Then
            qm = "Matric"
        End If
        If ChkInter.Checked = True Then
            qi = "Inter"
        End If

        'Passing vb controls value to module variable.
        n1 = TxtName.Text
        dob = DtDob.Text
        g = gen1
        m1 = qm
        i1 = qi
        mob = NumMobile.Text
        na = CmbNational.Text
        ui = TxtUname.Text
        upass = TxtPass.Text
        ad = RtbAddress.Text
    End Sub
'------------------------------------------
Form2.vb

Public Class Form2

    Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'Receiving values from module and displaying it
        MsgBox(n1)
        MsgBox(dob)
        MsgBox(g)
        MsgBox(m1)
        MsgBox(i1)
        MsgBox(mob)
        MsgBox(na)
        MsgBox(ui)
        MsgBox(upass)
        MsgBox(ad)
    End Sub
End Class

Example : How to save/edit/delete/search data with database using vb module.

Module1.vb

   Imports System
   Imports System.Data
   Imports System.Data.SqlClient
   Imports System.Data.SqlClient.SqlException
   Imports System.IO

Public Module Module1

    Public cn As New SqlConnection
    Public da As New SqlDataAdapter
    Public ds As New DataSet
    Public cm As New SqlCommand
    Public dt As New DataTable

    Public rdr As SqlDataReader
    Public bnd As BindingSource = New BindingSource()
    Public cs As String

    Sub DataConnect()

        cs = "Server= localhost; Database=SMS;Integrated Security=SSPI;"

        cn = New SqlConnection(cs)
        cm = New SqlCommand(cs, cn)
        da = New SqlDataAdapter("Select * from DatabaseName", cn)
        ds = New DataSet
        'MsgBox("Database Connected")
    End Sub
End Module
'---------------------------------------
Form1.vb

Public Class Form1

    Dim gen1 As String
    Dim co As String
    Dim qm As String
    Dim qi As String
'---------------------------------------
    Sub Clear()
        TxtName.Text = ""
        DtDob.Text = Date.Now
        RdbGender1.Checked = False
        RdbGender2.Checked = False
        ChkMatric.Checked = False
        ChkInter.Checked = False
        NumMobile.Text = ""
        CmbNational.Text = "Select"
        TxtUname.Text = ""
        TxtPass.Text = ""
        RtbAddress.Text = ""
    End Sub
'---------------------------------------
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        DataConnect()

        Me.WindowState = FormWindowState.Maximized        
        Clear()
    End Sub
'---------------------------------------
    Private Sub Close_Click(sender As Object, e As EventArgs)
        Me.Dispose()
    End Sub
'---------------------------------------
    Private Sub BtnSave_Click(sender As Object, e As EventArgs)

        If RdbGender1.Checked = True Then
            gen1 = "Male"
        ElseIf RdbGender2.Checked = True Then
            gen1 = "Female"
        End If
        If ChkMatric.Checked = True Then
            qm = "Matric"
        End If
        If ChkInter.Checked = True Then
            qi = "Inter"
        End If

        Dim k As Integer = MsgBox("Do You Want To Save!", MsgBoxStyle.YesNo, "Save Execpiton")
        If k = 6 Then

            Try
                cn.Open()
                cm.CommandText = "Insert into DatabaseName(TxtName5, DtDob5, RdbGender5, ChkMatric5, 
                ChkInter5, TxtMobile5, CmbNational5, TxtUname5, TxtPass5, RtbAddress5)Values
                ('" + TxtName.Text + "','" + DtDob.Text + "','" + gen1.ToString + " ','" + 
                qm.ToString + " ','" + qi.ToString + " ','" + NumMobile.Text + " ','" + 
                CmbNational.Text + " ','" + TxtUname.Text + " ','" + TxtPass.Text + " ','" +
                RtbAddress.Text + " ')"
                cm.ExecuteScalar()
                cn.Close()
            Catch ex As Exception
                MsgBox(ex.ToString)
                Exit Sub
            Finally
                cn.Close()
            End Try
            MsgBox("RECORD SAVED SUCCESSFULLY")
            Clear()
        End If
    End Sub
'---------------------------------------
    Private Sub Clear_Click(sender As Object, e As EventArgs)       
        Clear()
    End Sub
'---------------------------------------
    Private Sub BtnSearch_Click(sender As Object, e As EventArgs)
        If TxtUname.Text = "" Then
            MsgBox("Please Insert User Id", MsgBoxStyle.Information)
            TxtUname.Focus()
        Else
            cn.Open()
            cm.CommandText = "select * from DatabaseName where TxtUname5='" + TxtUname.Text + "'"
            rdr = cm.ExecuteReader()
            While (rdr.Read())
                TxtName.Text = rdr("TxtName5").ToString
                DtDob.Text = rdr("DtDob5").ToString

                gen1 = rdr("RdbGender5").ToString

                If gen1.Trim().ToString = "Male" Then
                    RdbGender1.Checked = True
                ElseIf gen1.Trim().ToString = "Female" Then
                    RdbGender2.Checked = True
                End If

                qm = rdr("ChkMatric5").ToString
                'MsgBox(qm)
                If qm.Trim().ToString = "Matric" Then
                    ChkMatric.Checked = True
                End If
                qi = rdr("ChkInter5").ToString
                'MsgBox(qi)
                If qi.Trim().ToString = "Inter" Then
                    ChkInter.Checked = True
                End If
                NumMobile.Text = rdr("TxtMobile5").ToString

                co = rdr("CmbNational5").ToString
                Me.CmbNational.SelectedItem = co.Trim()               

                TxtUname.Text = rdr("TxtUname5")
                TxtPass.Text = rdr("TxtPass5").ToString
                RtbAddress.Text = rdr("RtbAddress5").ToString
            End While
            cn.Close()
        End If
    End Sub
'---------------------------------------   
    Private Sub BtnUpdate_Click(sender As Object, e As EventArgs)

        If RdbGender1.Checked = True Then
            gen1 = "Male"
        ElseIf RdbGender2.Checked = True Then
            gen1 = "Female"
        End If
        If ChkMatric.Checked = True Then
            qm = "Matric"
        Else
            qm = "NULL"
        End If
        If ChkInter.Checked = True Then
            qi = "Inter"
        Else
            qi = "NULL"
        End If

        Dim k As Integer = MsgBox("Do You Want to Update Details", MsgBoxStyle.YesNo, "Update Alert")
        If k = 6 Then
            Try
                cn.Open()
                cm.CommandText = "update DatabaseName set TxtName5 ='" + TxtName.Text + "', DtDob5='" 
                + DtDob.Text + "', RdbGender5='" + gen1.ToString + " ',ChkMatric5='" + qm.ToString 
                + " ',ChkInter5='" + qi.ToString + " ',TxtMobile5='" + NumMobile.Text + " '
                , CmbNational5='" + CmbNational.Text + " ',TxtPass5='" + TxtPass.Text + " '
                ,RtbAddress5='" + RtbAddress.Text + " ' where TxtUname5='" + TxtUname.Text.Trim 
                + "' "
                cm.ExecuteNonQuery()
                'Dim i As Integer = cm.ExecuteNonQuery
                'cn.Close()
                'MsgBox(i.ToString + " RECORD UPDATE SUCCESSFULLY")
            Catch ex As Exception
                MsgBox(ex.Message)
                Exit Sub
            Finally
                MsgBox("Update Succesfully")
                cn.Close()
            End Try
        End If
    End Sub
'---------------------------------------
    Private Sub BtnDelete_Click(sender As Object, e As EventArgs)
        Dim k As Integer = MsgBox("Do You Want to Detele the Record", MsgBoxStyle.YesNo, "Delete 
            Confirmation")
        If k = 6 Then
            Try
                cn.Open()
                cm.CommandText = "DELETE FROM DatabaseName WHERE TxtUname5='" + TxtUname.Text + "'"
                cm.ExecuteNonQuery()
            Catch ex As Exception
                MsgBox(ex.Message)
            Finally
                MsgBox("Record is Deleted..")
            End Try
        End If
    End Sub   
End Class

Loading

Categories: VB .Net Codes

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.