﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>A1VBCode Forums / Classic Visual Basic (VB 6 or earlier) / Database Programming  / Problem with Insert Statement / Latest Posts</title><generator>InstantForum.NET v4.1.1</generator><description>A1VBCode Forums</description><link>http://www.a1vbcode.com/vbforums/</link><webMaster>forums@a1vbcode.com</webMaster><lastBuildDate>Thu, 09 Sep 2010 02:05:05 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Problem with Insert Statement</title><link>http://www.a1vbcode.com/vbforums/Topic28646-4-1.aspx</link><description>&lt;FONT color=#111111&gt;So then why is the OP in this forum??? This is the VB6.0 Database Programming section.... Should not this thread be moved to where the OP could get more knowledgable help???&lt;/FONT&gt;</description><pubDate>Mon, 08 Feb 2010 14:25:21 GMT</pubDate><dc:creator>vb5prgrmr</dc:creator></item><item><title>RE: Problem with Insert Statement</title><link>http://www.a1vbcode.com/vbforums/Topic28646-4-1.aspx</link><description>sangay - you are posting a VB6 solution while the OP is working in .NET.</description><pubDate>Mon, 08 Feb 2010 07:51:37 GMT</pubDate><dc:creator>Mark</dc:creator></item><item><title>RE: Problem with Insert Statement</title><link>http://www.a1vbcode.com/vbforums/Topic28646-4-1.aspx</link><description>Public Shared Function AddCustomer(ByVal customer As Customer) As Boolean&lt;P&gt;&lt;BR&gt;        Dim connection As OleDbConnection = PaulMeadeInsuranceDB.GetConnection&lt;BR&gt;        Try&lt;BR&gt;            Dim insertStatement As String _&lt;BR&gt;            = "INSERT INTO Customers (CustNumber, Lastname, Firstname, MidName, Address1, Address2," _&lt;BR&gt;            &amp;amp; " City, State, ZIP, Phone1, Phone2, PolicyNumber) VALUES" _&lt;BR&gt;            &amp;amp; " (@CustNumber, @Lastname, @Firstname, @MidName, @Address1, @Address2, @City, @State," _&lt;BR&gt;            &amp;amp; " @ZIP, @Phone1, @Phone2, @PolicyNumber)"&lt;/P&gt;&lt;P&gt;            Dim insertcommand As New OleDbCommand(insertStatement, connection)&lt;BR&gt;            insertcommand.Parameters.AddWithValue("@CustNumber", customer.Custnumber)&lt;BR&gt;            insertcommand.Parameters.AddWithValue("@Lastname", customer.Lastname)&lt;BR&gt;            insertcommand.Parameters.AddWithValue("@Firstname", customer.FirstName)&lt;BR&gt;            insertcommand.Parameters.AddWithValue("@Midname", customer.MiddleName)&lt;BR&gt;            insertcommand.Parameters.AddWithValue("@Address1", customer.Address1)&lt;BR&gt;            insertcommand.Parameters.AddWithValue("@Address2", customer.Address2)&lt;BR&gt;            insertcommand.Parameters.AddWithValue("@City", customer.City)&lt;BR&gt;            insertcommand.Parameters.AddWithValue("@State", customer.State)&lt;BR&gt;            insertcommand.Parameters.AddWithValue("@ZIP", customer.Zip)&lt;BR&gt;            insertcommand.Parameters.AddWithValue("@Phone1", customer.Phone1)&lt;BR&gt;            insertcommand.Parameters.AddWithValue("@Phone2", customer.Phone2)&lt;BR&gt;            insertcommand.Parameters.AddWithValue("@PolicyNumber", customer.Policynumber)&lt;BR&gt;            Return AddCustomer&lt;BR&gt;        Catch ex As OleDbException&lt;BR&gt;            MessageBox.Show(ex.Message, ex.GetType.ToString)&lt;BR&gt;        Finally&lt;BR&gt;            connection.Close()&lt;BR&gt;        End Try&lt;BR&gt;Here is the code from the actual program:&lt;/P&gt;&lt;P&gt;Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click&lt;BR&gt;Try&lt;BR&gt;Dim record1C As New Customer(txtCustNumber.Text, txtLastname.Text, txtFirstName.Text, _&lt;BR&gt;txtMiddleName.Text, txtAddress1.Text, txtAddress2.Text, txtCity.Text, _&lt;BR&gt;txtState.Text, txtZIP.Text, txtPhone1.Text, txtPhone2.Text, txtPolicyNumber.Text)&lt;/P&gt;&lt;P&gt;&lt;BR&gt;CustomerDB.AddCustomer(record1C)&lt;/P&gt;&lt;P&gt;&lt;BR&gt;Catch ex As Exception&lt;BR&gt;Throw ex&lt;BR&gt;End Try&lt;BR&gt;End Sub &lt;BR&gt;&amp;lt;code&amp;gt; &lt;/P&gt;&lt;P&gt;With the above code I think you are not properly executing the code: A simple code to insert...&lt;/P&gt;&lt;P&gt;Private Sub cmddata_Click(Index As Integer)&lt;BR&gt;    On Error GoTo cmddata_Click_Err&lt;BR&gt;    &lt;BR&gt;        Dim strSQL            As String&lt;BR&gt;        Dim lngAffected     As Long&lt;BR&gt;        Dim mrs                As New ADODB.Recordset&lt;BR&gt;        &lt;BR&gt;        ' Control Array to INSERT, UPDATE AND DELETE&lt;BR&gt;        Select Case Index&lt;BR&gt;            '   INSERT&lt;BR&gt;            Case 0&lt;BR&gt;                If cmdData(0).Caption = "Insert" Then&lt;BR&gt;                        strSQL = "INSERT INTO Customers VALUES(" &amp;amp; _&lt;BR&gt;                        "'" &amp;amp; txtData(1).Text &amp;amp; "'," &amp;amp; _&lt;BR&gt;                        "'" &amp;amp; txtData(2).Text &amp;amp; "'," &amp;amp; _&lt;BR&gt;                        "'" &amp;amp; txtData(3).Text &amp;amp; "'," &amp;amp; _&lt;BR&gt;                        "'" &amp;amp; txtData(4).Text &amp;amp; "'," &amp;amp; _&lt;BR&gt;                        "'" &amp;amp; txtData(5).Text &amp;amp; "'," &amp;amp; _&lt;BR&gt;                        "'" &amp;amp; txtData(6).Text &amp;amp; "'," &amp;amp; _&lt;BR&gt;                        "'" &amp;amp; txtData(7).Text &amp;amp; "'," &amp;amp; _&lt;BR&gt;                        "'" &amp;amp; txtData(8).Text &amp;amp; "'," &amp;amp; _&lt;BR&gt;                        "'" &amp;amp; txtData(9).Text &amp;amp; "'," &amp;amp; _&lt;BR&gt;                        "'" &amp;amp; txtData(10).Text &amp;amp; "'," &amp;amp; _&lt;BR&gt;                        "'" &amp;amp; txtData(11).Text &amp;amp; "')"&lt;BR&gt;    &lt;BR&gt;                Else&lt;BR&gt;                    'UPDATE&lt;BR&gt;                    strSQL = "UPDATE tbldocdetails " &amp;amp; _&lt;BR&gt;                        "SET Lastname= '" &amp;amp; txtData(2).Text &amp;amp; "'" &amp;amp; _&lt;BR&gt;                        " ,Firstname= '" &amp;amp; txtData(3).Text &amp;amp; "'" &amp;amp; _&lt;BR&gt;                        " ,MiddleName= '" &amp;amp; txtData(4).Text &amp;amp; "'" &amp;amp; _&lt;BR&gt;                        " ,Address1= '" &amp;amp; txtData(5).Text &amp;amp; "'" &amp;amp; _&lt;BR&gt;                        " ,Address2= '" &amp;amp; txtData(6).Text &amp;amp; "'" &amp;amp; _&lt;BR&gt;                        " ,City= '" &amp;amp; txtData(7).Text &amp;amp; "'" &amp;amp; _&lt;BR&gt;                        " ,State= '" &amp;amp; txtData(8).Text &amp;amp; "'" &amp;amp; _&lt;BR&gt;                        " ,Address = '" &amp;amp; txtData(9).Text &amp;amp; "'" &amp;amp; _&lt;BR&gt;                        " ,Phone1= '" &amp;amp; txtData(10).Text &amp;amp; "'" &amp;amp; _&lt;BR&gt;                        " ,Phone2= '" &amp;amp; txtData(11).Text &amp;amp; "'" &amp;amp; _&lt;BR&gt;                        " ,Phone2= '" &amp;amp; txtData(12).Text &amp;amp; "'" &amp;amp; _&lt;BR&gt;                        " WHERE Custnumber= '" &amp;amp; txtData(1).Text &amp;amp; "'"&lt;/P&gt;&lt;P&gt;                End If&lt;BR&gt;    &lt;BR&gt;                gcnn1.Execute strSQL, lngAffected (gcnn1 is a global connection)&lt;BR&gt;                If lngAffected = 0 Then&lt;BR&gt;                    MsgBox "Another user has been updated the record.", vbCritical&lt;BR&gt;                End If&lt;BR&gt;                txtData(0).Locked = True&lt;BR&gt;                txtData(0).BackColor = Me.BackColor&lt;BR&gt;                cmdData(0).Caption = "Update"&lt;BR&gt;                cmdData(1).Enabled = False&lt;BR&gt;                cmdData(2).Enabled = False&lt;BR&gt;                &lt;BR&gt;                &lt;BR&gt;            '   Add&lt;BR&gt;            Case 1&lt;BR&gt;    &lt;BR&gt;                Call frmCustomer_Form_Clear&lt;BR&gt;    &lt;BR&gt;                txtData(2).SetFocus&lt;BR&gt;                txtData(1).Locked = False&lt;BR&gt;                txtData(1).BackColor = &amp;amp;H8000000C&lt;BR&gt;                cmdData(0).Caption = "Insert"&lt;BR&gt;                                                        &lt;BR&gt;                mrs.Open "Select max(DoctorID) from Customer", gcnn1&lt;BR&gt;                txtData(1) = Format(Val(mrs.Fields(0)) + 1, "000")&lt;BR&gt;                mrs.Close&lt;BR&gt;        &lt;BR&gt;            '   Delete&lt;BR&gt;            Case 2&lt;BR&gt;                If vbNo = MsgBox("Are you sure to delete this?", vbQuestion + vbYesNo) Then Exit Sub&lt;BR&gt;                 &lt;BR&gt;                strSQL = "DELETE * FROM Customer" &amp;amp; _&lt;BR&gt;                    " WHERE CustomerNo= '" &amp;amp; txtData(1).Text &amp;amp; "'"&lt;BR&gt;        &lt;BR&gt;                gcnn1.Execute strSQL&lt;BR&gt;                &lt;BR&gt;                Call frmCustomer_Form_Clear&lt;BR&gt;                &lt;BR&gt;        End Select&lt;BR&gt;        &lt;BR&gt;        Call frmCustomer_InitListView&lt;BR&gt;        &lt;BR&gt;        Exit Sub&lt;BR&gt;cmddata_Click_Err:&lt;BR&gt;    If Err = -2147217900 Then&lt;BR&gt;        MsgBox "Customer No (" &amp;amp; txtData(0).Text &amp;amp; ") already exists", vbCritical&lt;BR&gt;    Else&lt;BR&gt;        MsgBox Error, vbCritical&lt;BR&gt;    End If&lt;BR&gt;End Sub</description><pubDate>Mon, 08 Feb 2010 01:49:55 GMT</pubDate><dc:creator>sangay</dc:creator></item><item><title>RE: Problem with Insert Statement</title><link>http://www.a1vbcode.com/vbforums/Topic28646-4-1.aspx</link><description>As Mark pointed ou you never executed the command. After adding your parameters you should execute insertcommand.ExecuteNoQuery like this, &lt;br&gt;&lt;br&gt;insertcommand.Parameters.AddWithValue("@PolicyNumber", customer.Policynumber)&lt;br&gt;dim intRows as integer = insertcommand.ExecuteNonQuery() '(ExecuteNonQuery returns an int containing the number of rows affected)&lt;br&gt;IF intRows &lt;= 0 Then&lt;br&gt;Return False&lt;br&gt;Else&lt;br&gt;Return True&lt;br&gt;End If&lt;br&gt;Catch Ex As OledbException ... etc&lt;br&gt;&lt;br&gt;Hope this helps.</description><pubDate>Thu, 28 Jan 2010 06:06:12 GMT</pubDate><dc:creator>zimvbcoder</dc:creator></item><item><title>RE: Problem with Insert Statement</title><link>http://www.a1vbcode.com/vbforums/Topic28646-4-1.aspx</link><description>I don't see that you are actually executing the command.</description><pubDate>Tue, 05 Jan 2010 19:46:06 GMT</pubDate><dc:creator>Mark</dc:creator></item><item><title>Problem with Insert Statement</title><link>http://www.a1vbcode.com/vbforums/Topic28646-4-1.aspx</link><description>I am a beginning programmer and am having a problem with adding a record to a customer table in a Microsoft Access database. I have worked for hours reading my textbook and troubleshooting to no avail.  When I execute this code below the record is not added and I do not get an error of any type. When I go into debug mode, all of the parameters are correct and everything looks good. If I open up Access I can manually add the record but using my program nothing happens. All datatypes are string in VS and text in Access.&lt;P&gt;First an instance of the Customer Class is created and this seems to be successful. But when I take that customer object and run the AddCustomer method of the customer class it is not successful in that the database does not reflect that the record was ever added.&lt;/P&gt;&lt;P&gt;&lt;BR&gt;Here is the code for the AddCustomer method:&lt;/P&gt;&lt;P&gt;&amp;lt;code&amp;gt;&lt;BR&gt;Public Shared Function AddCustomer(ByVal customer As Customer) As Boolean&lt;/P&gt;&lt;P&gt;&lt;BR&gt;        Dim connection As OleDbConnection = PaulMeadeInsuranceDB.GetConnection&lt;BR&gt;        Try&lt;BR&gt;            Dim insertStatement As String _&lt;BR&gt;            = "INSERT INTO Customers (CustNumber, Lastname, Firstname, MidName, Address1, Address2," _&lt;BR&gt;            &amp;amp; " City, State, ZIP, Phone1, Phone2, PolicyNumber) VALUES" _&lt;BR&gt;            &amp;amp; " (@CustNumber, @Lastname, @Firstname, @MidName, @Address1, @Address2, @City, @State," _&lt;BR&gt;            &amp;amp; " @ZIP, @Phone1, @Phone2, @PolicyNumber)"&lt;/P&gt;&lt;P&gt;            Dim insertcommand As New OleDbCommand(insertStatement, connection)&lt;BR&gt;            insertcommand.Parameters.AddWithValue("@CustNumber", customer.Custnumber)&lt;BR&gt;            insertcommand.Parameters.AddWithValue("@Lastname", customer.Lastname)&lt;BR&gt;            insertcommand.Parameters.AddWithValue("@Firstname", customer.FirstName)&lt;BR&gt;            insertcommand.Parameters.AddWithValue("@Midname", customer.MiddleName)&lt;BR&gt;            insertcommand.Parameters.AddWithValue("@Address1", customer.Address1)&lt;BR&gt;            insertcommand.Parameters.AddWithValue("@Address2", customer.Address2)&lt;BR&gt;            insertcommand.Parameters.AddWithValue("@City", customer.City)&lt;BR&gt;            insertcommand.Parameters.AddWithValue("@State", customer.State)&lt;BR&gt;            insertcommand.Parameters.AddWithValue("@ZIP", customer.Zip)&lt;BR&gt;            insertcommand.Parameters.AddWithValue("@Phone1", customer.Phone1)&lt;BR&gt;            insertcommand.Parameters.AddWithValue("@Phone2", customer.Phone2)&lt;BR&gt;            insertcommand.Parameters.AddWithValue("@PolicyNumber", customer.Policynumber)&lt;BR&gt;            Return AddCustomer&lt;BR&gt;        Catch ex As OleDbException&lt;BR&gt;            MessageBox.Show(ex.Message, ex.GetType.ToString)&lt;BR&gt;        Finally&lt;BR&gt;            connection.Close()&lt;BR&gt;        End Try&lt;BR&gt;Here is the code from the actual program:&lt;/P&gt;&lt;P&gt;Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click&lt;BR&gt;Try&lt;BR&gt;Dim record1C As New Customer(txtCustNumber.Text, txtLastname.Text, txtFirstName.Text, _&lt;BR&gt;txtMiddleName.Text, txtAddress1.Text, txtAddress2.Text, txtCity.Text, _&lt;BR&gt;txtState.Text, txtZIP.Text, txtPhone1.Text, txtPhone2.Text, txtPolicyNumber.Text)&lt;/P&gt;&lt;P&gt;&lt;BR&gt;CustomerDB.AddCustomer(record1C)&lt;/P&gt;&lt;P&gt;&lt;BR&gt;Catch ex As Exception&lt;BR&gt;Throw ex&lt;BR&gt;End Try&lt;BR&gt;End Sub &lt;BR&gt;&amp;lt;code&amp;gt;</description><pubDate>Tue, 05 Jan 2010 17:34:23 GMT</pubDate><dc:creator>nick447923</dc:creator></item></channel></rss>