MaxLength niscaya kita jumpai dalam pembuatan suatu aplikasi dengan bahasa pemrograman apapun, untuk kali ini admin akan share MaxLength secara otomatis menyesuaikan dengan MaxLength dengan tabel dari database yang kita buat. Dalam hal ini database yang dipakai memakai Ms. Access 2000 dan Visual Basic 6.0 (vb6).

Buatlah database dari Ms. Access simpan dengan format Ms. Access 2000 dengan nama database sampleDb lalu buat juga tabel dengan nama SISWA untuk field-fieldnya menyerupai dibawah ini
Field
Property
MaxLength
NIS
Text
20
NAMA
Text
30
ALAMAT
Text
50
TELEPON
Text
20

Sekarang kita akan menciptakan beberapa komponen TextBox pada sebuah form Microsoft Visual Basic 6.0 (VB6) dengan Max Length mengikuti Max Length dari field tabel yang terdapat pada database Ms. Access 2000 yang telah kita buat diawal pembahasan ini. Langsung saja pada pokok pembahasan buka Ms. Visual Basic 6.0 (VB6) – StandartEXE dan buat Form lalu tambahkan beberapa komponen adalah TextBox dan Label menyerupai dibawah ini : 

Pengesetan Properties Maxlength Secara Otomatis

Jangan lupa buat ClassModule dengan nama : clsAttribut lalu pastekan syntac dibawah ini

Private mFormName   As Form
Private mCol        As Collection
Private mTableName  As String

Public Property Let tableName(ByVal vData As String)
    mTableName = vData
End Property
Public Property Get tableName() As String
    tableName = mTableName
End Property

Public Property Let formName(ByVal vData As Form)
    Set mFormName = vData
End Property
Public Property Get formName() As Form
    Set formName = mFormName
End Property

Private Property Get getColumns(ByVal indexKey As Long) As clsAttribut
    Set getColumns = mCol(indexKey)
End Property

Private Function isFieldExists(ByVal fieldName As String, ByRef textBoxName As String) As Boolean
    Dim objAttributs    As clsAttribut
    Dim i               As Integer
   
    Set objAttributs = New clsAttribut
    For i = 1 To mCol.Count
        Set objAttributs = getColumns(i)
        If LCase(objAttributs.fieldName) = LCase(fieldName) Then
            textBoxName = objAttributs.objTextBox.Name
            isFieldExists = True
           
            Exit For
        End If
    Next i
    Set objAttributs = Nothing
End Function

Private Function isTableExists() As Boolean
    Dim rs      As ADODB.Recordset
    Dim strSql  As String
   
    On Error GoTo errHandle
   
    strSql = “SELECT * FROM ” & tableName & “”
    Set rs = New ADODB.Recordset
    rs.Open strSql, conn, adOpenForwardOnly, adLockReadOnly
    rs.Close
    Set rs = Nothing
   
    isTableExists = True
    Exit Function
errHandle:
    isTableExists = False
End Function

Public Sub addAttributs(ByVal fieldName As String, ByVal objTextBox As TextBox)
    Dim objNewMember As clsAttribut
   
    Set objNewMember = New clsAttribut
    objNewMember.fieldName = fieldName
    objNewMember.objTextBox = objTextBox
   
    mCol.Add objNewMember
    Set objNewMember = Nothing
End Sub

Public Sub autoMaxLength()
    Dim rs              As ADODB.Recordset
    Dim ctl             As Object
   
    Dim strSql          As String
    Dim textBoxName     As String
   
    Dim i               As Integer
   
    If isTableExists Then
        strSql = “SELECT * FROM ” & tableName & “”
        Set rs = New ADODB.Recordset
        rs.Open strSql, conn, adOpenForwardOnly, adLockReadOnly
        For i = 0 To rs.Fields.Count – 1
            If isFieldExists(rs.Fields(i).Name, textBoxName) Then
                For Each ctl In formName.Controls
                    If TypeName(ctl) = “TextBox” And ctl.Name = textBoxName Then
                        ctl.MaxLength = rs.Fields(i).DefinedSize ‘set MaxLength
                        Exit For
                    End If
                Next
            End If
        Next i
        rs.Close
        Set rs = Nothing
       
    Else
        MsgBox “Nama tabel salah”, vbExclamation, “Peringatan”
    End If
End Sub

