Please following below :
#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
To practise insert database, you can insert sample data to table : TBL_CUSTMERS
After that you can save to Folder,
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 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
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 Insert Data to Database"