Results 1 to 15 of 15
  1. #1
    d9pierce1 is offline Expert
    Windows 10 Access 2016
    Join Date
    Jan 2012
    Location
    Oklahoma
    Posts
    776

    Not In List compile error, varible not defined?

    CompanyTesterVlad.zip

    Good morning all,
    I just ran into an error testing out a NotInList event which i have used a lot over time with no issues.
    So while testing this out it is asking me to declare a varible? What varible is not decared all ready?

    Code:
    Private Sub CboLocationType_NotInList(NewData As String, Response As Integer)
    Dim strsql As String, x As Integer
    x = MsgBox("Location Name is Not in Current List, Would you Like to Add?", vbYesNo)
    If x = vbYes Then
    strsql = "INSERT INTO tblHelper (HelperTypeID, HelperValue) " & _
            "VALUES (" & CboLocationType & ", '" & NewData & "')"
        'MsgBox strsql
        CurrentDb.Execute strsql, dbFailOnError
        Response = acDataErrAdded
    Else
        Response = acDataErrContinue
    End If
    
    
    End Sub
    I dont get why this creates an error. I have the same code in many db's and it works fine!!!!
    What have I not done correctly?



    Update: OK, I see where I beleive the issue is and its in the rowsource of the CboLocationType.
    So, Instead of putting the Cbo in that i put "1" in and it works. Not sure if this is the correct way
    to do so but see new code.

    Code:
    Private Sub CboLocationType_NotInList(NewData As String, Response As Integer)
        Dim strsql As String, x As Integer
        x = MsgBox("Location Name is Not in Current List, Would you Like to Add?", vbYesNo)
        If x = vbYes Then
        strsql = "INSERT INTO tblHelper (HelperTypeID, HelperValue) " & _
                "VALUES (" & 1 & ", '" & NewData & "')"
            'MsgBox strsql
            CurrentDb.Execute strsql, dbFailOnError
            Response = acDataErrAdded
        Else
            Response = acDataErrContinue
        End If
        
    End Sub
    Thanks
    Dave
    Last edited by d9pierce1; 03-14-2021 at 10:09 AM. Reason: Found what was creating error, possible solution

  2. #2
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,834
    Did you compile your code? That ought to make it obvious. If the same code works in other db's, that suggests a missing reference. Perhaps DAO.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  3. #3
    d9pierce1 is offline Expert
    Windows 10 Access 2016
    Join Date
    Jan 2012
    Location
    Oklahoma
    Posts
    776
    Hi Micron,
    Yes, I did compile and it had one error which was "dbFailOnError" highlighted in blue. I do have dao in references. I put a "1" in instead of the CboLocationType as the row source in that didnt have the HelperTypeID in it and it worked. Not sure or not if correct method but it does work now.
    Suggestions....
    See Update Code above


  4. #4
    Gicu's Avatar
    Gicu is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,125
    In the sample db I added a reference to Microsoft Office 15.0 Access database engine Object Library and the compile error is gone. As for your initial question the Me.cboLocationType is bound to HelperID which is the autonumber PK of tblHelper. HelperTypeID (=1) is not in the combo's row source. So you have to leave it hard coded as you currently have it (1) as you do not have it in the combo.
    Cheers,
    Vlad
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  5. #5
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,834
    Well I haven't looked too far beyond what I found when I posted that because your code failed to compile until I added the reference. At first glance, it seems that the NotInListEvent is balking about adding a new value and will re-fire every time you try to leave the control, yet it will add multiple new values to the table anyway.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  6. #6
    d9pierce1 is offline Expert
    Windows 10 Access 2016
    Join Date
    Jan 2012
    Location
    Oklahoma
    Posts
    776
    Quote Originally Posted by Gicu View Post
    In the sample db I added a reference to Microsoft Office 15.0 Access database engine Object Library
    strange, I dont have that reference checked in my db and didnt get any compile errors after i changed the Cbo to a "1" ...
    so, I tried adding that reference and it gave me a name conflict error?

    So, hard coding the #1 then is OK, I am so glad.

  7. #7
    d9pierce1 is offline Expert
    Windows 10 Access 2016
    Join Date
    Jan 2012
    Location
    Oklahoma
    Posts
    776
    Hi Micron,

    Not sure but i first compiled it, got the error, I added DAO 3.6, Still gave me error, and then i changed the Cbo.... to a #1, and compiled it again with success.

  8. #8
    Gicu's Avatar
    Gicu is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,125
    The DAO (3.6) library has been incorporated into the newer one I mentioned. I think hardcoding it or better using a dLookup to get the value from tblHelperType (dLookup("[HelperTypeID]","[tblHelperType]","[Description]='Location Type') would both work (I don't like to hard code values as sometimes the IDs could change).
    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  9. #9
    d9pierce1 is offline Expert
    Windows 10 Access 2016
    Join Date
    Jan 2012
    Location
    Oklahoma
    Posts
    776
    Hi Gicu

    I hate to be a pest but I am trying to understand this code in post #8 and i have no idea where that would go? In the rowsource of combo? I just not real sure and would love to know if you have the time.

    Thanks
    Dave

  10. #10
    Gicu's Avatar
    Gicu is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,125
    Hi Dave, it should replace the hard-coded 1 in the NotInList event:
    Code:
    strsql = "INSERT INTO tblHelper (HelperTypeID, HelperValue) " & _
                "VALUES (" & dLookup("[HelperTypeID]","[tblHelperType]","[Description]='Location Type') & ", '" & NewData & "')"
    Cheers,
    Vlad
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  11. #11
    d9pierce1 is offline Expert
    Windows 10 Access 2016
    Join Date
    Jan 2012
    Location
    Oklahoma
    Posts
    776
    thank you so much.... I looked that over and like ????? Thank you for your time explaining it.
    Dave

  12. #12
    d9pierce1 is offline Expert
    Windows 10 Access 2016
    Join Date
    Jan 2012
    Location
    Oklahoma
    Posts
    776
    CompanyTesterVlad - Copy.zip

    Hi Gicu

    I tested this out and I am getting a parameter error for [Description]. I have checked all spelling and cannot imagine why this is happening?
    I did have to add a " to the code, see below as it gave me an error and redded out when inserted into NIL statement code.

    Code:
    strsql = "INSERT INTO tblHelper (HelperTypeID, HelperValue) " & _
                "VALUES (" & dLookup("[HelperTypeID]","[tblHelperType]","[Description]='Location Type'") & ", '" & NewData & "')"
    Any ideas on why this would give an error?

    Thanks
    Dave

  13. #13
    Gicu's Avatar
    Gicu is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,125
    Hi Dave,
    Here is the entire sub (this one works), notice you have an spelling error, the fields is called Decription:
    Code:
    Private Sub CboLocationType_NotInList(NewData As String, Response As Integer)
        Dim strsql As String, x As Integer, lLocationType As Long
        x = MsgBox("Location Type is Not in Current List, Would you Like to Add?", vbYesNo)
        If x = vbYes Then
        lLocationType = DLookup("[HelperTypeID]", "[tblHelperType]", "[Decription]='Location Type'")
        strsql = "INSERT INTO tblHelper (HelperTypeID, HelperValue) " & _
                "VALUES (" & lLocationType & ", '" & NewData & "')"
         
            'MsgBox strsql
            CurrentDb.Execute strsql, dbFailOnError
            Response = acDataErrAdded
        Else
            Response = acDataErrContinue
        End If
    End Sub
    Cheers,
    Vlad
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  14. #14
    d9pierce1 is offline Expert
    Windows 10 Access 2016
    Join Date
    Jan 2012
    Location
    Oklahoma
    Posts
    776
    Thanks, I am so sorry.... I looked that over and over and didnt see the spelling error... Embarasing\
    Thanks

  15. #15
    Gicu's Avatar
    Gicu is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,125
    No worries Dave, it happens to all of us (I didn't notice it the first time I looked to write the dLookup ).
    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

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

Similar Threads

  1. Compile error User-defined type not defined
    By Ashfaque in forum Modules
    Replies: 8
    Last Post: 03-03-2021, 03:37 AM
  2. Replies: 4
    Last Post: 10-16-2017, 09:09 AM
  3. Compile Error Sub or function not defined
    By Ray67 in forum Reports
    Replies: 3
    Last Post: 07-02-2012, 04:11 PM
  4. Replies: 4
    Last Post: 06-08-2012, 09:08 AM
  5. Compile error. Sub of function not defined
    By plavookins in forum Reports
    Replies: 7
    Last Post: 04-22-2011, 10:15 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