Results 1 to 10 of 10

Thread: [RESOLVED] Insert records in Ms Access Database sql error

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2021
    Posts
    71

    Resolved [RESOLVED] Insert records in Ms Access Database sql error

    I am trying to add records to a table of my access database.
    When I create the SQL string I get an error. I am not sure what is the problem but I am guessing the path.

    Code:
    Private Sub AddRecords(sPath As String)
    
            Dim SQLstr As String
            Dim Command As OleDbCommand
            Dim icount As Integer
            Dim di As New DirectoryInfo(sPath)
            ' Get a reference to each file in that directory.
            Dim fiArr As FileInfo() = di.GetFiles()
            ' Display the names of the files.
            Dim fri As FileInfo
    
            Try
    
                    Dim sName As String
                    Dim sLastAcc As String
                    Dim sType As String
                    Dim Size As String
    
                For Each fri In fiArr
    
                    sType = Path.GetExtension(fri.Name).ToLower()
                    sLastAcc = fri.LastAccessTime
                    Size = FileLen(sPath & fri.Name)
                    sName = UCase(fri.Name)
    
                    SQLstr = "INSERT INTO Documents ([Document Location],[Document Type],[Last Accessed],[File Size],[Document Name],[OldName]) VALUES '" & sPath & "','" & sType & "','" & sLastAcc & "','" & Size & "','" & sName & "','" & sName & "'"
    
                    Command = New OleDbCommand(SQLstr, con)
                    icount = Command.ExecuteNonQuery
    
                Next fri
    
            Catch ex As Exception
                MessageBox.Show(ex.Message & " - " & ex.Source)
            End Try
    
        End Sub
    This is the SQLstr that creates the error:

    Code:
    INSERT INTO Documents ([Document Location],[Document Type],[Last Accessed],[File Size],[Document Name],[OldName]) VALUES 'C:\INFORMATIE OPSLAG\2021\','.pdf','17-4-2021 14:29:02','137992','MY.FILE.IN.DIRECTORY.20210414.PDF','MY.FILE.IN.DIRECTORY.20210414.PDF'

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

    Re: Insert records in Ms Access Database sql error

    Firstly, what error? Secondly, don't use string concatenation to build SQL code in the first place. Always use parameters. If you do that then it will likely fix your problem. Follow the Blog link in my signature and check out my thread on using parameters in ADO.NET to learn how.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Apr 2021
    Posts
    71

    Re: Insert records in Ms Access Database sql error

    I solved it. An error in the sql string.

    Code:
    SQLstr = "INSERT INTO Documents ([Document Location],[Document Type],[Last Accessed],[File Size],[Document Name],[OldName]) VALUES ('" & sPath & "','" & sType & "','" & sLastAcc & "','" & Size & "','" & sName & "','" & sName & "')"

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

    Re: [RESOLVED] Insert records in Ms Access Database sql error

    That is a bad solution. If you were doing it the right way in the first place, i.e. using parameters, then your code would be far more readable and you'd be less likely to make mistakes like that at all. Don't be lazy and keep doing it the wrong way because it would take effort to learn to do it the right way. Learn the right way now and do it the right way hereafter. It will save you time and effort in the long run.

  5. #5
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,048

    Re: Insert records in Ms Access Database sql error

    Quote Originally Posted by clausowitz View Post
    I solved it. An error in the sql string.

    Code:
    SQLstr = "INSERT INTO Documents ([Document Location],[Document Type],[Last Accessed],[File Size],[Document Name],[OldName]) VALUES ('" & sPath & "','" & sType & "','" & sLastAcc & "','" & Size & "','" & sName & "','" & sName & "')"
    you should take the advice from Post#2 = Always use parameters.

    it really is a small change, something like this
    Code:
      'Create the command.
                    Dim insertSQL As String = "INSERT INTO Documents "
                    insertSQL &= "([Document Location],[Document Type],[Last Accessed],[File Size],[Document Name],[OldName])"
                    insertSQL &= "VALUES (?,?,?,?,?,?)"
                    Dim cmd As New OleDb.OleDbCommand(insertSQL, Con)
                    cmd.Parameters.AddWithValue("Document Location", sPath)
                    cmd.Parameters.AddWithValue("Document Type", sType)
                    'add the others
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,347

    Re: Insert records in Ms Access Database sql error

    Quote Originally Posted by ChrisE View Post
    you should take the advice from Post#2 = Always use parameters.

    it really is a small change, something like this
    Code:
      'Create the command.
                    Dim insertSQL As String = "INSERT INTO Documents "
                    insertSQL &= "([Document Location],[Document Type],[Last Accessed],[File Size],[Document Name],[OldName])"
                    insertSQL &= "VALUES (?,?,?,?,?,?)"
                    Dim cmd As New OleDb.OleDbCommand(insertSQL, Con)
                    cmd.Parameters.AddWithValue("Document Location", sPath)
                    cmd.Parameters.AddWithValue("Document Type", sType)
                    'add the others
    No need for any concatenation to build the SQL. If you don't like a long(ish) SQL statement in one line then use a multiline String literal, which VB has supported for a while now (since 2017 or maybe 2015, I think). Before that, XML literals were the best option.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Apr 2021
    Posts
    71

    Re: [RESOLVED] Insert records in Ms Access Database sql error

    I changed my code an used the Parameters. Only question remains why the use of parameters is better?!

  8. #8
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,930

    Re: [RESOLVED] Insert records in Ms Access Database sql error

    Using Parameters helps avoid many problems, including the kind of thing you experienced here, data type issues, security issues, and more.

    For further explanation, see the article Why should I use Parameters instead of putting values into my SQL string? from our Database Development FAQs/Tutorials (at the top of the Database Development forum).

  9. #9
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,538

    Re: [RESOLVED] Insert records in Ms Access Database sql error

    Of course the most famous reason to use parameters is of course the story of Little Bobby Drop Tables;

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,347

    Re: [RESOLVED] Insert records in Ms Access Database sql error

    Quote Originally Posted by clausowitz View Post
    Only question remains why the use of parameters is better?!
    Why would that question remain when I specifically explain the answer in the blog post I directed you to multiple times? It's almost like you were too lazy to look at it all to begin with and then, when you did look, you were too lazy top read it properly. If you ask for help and people provide it, maybe use it.

Tags for this Thread

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