Results 1 to 4 of 4
  1. #1
    TheKillerMonkey is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2016
    Posts
    26

    INSERT INTO creates a pop up messagebox?

    Hello,



    I'm trying to add a new user into a table. The code essentially works except that for my user I'm getting a msgbox with a caption of what I've put in as the new user.

    Click image for larger version. 

Name:	InsertIntoPopup.png 
Views:	8 
Size:	16.7 KB 
ID:	24400

    Code:
    SQL = "Insert into Users (Username,SecurityLevel_FK) "
    SQL = SQL & "Values (" & Forms!SystManHome!NSfSystManHome.Form!tbNewUser & ","
    SQL = SQL & DLookup("[ID]", "tblSecurityLevels", "[Level]='" & Forms!SystManHome!NSfSystManHome.Form!cbSecLevels & "'") & ")"
    
    
    DoCmd.RunSQL SQL
    Did 2nd try with tester and User in the boxes

    Click image for larger version. 

Name:	InsertIntoImmediate.png 
Views:	8 
Size:	2.8 KB 
ID:	24401

    Thanks!
    -Matt

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,549
    1 you don't need DLOOKUP for a combo box. The value is already in the combo.
    2. you don't put dlookup in sql
    3. text box is a string, so the sql must be surrounded by string quotes.
    ... & "Values ('" & Forms!SystManHome!NSfSystManHome.Form!tbNewUser & "',"

  3. #3
    ssanfu is offline Master of Nothing
    Windows XP Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Is the field "Username" a text type field or numeric? If it is a text type field (you are storing text), there needs to be delimiters in the SQL string.
    Code:
    SQL = "Insert into Users (Username,SecurityLevel_FK) "
    SQL = SQL & "Values ('" & Forms!SystManHome!NSfSystManHome.Form!tbNewUser & "',"   ' <--single quotes in this line
    SQL = SQL & DLookup("[ID]", "tblSecurityLevels", "[Level]='" & Forms!SystManHome!NSfSystManHome.Form!cbSecLevels & "'") & ")"
    Expanded, it looks like:
    SQL = SQL & "Values ( ' " & Forms!SystManHome!NSfSystManHome.Form!tbNewUser & " ' ,"



    Also, it might be easier to create two variables to get the values and use the variables in the SQL.

    Code:
    Dim UN as String
    Dim SL_FK as Long
    
    
    UN = Forms!SystManHome!NSfSystManHome.Form!tbNewUser
    SL_FK = [SIZE=2]DLookup("[ID]", "tblSecurityLevels", "[Level]='" & Forms!SystManHome!NSfSystManHome.Form!cbSecLevels & "'")
    
    SQL = "Insert into Users (Username,SecurityLevel_FK) "
    SQL = SQL & "Values ('" & UN & "', " & SL_FK & ")"
    '  Debug.Print SQL
    Currentdb.Execute SQL, dbfailonerror
    This makes it easier to check the values when single stepping through the code.


    BTW, "SQL" is a reserved word in Access and shouldn't be used as an object name. Maybe use "strSQL" or "sSQL".



    Edit: ranman256 is correct. You should be able to get the security level froim the combo box.
    I missed that......
    Last edited by ssanfu; 04-20-2016 at 01:38 PM. Reason: misread OP

  4. #4
    TheKillerMonkey is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2016
    Posts
    26
    Awesome, adding the single quotes helped. Thanks for the feedback!

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

Similar Threads

  1. Messagebox popping up twice?
    By 82280zx in forum Programming
    Replies: 26
    Last Post: 07-08-2014, 11:29 AM
  2. MessageBox if the report is Empty
    By annyK in forum Reports
    Replies: 2
    Last Post: 10-14-2013, 03:53 AM
  3. On Change or Before Update? MessageBox
    By burrina in forum Forms
    Replies: 6
    Last Post: 01-05-2013, 12:09 PM
  4. messagebox help :D
    By imintrouble in forum Forms
    Replies: 2
    Last Post: 10-20-2011, 01:50 PM
  5. Make a messagebox
    By carstenhdk in forum Queries
    Replies: 7
    Last Post: 05-18-2010, 06:18 AM

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