Results 1 to 5 of 5
  1. #1
    stacynh is offline Novice
    Windows 8 Access 2013 64bit
    Join Date
    Jul 2016
    Location
    Michigan
    Posts
    8

    Create new table record based on Form value

    Hello,

    I have a Form that collects information for table tblRC. Table tblRC has a primary key RC_ID.

    Sometimes, a record in tblRC should have a corresponding record in tblBC. In this case, the data enterer should click a button, which would create a new record in tblBC with primary key ID_Match (number field) equal to RC_ID (auto number field).

    I have tried many iterations, but I am having trouble with syntax errors in the following statement. I have tried docmd.runSQL, brackets, single quotes, double quotes, etc, but can't seem to get this statement to work correctly. I am experienced with Excel VBA, but new to Access VBA. Any help would be appreciated! Thank you!



    CurrentDb.Execute "INSERT INTO tblBC ([ID_Match]) VALUES (me.RC_ID)"

  2. #2
    pbaldy's Avatar
    pbaldy is online now Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    You would need to concatenate the form value:

    CurrentDb.Execute "INSERT INTO tblBC ([ID_Match]) VALUES (" & me.RC_ID & ")"

    Most of us would use a variable for the SQL to make debugging easier, though this is pretty simple.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    you cant put the variable in quotes so:
    execute "INSERT INTO tblBC ([ID_Match]) VALUES (" & me.RC_ID & ")"

  4. #4
    stacynh is offline Novice
    Windows 8 Access 2013 64bit
    Join Date
    Jul 2016
    Location
    Michigan
    Posts
    8
    Thank you for your quick response! I am still receiving the error "Run-Time error 3134: Syntax Error." I am wondering if there is an issue with the quotes because it is a numeric value instead of text? Or, possibly I need to chose a different primary key in tblBC?

  5. #5
    pbaldy's Avatar
    pbaldy is online now Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Try using a variable so you can review/test the SQL:

    http://www.baldyweb.com/ImmediateWindow.htm
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 8
    Last Post: 01-01-2020, 11:09 PM
  2. Replies: 5
    Last Post: 06-25-2017, 01:24 PM
  3. Replies: 2
    Last Post: 10-08-2016, 10:06 PM
  4. Replies: 2
    Last Post: 02-23-2016, 04:36 PM
  5. Replies: 0
    Last Post: 08-23-2013, 05:46 PM

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
  •  
Other Forums: Microsoft Office Forums