Private Sub Class_Initialize()
    Set mCol = New Collection
End Sub

Private Sub Class_Terminate()
    Set mCol = Nothing
End Sub

Simpan dan dilanjutkan buat kembali ClassModule dengan nama : clsAutoMaxLength pastekan lagi syntac dibawah ini : 

Private mFormName   As Form
Private mCol        As Collection
Private mTableName  As String

Public Property Let tableName(ByVal vData As String)
    mTableName = vData
End Property
Public Property Get tableName() As String
    tableName = mTableName
End Property

Public Property Let formName(ByVal vData As Form)
    Set mFormName = vData
End Property
Public Property Get formName() As Form
    Set formName = mFormName
End Property

Private Property Get getColumns(ByVal indexKey As Long) As clsAttribut
    Set getColumns = mCol(indexKey)
End Property

Private Function isFieldExists(ByVal fieldName As String, ByRef textBoxName As String) As Boolean
    Dim objAttributs    As clsAttribut
    Dim i               As Integer
   
    Set objAttributs = New clsAttribut
    For i = 1 To mCol.Count
        Set objAttributs = getColumns(i)
        If LCase(objAttributs.fieldName) = LCase(fieldName) Then
            textBoxName = objAttributs.objTextBox.Name
            isFieldExists = True
           
            Exit For
        End If
    Next i
    Set objAttributs = Nothing
End Function

Private Function isTableExists() As Boolean
    Dim rs      As ADODB.Recordset
    Dim strSql  As String
   
    On Error GoTo errHandle
   
    strSql = “SELECT * FROM ” & tableName & “”
    Set rs = New ADODB.Recordset
    rs.Open strSql, conn, adOpenForwardOnly, adLockReadOnly
    rs.Close
    Set rs = Nothing
   
    isTableExists = True
    Exit Function
errHandle:
    isTableExists = False
End Function

Public Sub addAttributs(ByVal fieldName As String, ByVal objTextBox As TextBox)
    Dim objNewMember As clsAttribut
   
    Set objNewMember = New clsAttribut
    objNewMember.fieldName = fieldName
    objNewMember.objTextBox = objTextBox
   
    mCol.Add objNewMember
    Set objNewMember = Nothing
End Sub

Public Sub autoMaxLength()
    Dim rs              As ADODB.Recordset
    Dim ctl             As Object
   
    Dim strSql          As String
    Dim textBoxName     As String
   
    Dim i               As Integer
   
    If isTableExists Then
        strSql = “SELECT * FROM ” & tableName & “”
        Set rs = New ADODB.Recordset
        rs.Open strSql, conn, adOpenForwardOnly, adLockReadOnly
        For i = 0 To rs.Fields.Count – 1
            If isFieldExists(rs.Fields(i).Name, textBoxName) Then
                For Each ctl In formName.Controls
                    If TypeName(ctl) = “TextBox” And ctl.Name = textBoxName Then
                        ctl.MaxLength = rs.Fields(i).DefinedSize ‘set MaxLength
                        Exit For
                    End If
                Next
            End If
        Next i
        rs.Close
        Set rs = Nothing
    Else
        MsgBox “Nama tabel salah”, vbExclamation, “Peringatan”
    End If
End Sub

Private Sub Class_Initialize()
    Set mCol = New Collection
End Sub

Private Sub Class_Terminate()
    Set mCol = Nothing
End Sub

Simpan lalu double klik pada form pilih mekanisme Form_Load dan pastekan lagi syntaknya : 

    Dim obj As clsAutoMaxLength
    Set obj = New clsAutoMaxLength
    With obj
        .formName = Me
        .tableName = “siswa”
       
        .addAttributs “nis”, txtNIS
        .addAttributs “nama”, txtNama
        .addAttributs “alamat”, txtAlamat
        .addAttributs “telepon”, txtTelepon
       
        Call .autoMaxLength
    End With
    Set obj = Nothing

Sangat gampang bukan, jadi kita tidak perlu merasa khawatir dan repot-repot men set MaxLength pada TexBox dan semuanya telah otomatis menyesuaikan MaxLength Field tabel dari database yang kita buat. Simpan lalu Run/F5. Coba ketik huruf pada textBox maka MaxLength dari textBox akan menyesuaikan dengan MaxLength yang ada pada tabel didalam database. Untuk sourcodenya sanggup didownload di LINK INI