Results 1 to 10 of 10
  1. #1
    NISMOJim is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2010
    Posts
    273

    INSERT INTO for Not In List

    This is the beginning of what will probably be a two-part question. I've tried several variations on working code I've used in the past, and scoured the internet until my eyes fell out (I'm better now), but I still can't get it working.
    I have a combobox (cboVstrName) on a form using the field "VstrName" as the row source from tblVstr (other fields in this table are "VstrID {PK, autonumber} and VstrCompany). Limit To List is set to "Yes". I'm trying to add the following code to the "Not In List" to add the name to the table, so it will show up in the dropdown box the next time...

    Private Sub cboVstrName_NotInList(NewData As String)
    'Adds visitor name to tblVstr if not already on dropdown list
    Dim strSQL As String

    DoCmd.SetWarnings False
    strSQL = "INSERT INTO tblVstr (VstrName)"
    strSQL = strSQL & "Select """ & NewData & """;"
    DoCmd.RunSQL strSQL
    DoCmd.SetWarnings True
    Response = acDataErrAdded

    End Sub



    but I get this error...
    Click image for larger version. 

Name:	Untitled.jpg 
Views:	21 
Size:	21.7 KB 
ID:	21491

    How can I make this work?

  2. #2
    NISMOJim is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2010
    Posts
    273
    Now that I've looked at things, I'm getting this error on events for every control on the form. Everything else worked great yesterday, now things that weren't even changed are sending errors. What happened?

  3. #3
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    That error message does not look familiar to me but I would start to look for code in my form's module that is out of place. I would look for code that is not within a Sub or a Function or a misplaced/duplicate End Sub or End Function termination line. Have you tried Compiling your code?

  4. #4
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    Are you entering this code freehand? I ask because when using the ellipsis in the Property Pane to go to the code window,

    Private Sub cboVstrName_NotInList(NewData As String)

    would be generated as

    Private Sub cboVstrName_NotInList(NewData As String, Response As Integer)

    The NotInList parameters the Access Gnomes expect are NewData As String and Response As Integer, and that missing second parameter is what they're balking at; the parameters you have don't match the parameters that Access expects, hence the error message!

    Experience has shown that, at least in 2007/2010 and 2013, some of the things in code that Access checks for are only checked for when the Form is opened directly, in Form View, not when it is opened in Design View and then changed to Form View.

    An example of this involves the 'Trusted' status; Access will run code, when going from Design View to running it, in Form View, even with the Folder not 'trusted,' but will balk when opened directly in Form View, under the same conditions.

    Linq ;0)>

  5. #5
    CJ_London is online now VIP
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,932
    when trying to debug code, it always helps to comment out setwarnings and other error handling so you can see the actual errors

    In addition to Linq's comment, it looks like you are missing a space before Select

  6. #6
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,870
    Ajax,

    I don't think access sql will have a problem with no space before the Select

    Code:
    "INSERT INTO tblVstr (VstrName)Select """ ....
    I would put the space, but I don't think 'no space' violates syntax.

  7. #7
    CJ_London is online now VIP
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,932
    you're right - never knew that

  8. #8
    NISMOJim is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2010
    Posts
    273
    I won't get a chance to try this out until Wednesday, but thanks for starting me somewhere. When I first tried the code I typed "Me.cboVstrName" instead of "NewData". I thought that might be where the error came from, but I guess not. I removed the "Response as integer" part because I thought it referred to a yes/no answer to a message box that I'm not using. Some of the controls are throwing this error on "OnClick" events that don't even have a "NotInList" event. I did look through all of the code for errors, but nothing I noticed looked wrong. Not sure if I would recognize an error or not at this point.

  9. #9
    NISMOJim is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2010
    Posts
    273
    Mr. Linq, you've done it again! As soon as I added "Response as Integer", everything on the form worked perfectly. As for the second part of my question, I figured it out through other posts. Thanks again, you guys are priceless!

  10. #10
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    Glad we could help!

    Linq ;0)>

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

Similar Threads

  1. Replies: 3
    Last Post: 05-27-2015, 01:04 PM
  2. Replies: 5
    Last Post: 10-07-2014, 09:20 AM
  3. INSERT query: insert new data only
    By drh in forum Access
    Replies: 2
    Last Post: 04-04-2014, 05:31 PM
  4. Replies: 1
    Last Post: 10-26-2012, 05:17 PM
  5. Insert drop-down list in Parent Field in a form
    By Haleakala17 in forum Forms
    Replies: 7
    Last Post: 09-11-2012, 07:20 PM

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