Results 1 to 11 of 11
  1. #1
    StephenVW is offline Novice
    Windows 8 Access 2013
    Join Date
    Mar 2013
    Posts
    6

    VB Code for INSERT INTO

    Ok, I'm fairly new to Access. What I'm attempting to accomplish is using a command button to insert short text to a specific field within a table. So for example, in my Master form I'd like to have a command button, that when pushed, inserts the text "A1" into the Location field in my Master Inventory table. I tried accomplishing this by using the INSERT INTO statement, but I had no luck. Is this the best way to do it, or is there a simpler way? If it's the best way, then what am I doing wrong? Here is the code I used:

    'add data to Master Inventory Table


    CurrentDb.Execute "INSERT INTO MasterInventoryTable (Location) " & _
    " VALUES ('
    A1') "
    Last edited by June7; 03-23-2013 at 07:17 PM. Reason: fix code to be more readable

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    That SQL should create a new record in table. Don't see anything wrong with code. Why doesn't it work - error message, wrong results, nothing happens? Why do you need to do this? What are you really trying to accomplish?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    StephenVW is offline Novice
    Windows 8 Access 2013
    Join Date
    Mar 2013
    Posts
    6
    Nothing happens, there's no error or anything. I need to do this to upload location on inventory, but I'd like to just create several command buttons to input location and not a combo box, list box, or anything like that.

    EDIT: It seems I get this error -- Runtime error '3134': Syntax error in INSERT INTO statement.

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Do you want to create a new record?

    Or do you want to enter data into current record displayed on form?

    Your code would create new record. Are you sure nothing happens? Not seeing new record in table? Step debug. Refer to link at bottom of my post for debugging guidelines.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    StephenVW is offline Novice
    Windows 8 Access 2013
    Join Date
    Mar 2013
    Posts
    6
    Ok, I got it to work. Here's what I did.

    CurrentDb.Execute "INSERT INTO MasterInventoryTable (Location, Item)" & _
    "VALUES ('A1', '')"

    Only problem now is that I can only input A1 once, and when if I hit it again it won't input the new data.

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Why are you putting empty string in Item field?

    Why would you enter another identical record?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  7. #7
    StephenVW is offline Novice
    Windows 8 Access 2013
    Join Date
    Mar 2013
    Posts
    6
    For some reason, when I put in the next field and have it enter no information it worked. I figured this out by trying to update multiple fields instead of just one.

    The record is not identical, because I could have a certain item in the same location. Each record has Location, Item, Color, Qty, Qty by Post, and Stage. I'm simply only inserting the location.

  8. #8
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    But you are not updating data in existing record, the INSERT is creating a new record in table.

    If you want to populate field of current record on form, can simply:

    Me!Location = "A1"
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  9. #9
    StephenVW is offline Novice
    Windows 8 Access 2013
    Join Date
    Mar 2013
    Posts
    6
    Ok, I understand. Here's the code I was using to update the record, however, it would update all records in that field.

    CurrentDb.Execute "UPDATE MasterInventoryTable" & _
    "SET Item = ('" & Me.ItemID & "')"

    So here's what I tried doing. But there's a syntax error due to the Where criteria.

    CurrentDb.Execute "UPDATE MasterInventoryTable" & _
    "SET Item = ('" & Me.ItemID & "')" & _
    "WHERE Item = ('NULL')"

    I only want to update blank records in the Item field, so if there's any data in them it will not update with new data.

  10. #10
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Null is never a string value so placing within quotes or apostrophes does not work, not if the field is truly null and isn't actually populated with the word NULL.

    Can't use = Null in expressions http://allenbrowne.com/casu-12.html

    Try:
    WHERE Item Is Null
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  11. #11
    StephenVW is offline Novice
    Windows 8 Access 2013
    Join Date
    Mar 2013
    Posts
    6
    Awesome! Seems like that was the trick! Thank you for your help

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

Similar Threads

  1. Replies: 2
    Last Post: 12-04-2012, 01:03 AM
  2. VBA code syntac doubt "Before Insert"
    By ramindya in forum Programming
    Replies: 1
    Last Post: 02-12-2012, 07:52 PM
  3. Problem with INSERT INTO query code
    By rghollenbeck in forum Queries
    Replies: 8
    Last Post: 09-27-2011, 12:16 PM
  4. Replies: 3
    Last Post: 09-13-2011, 07:58 PM
  5. Forgot how to insert Code in a Thread
    By RAPSR in forum Programming
    Replies: 1
    Last Post: 10-11-2010, 10:04 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