Wednesday, April 13, 2011

module1(vb.net 2008) w/ connections string and auto number

'Call the pakages
Imports System.Data
Imports System.Data.OleDb

Module modVariables
    'Declare all variables
    Public olecon As OleDbConnection
    Public cmd As OleDbCommand
    Public cmd1 As OleDbCommand
    Public adp As OleDbDataAdapter
    Public dr As OleDbDataReader
    Public ds As New Data.DataSet
    Public i As Integer
    Public ssql As String
    Public con As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Elaine K Pawnshop\databaseko.mdb;Persist Security Info=False"
    Public dt As New DataTable
    Public days As Integer
    Public years As Integer
    Public months As Integer
    Public mint As Integer
    Dim dr3 As OleDbDataReader
    Public Sub connection_open()
        'Setting the database connection
        'You could also put try catch to control your errors
        olecon = New OleDbConnection(con)
        olecon.Open()

    End Sub

    Public Sub connection_close()
        'Closing the connection
        olecon.Close()
        olecon.Dispose()

    End Sub

    Public Function Getid(ByVal tablename As String, ByVal fieldname As String, Optional ByVal cond As String = "") As Double
        Dim cmd2 As New OleDbCommand

        connection_open()
        Try

            cmd2.Connection = olecon
            cmd2.CommandText = "select top 1 " & fieldname & " from " & tablename & IIf(cond <> "", " where " & cond, "") & " order by 1 desc"
            dr = cmd2.ExecuteReader
            If dr3.Read = True Then
                Getid = dr3(0) + 1
            Else
                Getid = 1
            End If
            dr3.Close()
        Catch ex As Exception
            MsgBox(Err.Description)
        End Try
        connection_close()
    End Function
    Function AutoID(ByVal Tablename As String, ByVal Fieldname As String) As Int32
        connection_open()
        Dim cmd1 As New OleDbCommand
        Dim dr1 As OleDbDataReader
        cmd1.Connection = olecon
        cmd1.CommandType = CommandType.Text
        cmd1.CommandText = "select top 1 " & Fieldname & " from " & Tablename & " Order by 1 Desc "
        dr1 = cmd1.ExecuteReader
        If dr1.Read = True Then
            AutoID = Microsoft.VisualBasic.Right(dr1(0), 4) + 1
        Else
            AutoID = 1
        End If
        dr1.Close()
        connection_close()
    End Function
   
End Module

No comments:

Post a Comment