Results 1 to 6 of 6

Thread: [RESOLVED] [2005] Syntax error in INSERT INTO statement???

  1. #1

    Thread Starter
    Addicted Member phenom's Avatar
    Join Date
    Apr 2006
    Location
    UAE
    Posts
    233

    Resolved [RESOLVED] [2005] Syntax error in INSERT INTO statement???

    Dear all,
    Could anyone please tell me what’s wrong with this code?!?! I’m sure that there is no error, but it keeps showing this error message (Syntax error in INSERT INTO statement) as shown in the attached picture.

    I’m developing software for my organization, part of this software allow the user to generate a PC name, everything is working fine except when it comes to save the date. I’ve created new Database, rewrite the code, and still same problem.

    Just to mention that all the other part of this software is 100% working, like generating a serial number, add new PC etc… all are working and saving data into the data base correctly. I’m facing only this problem in this part only!!!! I don’t know why…

    Appreciate your help…

    Regards,

    Code:
    Private Sub Save_info()
    
            m_Connection.Close()
            m_NewName.Reset()
            m_NewName.Dispose()
    
            Try
                m_Connection.ConnectionString = _
                "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Application.StartupPath & "\DB\MainDB.accdb;Jet OLEDB:Database Password=MyDbPassword;"
    
                m_Connection.Open()
                m_DataAdapter = New _
                OleDb.OleDbDataAdapter("Select * From Name_PC", m_Connection)
                m_cmb = New OleDb.OleDbCommandBuilder(m_DataAdapter)
                m_DataAdapter.Fill(m_NewName)
    
            Catch objException As Exception
                Dim what As MsgBoxResult
                what = MessageBox.Show("An Error Occurred: " & objException.Message, _
                "Message Type: Error In Connection 'Step 1' in Save_Info (fclsPCName)", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
    
            'Add New PCNAME
            Try
    
                Dim AllDept As String = Me.cmbDeptName.SelectedItem.ToString
                Dim fullDeptCode As String = AllDept
                fullDeptCode = fullDeptCode.Substring(fullDeptCode.IndexOf(" "))
    
                Dim DeptName As String = ""
                DeptName = Microsoft.VisualBasic.Left(AllDept, AllDept.Length - fullDeptCode.Length)
                'MessageBox.Show(DeptName & DeptName.Length)
    
                Dim DeptCode As String = AllDept
                DeptCode = DeptCode.Substring(DeptCode.LastIndexOf(" ") + 1)
    
                Dim Lc As String = ""
                If Me.rbHQ.Checked = True Then
                    Lc = "HQ"
                ElseIf Me.rbSite.Checked = True Then
                    Lc = "SO"
                End If
    
                Dim Final_PC_Name As String = Lc & DeptCode & Me.txtNumber.Text & "-" & Me.txtUserName.Text.ToUpper
    
                Dim NewInfo As DataRow
                NewInfo = m_NewName.NewRow
    
                NewInfo("Place") = Lc
                NewInfo("Dept") = DeptName
                NewInfo("NB") = txtNumber.Text
                NewInfo("User_Name") = txtUserName.Text
                NewInfo("PC_Name") = Final_PC_Name
                NewInfo("DoneBy") = fclsMain.lblUserName.Text
                NewInfo("On") = Now
    
                
                m_NewName.Rows.Add(NewInfo)
    
                Try
                    m_DataAdapter.Update(m_NewName)
                Catch ex As Exception
                    MessageBox.Show(ex.Message)
                End Try
    
    
                m_Connection.Close()
                m_NewName.Reset()
                m_NewName.Dispose()
    
                Me.txtID.Text = Final_PC_Name
    
            Catch objException As Exception
                Dim what As MsgBoxResult
                what = MessageBox.Show("An Error Occurred: " & objException.Message, _
                "Message Type: Error In Update Save_Info (fclsPCName)", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
    
            m_Connection.Close()
            m_NewName.Reset()
            m_NewName.Dispose()
        End Sub
    Attached Images Attached Images  
    =======================================
    If I helped you, Kindly Rate my post. Thanks
    -----------
    PHENOM

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Syntax error in INSERT INTO statement???

    This same question has been asked and answered many times. This is one of the weaknesses of CommandBuilders. They take their cue from the query as to what columns to write into the other SQL statements. When you use a wild card in the query the column names are just used "as is". This is a problem if one of your column names is an SQL reserved word. In your case the culprit is "On". That will be interpreted as an SQL reserved word unless you enclose it in brackets. To get a CommandBuilder to do that you must do it in the query too. That means that you must write out the full column list rather than use a wild card. The alternative is to not use a CommandBuilder and create the other SQL statements yourself.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Addicted Member phenom's Avatar
    Join Date
    Apr 2006
    Location
    UAE
    Posts
    233

    Re: [2005] Syntax error in INSERT INTO statement???

    Quote Originally Posted by jmcilhinney
    This same question has been asked and answered many times. This is one of the weaknesses of CommandBuilders. They take their cue from the query as to what columns to write into the other SQL statements. When you use a wild card in the query the column names are just used "as is". This is a problem if one of your column names is an SQL reserved word. In your case the culprit is "On". That will be interpreted as an SQL reserved word unless you enclose it in brackets. To get a CommandBuilder to do that you must do it in the query too. That means that you must write out the full column list rather than use a wild card. The alternative is to not use a CommandBuilder and create the other SQL statements yourself.
    Thanks jmcilhinney,

    I was doing a search and I found your answer there thanks again what can I say you are genius

    Have a nice day…
    =======================================
    If I helped you, Kindly Rate my post. Thanks
    -----------
    PHENOM

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [RESOLVED] [2005] Syntax error in INSERT INTO statement???

    Don't mistake experience for intelligence.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Addicted Member phenom's Avatar
    Join Date
    Apr 2006
    Location
    UAE
    Posts
    233

    Re: [RESOLVED] [2005] Syntax error in INSERT INTO statement???

    Quote Originally Posted by jmcilhinney
    Don't mistake experience for intelligence.
    Sure you are Genius & Expert (MVP very Pro) man too
    =======================================
    If I helped you, Kindly Rate my post. Thanks
    -----------
    PHENOM

  6. #6
    Fanatic Member
    Join Date
    Aug 2006
    Location
    In my head
    Posts
    913

    Re: [RESOLVED] [2005] Syntax error in INSERT INTO statement???

    I apologize ahead of time, this is off topic and I do not want to hijack this thread. Phenom, what are you using to get the forms to look like that? Do you currently have Vista or XP? I would really like to get my forms and such to have that kind of border, button look, etc.

    Thanks,

    D
    Platforms of choice: Visual Studio 2005/2008 Professional : Visual Studio 2010 Enterprise : PHP - Notepad++/WAMP

    Please Rate If I helped you.
    Please remember to mark threads as closed if your issue has been resolved.

    Reserved Words in Access | Connection Strings

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width