Friday, June 10, 2011

auto number

Imports System.Data
Imports System.Data.OleDb
Module Student

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

        cnSQL = New OleDbConnection(con)
        cnSQL.Open()
        Try

            cmd2.Connection = cnSQL
            cmd2.CommandText = "select top 1 " & fieldname & " from " & tablename & IIf(cond <> "", " where " & cond, "") & " order by 1 desc"
            dr = cmd2.ExecuteReader
            If dr.Read = True Then
                Getid = dr(0) + 1
            Else
                Getid = 1
            End If
            dr.Close()
        Catch ex As Exception
            MsgBox(Err.Description)
        End Try
        cnSQL.Close()
    End Function
    Function AutoID(ByVal Tablename As String, ByVal Fieldname As String) As Int32
        cnSQL = New OleDbConnection(con)
        cnSQL.Open()
        Dim cmd1 As New OleDbCommand
        Dim dr1 As OleDbDataReader
        cmd1.Connection = cnSQL
        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()
        cnSQL.Close()
    End Function


End Module







Private Sub Stud_autonum()
        Dim year As String
        Dim studid As String
        year = Now.ToString("yyyy")
        studid = "" & Format(AutoID("tblStudent", "StudentID"), "000000")
        lblstudID.Text = year & studid
    End Sub

No comments:

Post a Comment