Please following below steps :
#Step1 : Create database and table
Please open Microsoft office Access to create Database and Table
Create Database : DBTM.mdb
Create Table : TBL_CUSTOMERS
Here for the design of TBL_CUSTOMERS
you can insert sample data to table : TBL_CUSTMERS
After that you can save to Folder,
#Step2 : Create Design Form in VB 6.0
Please open Visual Basic 6.0 in your computer, and then save in the same folder which you save database.
Create Form1 like below picture :
After that, please place below code in form1 :
Dim Conn As New ADODB.Connectionand the last, you can Run your VB 6.0 Project.
Dim RSCustomers As ADODB.Recordset
Sub OpenDB()
Set Conn = New ADODB.Connection
Set RSCustomers = New ADODB.Recordset
Conn.Open "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\DBTM.mdb"
End Sub
Private Sub Form_Activate()
Text1 = ""
Text2 = ""
Text3 = ""
Text4 = ""
Call OpenDB
Adodc1.ConnectionString = Conn
Adodc1.RecordSource = "TBL_CUSTOMERS"
Adodc1.Refresh
Set DataGrid1.DataSource = Adodc1
Call AutoNumberID
Text1.Enabled = False
Text2.SetFocus
End Sub
Private Sub AutoNumberID()
Call OpenDB
On Error Resume Next
RSCustomers.Open ("select * from TBL_CUSTOMERS Where CustomerID In(Select Max(CustomerID)From TBL_CUSTOMERS)Order By CustomerID Desc"), Conn
RSCustomers.Requery
Dim LongCaracter As String * 6
Dim CountID As Long
With RSCustomers
If .EOF Then
LongCaracter = "CUS" + "001"
Text1 = LongCaracter
Else
CountID = Right(RSCustomers!CustomerID, 3) + 1
LongCaracter = "CUS" + Right("000" & CountID, 3)
End If
Text1 = LongCaracter
End With
End Sub
Private Sub Command1_Click()
Call OpenDB
If Text1 = "" Or Text2 = "" Or Text3 = "" Or Text4 = "" Then
MsgBox "Please fill all field !"
Else
Dim SaveData As String
SaveData = "Insert Into TBL_CUSTOMERS values ('" & Text1 & "','" & Text2 & "','" & Text3 & "','" & Text4 & "')"
Conn.Execute SaveData
MsgBox "Add Data success", vbInformation, "Information"
Form_Activate
End If
End Sub
Private Sub Command2_Click()
End
End Sub
CLICK HERE to download project tutorial above
0 Response to "VB 6.0 - How to Create an Autonumber ID"