Avatar of Tim313
Tim313
Flag for United States of America asked on

Insert Into Statement

I have a form with multiple txt and ck boxes, some filled by query, others by user.

How do I refer to these boxes in the "values" of a Insert Into statement?
Microsoft Development.NET Programming

Avatar of undefined
Last Comment
Tim313

8/22/2022 - Mon
nmarun

You probably have a 'Save' or an 'Update' button. In the click event of the button, you can do the following:

// note the usage of the single quotes;
 string insertString                                                              = @"
                                                                  insert into Categories
                                                                  (Col1, Col2, Col3)
                                                                  values ('" + txtField1.Text + "', " + chkBox1.Checked + ", '" + txtField2.Text + "')";
                                                             
                                                              // 1. Instantiate a new command                                                              with a query and connection
                                                              SqlCommand cmd =                                                              new SqlCommand(insertString, conn);
                                                             
                                                              // 2. Call ExecuteNonQuery to send                                                              command
                                                              cmd.ExecuteNonQuery();

For a better understanding please read:
http://www.csharp-station.com/Tutorials/AdoDotNet/Lesson01.aspx
nmarun

looks ugly.. try this:

// note the usage of the single quotes;
string insertString = @"insert into tbl1 (Col1, Col2, Col3) values ('" + txtField1.Text + "', " + chkBox1.Checked + ", '" + txtField2.Text + "')";
 
// 1. Instantiate a new command with a query and connection
SqlCommand cmd = new SqlCommand(insertString, conn);
 
// 2. Call ExecuteNonQuery to send command
cmd.ExecuteNonQuery();

Open in new window

Tim313

ASKER
nmarun,

Thank you for your reply.

I neglected to include that I'm using Visual Basic 2005 Express and MS Access 2003.

Can you help me again with the same question, based on the above?
Your help has saved me hundreds of hours of internet surfing.
fblack61
nmarun

Go throught this link for connecting to an MS Access database:
http://www.geekpedia.com/tutorial60_ADO-.NET-and-Access-database-I.html
Tim313

ASKER
nmarun,

Thanks for the link, but I have my connection set-up. Just need to know if the ' " +  txtField.Text + " ' and the " + chkBox1.Checked  + " references are still applicable with VB2005 & MS Access 2003.
ASKER CERTIFIED SOLUTION
nmarun

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Tim313

ASKER
Thanks.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.