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.Connection
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
End Sub
Private Sub DataGrid1_DblClick()
On Error Resume Next
Call OpenDB
RSCustomers.Open "Select * from TBL_CUSTOMERS where CustomerID = '" & DataGrid1.Columns(0) & "'", Conn
If Not RSCustomers.EOF Then
Text1 = RSCustomers!CustomerID
Text2 = RSCustomers!CustomerName
Text3 = RSCustomers!CustomerAddress
Text4 = RSCustomers!CustomerPhone
Text1.Enabled = False
Command1.Enabled = True
Else
MsgBox "Data not found!"
End If
End Sub
Private Sub Command1_Click()
Call OpenDB
If Text1 = "" Or Text2 = "" Or Text3 = "" Or Text4 = "" Then
MsgBox "Data Belum Lengkap"
Else
Dim EditCustomer As String
EditCustomer = "update TBL_CUSTOMERS Set CustomerName= '" & Text2 & "',CustomerAddress='" & Text3 & "',CustomerPhone='" & Text4 & "' where CustomerID='" & Text1 & "'"
Conn.Execute EditCustomer
MsgBox "Update Data Successed!", vbInformation, "Information"
Form_Activate
End If
End Sub
Private Sub Command2_Click()
End
End Sub
If you are success following step above, please run your project..
CLICK HERE to download Project tutorials above...
0 Response to "VB 6.0 - How to Update Data to Database"