Page 2 of 3 FirstFirst 123 LastLast
Results 16 to 30 of 37
  1. #16
    aytee111 is offline Competent At Times
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    When you convert a macro to code, Access creates a VBA module with the macro's name, in this case it would be "macro1" or something like that. You can open it from the navigation pane by double-clicking on it. Once open, copy the code (without the first and last lines) to your OnClick event.



    Well done for getting this far!

  2. #17
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,926
    If the macro was embedded in an event, such as button OnClick, after converting macro the event property should show [Event Procedure] then click the ellipsis (...) to go to the VBA procedure.

    Review https://support.office.com/en-us/art...0-DAB7C75CBE0C
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #18
    JBros is offline Novice
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    21
    Hello and thank you both for your appreciate answers. Finally I've converted the Macro into VBA in a way I can access it, and in the navigation panel now I have the module for the Macro to access it through VBA. Here's the code of the Macro so far:

    Code:
    Function OnClick()
    On Error GoTo OnClick_Err
    
        With CodeContextObject
            If (IsNull(Forms![Company Detail]!Id)) Then
                Beep
            End If
            If (Not IsNull(Forms![Company Detail]!Id)) Then
                DoCmd.OpenForm "Company Detail", acNormal, "", "[Id]=" & .Id, , acDialog
            End If
        End With
    
    
    OnClick_Exit:
        Exit Function
    
    OnClick_Err:
        MsgBox Error$
        Resume OnClick_Exit
    
    End Function
    For the moment, I think the code will work to what I'm doing. However, I'm still reluctanct when it comes to add code because I may change something I don't wish to. Which would be the code to add at the beginning so the form can be opened and not appear the error message? Did you already post it?

    I'll keep checking Access documentation and try to familiarize more with it. I'd also like to, as said before, the elements in the sub-form (the names of the companies) appear like a hyperlink so it is more visual that you may click on it to see information. Again, thank you very much, this is being really useful for me thank you.

  4. #19
    aytee111 is offline Competent At Times
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    Like I said before, you are referring to a form that hasn't been opened yet right from the start, that is where your error is coming from. Maybe you want to check the ID of the form that you are on instead? It would be If IsNull(Me!ID)

    The pseudo code would be:
    If IsNull(Me!ID) Then
    Beep
    Else
    If detail form is open then (post #6)
    filter it (post #8)
    else
    DoCmd.OpenForm....
    End If

  5. #20
    JBros is offline Novice
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    21
    Quote Originally Posted by aytee111 View Post
    Like I said before, you are referring to a form that hasn't been opened yet right from the start, that is where your error is coming from. Maybe you want to check the ID of the form that you are on instead? It would be If IsNull(Me!ID)

    The pseudo code would be:
    If IsNull(Me!ID) Then
    Beep
    Else
    If detail form is open then (post #6)
    filter it (post #8)
    else
    DoCmd.OpenForm....
    End If
    I don't think I'm doing it well, as too many If statements make me confuse a little bit; thus, the filter part I haven't completely understood it so I'm pretty sure I'm using a bad syntax. I've tried some things but the error keeps appearing. I'm sharing my code so you can see where the syntax is not good.

    Code:
    Function OnClick()
    On Error GoTo OnClick_Err
    
        With CodeContextObject
            If (IsNull(Me!Id)) Then
                Beep
            Else
            If CurrentProject.AllForms("Company Detail").IsLoaded = False Then
                ' Form is closed
            Else
                ' Form is open
            End If
            Forms!companydetailsform.Filter = "companyid=" & Me!companyid
            Forms!companydetailsform.FilterOn = True
            Forms!companydetailsform.Filter.Requery
            If (Not IsNull(Forms![Company Detail]!Id)) Then
                DoCmd.OpenForm "Company Detail", acNormal, "", "[Id]=" & .Id, , acDialog
            End If
        End With
    
    
    OnClick_Exit:
        Exit Function
    
    OnClick_Err:
        MsgBox Error$
        Resume OnClick_Exit
    
    End Function
    Thank you very much.

  6. #21
    aytee111 is offline Competent At Times
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    Code:
    Function OnClick()
    On Error GoTo OnClick_Err
    
        With CodeContextObject
            If (IsNull(Me!Id)) Then
                Beep
            Else
            If CurrentProject.AllForms("Company Detail").IsLoaded = False Then
                ' Form is closed
                DoCmd.OpenForm "Company Detail", acNormal, "", "[Id]=" & Me!Id, , acDialog
            Else
                ' Form is open
                Forms![Company Detail].Filter = "id=" & Me!ID
                Forms![Company Detail].FilterOn = True
                Forms![Company Detail].Requery
            End If
        End With
    
    
    OnClick_Exit:
        Exit Function
    
    OnClick_Err:
        MsgBox Error$
        Resume OnClick_Exit
    
    End Function

  7. #22
    JBros is offline Novice
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    21
    Thank you for fixing the code. However, I'm still struggling to make it work. The error that shows still is the same. I've named this macro 'OnClick', so in the events of the table it appears like this:

    Click image for larger version. 

Name:	c1.PNG 
Views:	6 
Size:	9.6 KB 
ID:	29528

    However, when I click in Access to see the Macro, it appears like this, which I think it is the same structure than before the changes:

    Click image for larger version. 

Name:	c2.PNG 
Views:	6 
Size:	14.7 KB 
ID:	29529

    On the navigation panel, I have the Macro 'OnClick' in the part of 'Macros'. When I click on it, it shows the same error than always. When I click on the VBA in modules it appears the VBA with the code you've just provided.

    Click image for larger version. 

Name:	c3.PNG 
Views:	6 
Size:	8.9 KB 
ID:	29530

    I don't know if with this reference you may know what may be happening here that shows the same error again and again... Thank you very much for all your help so far

  8. #23
    aytee111 is offline Competent At Times
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    In the properties list, in English it is called "OnClick". Next to this you will select [Event Procedure]. Then click on the 3 dots next to it and it will take you to the VBA code attached to that event. That is where you will put the code.

    Or if you want to leave it as a macro (although I don't see why you would want to do this) you can run the VBA function inside your macro using RunCode.

  9. #24
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,926
    Did you thoroughly review the referenced tutorial link in post 17? It describes in detail how to convert macros to VBA and how to view the resulting VBA procedure.

    I agree with aytee111, if you use VBA, why mix in macro coding? Move the code into the button's Click VBA procedure, not in a general module.

    I have never seen CodeContextObject. Did the macro converter generate that? I don't think it is necessary.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  10. #25
    JBros is offline Novice
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    21
    Thank you both for the answers.

    I have never seen CodeContextObject. Did the macro converter generate that? I don't think it is necessary.
    Yes, this was actually automatically generated.

    Regarding the main topic, I've changed the 'OnClick' event to where it says [Event Procedure] and added the code to the VBA associated to it. However, now it pops up another error message. The code that is in the [Event Procedure] VBA is this one:

    Code:
    Private Sub Company_Click()
    
    End Sub
    Function OnClick()
    On Error GoTo OnClick_Err
    
        With CodeContextObject
            If (IsNull(Me!Id)) Then
                Beep
            Else
            If CurrentProject.AllForms("Company Detail").IsLoaded = False Then
                ' Form is closed
                DoCmd.OpenForm "Company Detail", acNormal, "", "[Id]=" & Me!Id, , acDialog
            Else
                ' Form is open
                Forms![Company Detail].Filter = "id=" & Me!Id
                Forms![Company Detail].FilterOn = True
                Forms![Company Detail].Requery
            End If
        End With
    
    
    OnClick_Exit:
        Exit Function
    
    OnClick_Err:
        MsgBox Error$
        Resume OnClick_Exit
    
    End Function
    However, now the error message that appears says this:

    Click image for larger version. 

Name:	r1.PNG 
Views:	5 
Size:	16.2 KB 
ID:	29554

    Which translated would be something like:

    The expression 'OnClick' you've specified as a value of the event property give the error 'The member already exists in an object module from where it is from'. This error occurs when an event could not be executed because the location of the event logic can not be evaluated. For example, if the OnOpen property of a form is set to = [Field], this error occurs because an event or macro name is expected to be executed at the occurrence of the event.

    I don't know why this is actually that complicated... do you have any ideas why it gives me this error? Thank you very much.

  11. #26
    aytee111 is offline Competent At Times
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    When you click on the three dots next to Event Procedure, what does it show you? From here it looks like the subroutine is empty, there is no code associated with the event. This is showing that when you click on the field named "Company" nothing will happen. So where can the error coming from? There is something/somewhere else that we are not being shown.

  12. #27
    JBros is offline Novice
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    21
    I have re-created the database in order to avoid possible past mistakes. So, first of all I create the table 'Companies' and fill in some data. Next, I'd create the Subform Companies with the information in Companies' table. Then I create the 'Home' form, which is an empty form with the subform Companies embedded. It shows correctly. Finally I create the 'Company Detail' form, which is an emergent form with some data from the 'Companies' table.

    Then I go to detail view of 'Home' form, click into the 'Company name' field inside the SubForm and select [Event Procedure], press the ... button and add the code. When I look into it again, it shows this code:

    Code:
    Private Sub Form_Click()
    Function OnClick()
    On Error GoTo OnClick_Err
    
        With CodeContextObject
            If (IsNull(Me!Id)) Then
                Beep
            Else
            If CurrentProject.AllForms("Company Detail").IsLoaded = False Then
                ' Form is closed
                DoCmd.OpenForm "Company Detail", acNormal, "", "[Id]=" & Me!Id, , acDialog
            Else
                ' Form is open
                Forms![Company Detail].Filter = "id=" & Me!Id
                Forms![Company Detail].FilterOn = True
                Forms![Company Detail].Requery
            End If
        End With
    
    
    OnClick_Exit:
        Exit Function
    
    OnClick_Err:
        MsgBox Error$
        Resume OnClick_Exit
    
    End Function
    End Function
    The error shown is the same as before. It seems that it says that the register I'm clicking on it already exists in the destination form; well, that's what I want as the Company I click should be the same company that appears in 'Company detail' but just with some more detail. I can't figure out for the moment where the error can be. I'd provide more information if necessary but I think this is pretty much all. Thank you very much.

  13. #28
    aytee111 is offline Competent At Times
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    Sub and Function are both words used to denote the start of the routine. You have two End Function's and no End Sub. I don't see how Access could have created this.

    Private Sub Form_Click() - this is an OnClick event for the form, not for the Company.

    You are doing something wrong somewhere! Click on the Company field, make sure it is highlighted, in the Properties list make sure that the name of the object is Company. Then click on the three dots and it should end up looking something like:
    Private Sub Company_Click()

    With
    ....
    End With

    End Sub

  14. #29
    JBros is offline Novice
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    21
    Hello,

    you are right with your response. I've referred to the specific 'Company' field just like this:

    Click image for larger version. 

Name:	CC1.PNG 
Views:	3 
Size:	37.6 KB 
ID:	29555

    And when I click the three dots it appears this code:

    Click image for larger version. 

Name:	CC2.PNG 
Views:	3 
Size:	20.4 KB 
ID:	29556

    I've manually changed the last 'End Function' for an 'End Sub'. Is the idea clear for Access this way? The error it keeps showing is the same. Any thoughts on where or what may be causing this to not work? Thank you very much.

  15. #30
    aytee111 is offline Competent At Times
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    Make it look like I showed you in post #28. Remove all other code. Include all the code in between With til End With.

Page 2 of 3 FirstFirst 123 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 3
    Last Post: 05-28-2017, 07:36 PM
  2. Replies: 1
    Last Post: 01-21-2015, 02:02 PM
  3. Access macro vs. excel macro
    By twckfa16 in forum Macros
    Replies: 3
    Last Post: 01-07-2015, 03:44 PM
  4. Replies: 7
    Last Post: 03-27-2014, 11:47 AM
  5. Replies: 0
    Last Post: 03-04-2011, 10:28 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