Results 1 to 10 of 10
  1. #1
    Gregm66 is offline Advanced Beginner
    Windows 10 Access 2013 32bit
    Join Date
    Sep 2016
    Posts
    36

    Help with Compile error

    Hi all,

    I have created a form to save my recipes to my table Called Table2.
    Every time I click on the save button I get the following error.

    User-Defined type Not Defined.

    I am filling out all my text boxes and trying to save them to my table.



    Any help would be appreciated, I'm not quite sure where I have gone wrong.

    Below is the code I am using,

    Code:
    Option Compare Database
    Option Explicit
    
    
    
    Private Sub CmdSave_Click()
        'When a User is finished adding a Recipe, and presses Save.
    If Me.txtName = "" Or Me.txtIngredients = "" Or Me.txtMethod = "" Or Me.txtNotes = "" Or Me.txtNutritional = "" Then
        MsgBox "Please make sure make sure all fields are filled in place a full stop in a field if no info required", vbInformation
        Exit Sub
    End If
    Dim cmd As Command
    Set cmd = New Command
    'Query that inserts a new Recipe into the Recipe Table. Users will not be able to access the DB if they are not in the User Table.
        cmd.ActiveConnection = CurrentProject.Connection
            cmd.CommandText = "INSERT INTO Table2 (Category, RcpName, rcpIngredients, rcpMethod, Nutritional, PerServe, rcpNotes, Image) " _
                            & "VALUES('" & Me.txtCategory & "', '" & Me.txtName & "', '" & Me.txtIngredients & "', '" & Me.txtMethod & "', '" & Me.txtNotes & "', '" & Me.txtNutritional & "','" & Me.txtCount & "', '" & Me.txtImageName & "')"
            cmd.Execute
    Set cmd = Nothing
    Me.Refresh
    End Sub

  2. #2
    Micron is offline Virtually Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    You are likely missing a project reference - probably ADO.
    It helps if you specify which code line the failure occurs on. When you Dim something that belongs to ADOB or DAO, you should always be explicit about the library, such as
    Dim cmd As ADODB Command
    Last edited by Micron; 09-20-2016 at 06:44 PM. Reason: clarification
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  3. #3
    Gregm66 is offline Advanced Beginner
    Windows 10 Access 2013 32bit
    Join Date
    Sep 2016
    Posts
    36
    thanks for your reply Micron
    the first line highlighted yellow is:

    Code:
    Private Sub CmdSave_Click()
    then highlighted blue is:

    Code:
    Dim cmd As Command

  4. #4
    Micron is offline Virtually Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    Not meaning to be condescending, but did you understand my proposed solution?

  5. #5
    Gregm66 is offline Advanced Beginner
    Windows 10 Access 2013 32bit
    Join Date
    Sep 2016
    Posts
    36
    to be totally honest with you Micron, I am not quite sure what you meant, I am only new to programming in access, so excuse my ignorance when I say I have no idea, I am still trying to get my head around the coding side of things.

  6. #6
    Micron is offline Virtually Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    OK, you must know how to get at the code since you've either created it or copied and pasted it into a button click event. This is done in the VB editor, where you must have a reference to any "library" that Access needs, or else you get the message you got. Some of these references cannot be removed (un-referenced), but many are optional. Access thinks that the object you're trying to create is a user-created thing because you've not given Access any reference needed for it to create and manage the object.

    In the vb editor you should find the Tools > References menu item. Select that and a dialog showing lots of available references from which you need to locate and check the box for (I believe) an ActiveX Data Objects library version. Which one I can't tell you since I rarely use ADO, but I'd try the version with the highest number.
    Last edited by Micron; 09-20-2016 at 07:19 PM. Reason: clarification

  7. #7
    Gregm66 is offline Advanced Beginner
    Windows 10 Access 2013 32bit
    Join Date
    Sep 2016
    Posts
    36
    thanks Micron

    I have done as you suggested, but still not working, would this reference still apply if I changed my code to the Following:
    I am trying different approaches from code that I have found by searching google etc. but I am still having difficulties getting it to save the form data back to the table.

    Code:
    Option Compare Database
    Option Explicit
    
    
    
    Private Sub CmdSave_Click()
        Dim db As Database
    Dim rec As Recordset
    
    
    Set db = CurrentDb
    Set rec = db.OpenRecordset("Select * from Table2")
    
    
    rec.AddNew
    rec("Category") = Me.txtCategory
    rec("RcpName") = Me.txtName
    rec("rcpIngredients") = Me.txtIngredients
    rec("rcpMethod") = Me.txtMethod
    rec("Nutritional") = Me.txtNutritional
    rec("PerServe") = Me.txtCount
    rec("rcpNotes") = Me.txtNotes
    rec("Image") = Me.txtImageName
    rec.Update
    
    
    Set rec = Nothing
    Set db = Nothing
    End Sub

  8. #8
    Micron is offline Virtually Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    would this reference still apply if I changed my code to the Following
    The reference mentioned may not apply but the problem may persist for the same reason. I stated that you should always be explicit about the library yet you still are not. Wherever you got that code from, it is not written as it should be. When working with record sets, one typically declares them as ADO or DAO record sets, depending on which reference you have set. If you don't, Access will pick the first library reference in order of your set preference, but if you have not referenced either library, expect the problem still.

    What you're doing seems overly complicated for the stated objective. It should be a simple matter to either navigate to a new record in the form's record set and add the info or open the form to a new single to add the info. This can be affected by your design approach; e.g. is it a main/subform design, or a single form with separate controls, or a datasheet design, etc. Maybe take a look at this or Google "ms access add record to table from form".

    Re: I have done as you suggested, but still not working,
    - as my signature says, "doesn't work doesn't help".
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  9. #9
    Gregm66 is offline Advanced Beginner
    Windows 10 Access 2013 32bit
    Join Date
    Sep 2016
    Posts
    36
    ok I understand what you are saying Micron, my Recipe form has a filter and 2 cascading combo boxes, every time I go to add new record and try to enter into the Recipe name combo I get an error (Debug), due to the fact that I have also a field that references an image to for my for that is where it stops. hence why I wanted to make just a new recipe entry form to try and avoid this problem.
    the problem that keeps appearing this way is as follows.

    Code:
    Private Sub Combo9_Change()
        Me.Pic1.Picture = Me.Combo9.Column(2)
    End Sub

  10. #10
    Micron is offline Virtually Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    How many columns in the combo box? You realize that the list box and combo box column property is a zero based property (i.e. 0 is the first column, 1 is the 2nd and so on...)?
    You are beginning to lose me as to what's going on. It's like you had something that wasn't working (you're still being vague about the failure - "that is where it stops.") so you started over and that's where this post began. Now it seems like we're talking about what you originally had. At this point, I think it would be best if you posted a zipped copy of your db and gave explicit details as to what you're after and what's happening when you try to achieve whatever it is you're trying to achieve. We're at 9 posts now and seem to be going nowhere fast...

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

Similar Threads

  1. Replies: 7
    Last Post: 07-24-2013, 02:01 PM
  2. Compile Error: Syntax Error in DoCmd.RunSQL Statement
    By Evilferret in forum Programming
    Replies: 1
    Last Post: 08-27-2012, 12:32 PM
  3. Replies: 2
    Last Post: 06-23-2012, 11:59 PM
  4. Replies: 7
    Last Post: 06-08-2012, 09:55 PM
  5. Replies: 6
    Last Post: 09-28-2011, 09: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