Inserting a new record and updating a record using the Execute command in Visual Basic.

We can use the SQL Insert statement to issue a command that will add a new record in a data source.

Option Explicit
Dim CN As Connection
Dim comobj As Command

Private Sub Form_Load()
Set CN = New Connection
Set comobj = New Command

With CN
.ConnectionString = “Integrated Security=SSPI;Initial Catalog=FinAccounting;Data Source=SYS1”
.Provider = “SQLOLEDB”
.Open
End With
With comobj
.ActiveConnection = CN
.CommandText = “INSERT INTO AccountsTable(AccountCode,AccountName,AccountCat) values(‘C001’,’Creditor1',’1')”
.Execute
End With
End Sub

Updating Records

You can use the SQL Update statement to issue a command that changes a record or group of records.

Option Explicit
Dim CN As Connection
Dim comobj As Command

Private Sub Form_Load()
Set CN = New Connection
Set comobj = New Command

With CN
.ConnectionString = “Integrated Security=SSPI;Initial Catalog=FinAccounting;Data Source=SYS1”
.Provider = “SQLOLEDB”
.Open
End With

With comobj
.ActiveConnection = CN
.CommandText = “UPDATE AccountsTable SET AccountName =’Main Creditor1' WHERE AccountCode=’C001'”
.Execute
End With

End Sub

use the adodc component in vb,it is very easy to learn.go to your components and click adodc control and datagrid control to display your data.
hope this helps....

This article has been dead for over six months. Start a new discussion instead.