Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Insert Record into Access

Insert Record into Access

Scheduled Pinned Locked Moved Database
databaseperlhelptutorialquestion
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Brad Fackrell
    wrote on last edited by
    #1

    I’m trying to insert a new record into an MS Access (2002-2003) database using Perl. My instructor's example has me getting the last record ID and using that to generate the next ID. Two things: 1- When I do this the way the instructor illustrates (based on existing ID) using this code: if($db->Sql("SELECT MAX(ID) lastID FROM Test")) { print "SQL Error: " . $db->Error() . "\n"; $db->Close(); exit; } Can anybody tell me why I'm getting this error: SQL Error: [-3100] [1] [0] "[Microsoft][ODBC Microsoft Access Driver] Syntax error (missing operator) in query expression 'MAX(ID) lastID'." 2- If my ID field is set to “AutoNumber” shouldn’t I be able to simply insert and new record (with no ID)? I’ve attempted that with no success.

    L W 2 Replies Last reply
    0
    • B Brad Fackrell

      I’m trying to insert a new record into an MS Access (2002-2003) database using Perl. My instructor's example has me getting the last record ID and using that to generate the next ID. Two things: 1- When I do this the way the instructor illustrates (based on existing ID) using this code: if($db->Sql("SELECT MAX(ID) lastID FROM Test")) { print "SQL Error: " . $db->Error() . "\n"; $db->Close(); exit; } Can anybody tell me why I'm getting this error: SQL Error: [-3100] [1] [0] "[Microsoft][ODBC Microsoft Access Driver] Syntax error (missing operator) in query expression 'MAX(ID) lastID'." 2- If my ID field is set to “AutoNumber” shouldn’t I be able to simply insert and new record (with no ID)? I’ve attempted that with no success.

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      hi, 1. I think you need an as:

      SELECT MAX(ID) AS lastID FROM Test

      2. I should think so. And having two separate DB operations to first fetch then use MAX(ID) seems like a recipe for failure as soon as other users are also operating the DB. :)

      Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

      Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

      B W 2 Replies Last reply
      0
      • B Brad Fackrell

        I’m trying to insert a new record into an MS Access (2002-2003) database using Perl. My instructor's example has me getting the last record ID and using that to generate the next ID. Two things: 1- When I do this the way the instructor illustrates (based on existing ID) using this code: if($db->Sql("SELECT MAX(ID) lastID FROM Test")) { print "SQL Error: " . $db->Error() . "\n"; $db->Close(); exit; } Can anybody tell me why I'm getting this error: SQL Error: [-3100] [1] [0] "[Microsoft][ODBC Microsoft Access Driver] Syntax error (missing operator) in query expression 'MAX(ID) lastID'." 2- If my ID field is set to “AutoNumber” shouldn’t I be able to simply insert and new record (with no ID)? I’ve attempted that with no success.

        W Offline
        W Offline
        Wendelius
        wrote on last edited by
        #3

        Hi For the first question: You could try adding the keyword AS for the column:

        if($db->Sql("SELECT MAX(ID) AS lastID FROM Test"))

        And for the second question. Yes if the field is autonumbered, you don't have to mention it in an INSERT statement just like you don't have to mention any field that has a default value. However, if you're later going to add something for this row to a child table of this table you need to know the value for the foreign key in the child table.

        The need to optimize rises from a bad design.My articles[^]

        B 1 Reply Last reply
        0
        • L Luc Pattyn

          hi, 1. I think you need an as:

          SELECT MAX(ID) AS lastID FROM Test

          2. I should think so. And having two separate DB operations to first fetch then use MAX(ID) seems like a recipe for failure as soon as other users are also operating the DB. :)

          Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

          Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

          B Offline
          B Offline
          Brad Fackrell
          wrote on last edited by
          #4

          Thanks Luc. Aliasing did the trick.

          1 Reply Last reply
          0
          • L Luc Pattyn

            hi, 1. I think you need an as:

            SELECT MAX(ID) AS lastID FROM Test

            2. I should think so. And having two separate DB operations to first fetch then use MAX(ID) seems like a recipe for failure as soon as other users are also operating the DB. :)

            Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

            Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

            W Offline
            W Offline
            Wendelius
            wrote on last edited by
            #5

            Luc Pattyn wrote:

            having two separate DB operations to first fetch then use MAX(ID) seems like a recipe for failure

            Indeed it does. Using for example @@IDENTITY would be safer.

            The need to optimize rises from a bad design.My articles[^]

            1 Reply Last reply
            0
            • W Wendelius

              Hi For the first question: You could try adding the keyword AS for the column:

              if($db->Sql("SELECT MAX(ID) AS lastID FROM Test"))

              And for the second question. Yes if the field is autonumbered, you don't have to mention it in an INSERT statement just like you don't have to mention any field that has a default value. However, if you're later going to add something for this row to a child table of this table you need to know the value for the foreign key in the child table.

              The need to optimize rises from a bad design.My articles[^]

              B Offline
              B Offline
              Brad Fackrell
              wrote on last edited by
              #6

              Thanks Mika. As both you and Luc suggested, using AS worked by referencing the existing ID's. I'd much rather not do it that way but when I try to insert a record with no ID my table does not update. I'll try to figure that out latter since I can get by with Aliasing for now. ;)

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups