+ Reply to Thread
Results 1 to 2 of 2

Trouble with INSERT statement

  1. #1
    Registered User
    Join Date
    07-22-2011
    Location
    Boston, MA
    MS-Off Ver
    Excel 2007
    Posts
    21

    Trouble with INSERT statement

    Hi all,

    I'm trying to execute an insert statement in VBA into an access database. Basically, I have a UserForm that takes an ID, Last Name, Middle Name, and First Name from the user. When the data is submitted, the insert statement will run. One of the values of the insert should be a "NICKNAME". The important part to mention is that there is a table called NAMEMASTER that is guaranteed to have the columns "FirstName", "MiddleName", "FirstName", and a "NickName" that will be unique to every unique combination of a user's First Name, Middle Name, and Last Name (this data is pre-loaded into the table, so it is guaranteed to be there). Below is my code:

    insert into TABLE_A
    (
    ID,
    LASTNAME,
    MIDDLENAME,
    FIRSTNAME,
    NickName
    )
    VALUES
    (
    123,
    'Smith',
    'Andrew',
    'Joseph',
    (
    SELECT NAMEMASTER.NickName
    FROM NAMEMASTER
    WHERE
    NAMEMASTER.LASTNAME = 'Smith' AND
    NAMEMASTER.MIDDLENAME = 'Andrew' AND
    NAMEMASTER.FIRSTNAME = 'Joseph'
    )
    )




    When I execute this code, I get the error message: "Query input must contain at least one table or query." If I remove the nested select from the "values" section of the insert (as well as the "NickName" column from the "insert" section, the error doesn't occur. The select works perfectly on its own if i run it in the access database, and it might be equally important to add that the NickName column in the database is Text.

    I know this was wordy, but I wanted to make sure I didn't leave anything out.


    Any help on this at all would be GREATLY appreciated.

    Thanks so much!!!

  2. #2
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,168

    Re: Trouble with INSERT statement

    What happens if you have another text under 'Joseph', ?

    Maybe you should have a blank or something to fill a NickName spot.
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1