I am only new and trying to build a simple application in VB5. I can create a datatable with a dataform from the SQL database that part is fine. I can't seem to create a button to insert data from a textbox to the database though. this is one of the attempts i have made:
Private Sub ToolStripButton3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton3.Click
INSERT INTO users VALUES (textbox2.value)
End Sub
The problem is it says that Insert & Into are not declared. and that users (the name of the table) needs a valid expresion.
Can you please help me.
19-05-2007 at 12:26 AM
|
stickleprojects Level: Moderator Registered: 09-09-2002 Posts: 891
Re: Button to save data to SQL Server
Hi,
This doesn't appear to be VB5.. more like VS.Net 2005.
Suggest you read on data connectivity using ADo.NET
____________________________
Build it better, faster, quicker, easier.. then fix it (non-offical MS mission statement)
Inserting a new record, updating and deleting a record using the Execute command.
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
Deleting a Record using Execute command
You can use the SQL Delete statement to issue a command that deletes a record.
Option Explicit
Dim CN As Connection
Dim comobj As Command
Private Sub Form_Load()
Set CN = New Connection
Set comobj = New Command