Results 1 to 10 of 10
  1. #1
    kleaverjr is offline Advanced Beginner
    Windows 7 64bit Access 2013 64bit
    Join Date
    Apr 2022
    Posts
    53

    Code only works in Module and not in Form, why?

    I am pounding my head on the desk. So when I use the following code IN the Form that the controls are in I get a "Type Mismatch" Error. But if I move it to a Module, it works fine. I much rather load the combobox array (there are 15 comboboxes being loaded). Although they all are loaded with the same "list" the "list can change (so I can't just input the list in the properties list) depending on what the user selects. But all of that doesn't matter as the error is where this line of code is being located.

    This code does NOT work (I have simplified it because i use very long variables, and there are other controls on the Form that impact the code, but again the code works, but not in the Form, only in a separate module as I will explain, but I would like the code in the form if i can. And I have tried using Me.Controls, same error.



    I am using the term RECORDSET to represent the actual code that extracts the data from the Recordset from the DB. That part works fine too.

    Code:
    
    For comboboxindex = 1 To 15
    
     BrandIndex = 0
    
      Do
    
          if RECORDSET.EOF then exit do
    
          AddBrand(BrandIndex) = RECORDSET
    
         comboBrandName = "ComboBoxBrand" & comboboxindex
    
         Form_AddInvoice.controls(comboBrandName).additem (AddBrand(BrandIndex)), BrandIndex
      
        BrandIndex = BrandIndex + 1
    
        RECORDSET.MoveNext
    
      Loop
    
      RECOREDSET.MoveFirst
     
    Next comboboxindex

    Now when I make THIS change it works:

    Code:
    For comboboxindex
    
     BrandIndex = 0
    
      Do
    
         if RECORDSET.EOF then exit do
    
           AddBrand(BrandIndex) = RECORDSET
    
          comboBrandName = "ComboBoxBrand" & comboboxindex
    
         CALL UPDATEINVOICE
    
        BrandIndex = BrandIndex + 1
    
        RECORDSET.MoveNext
    
      Loop
    
      RECOREDSET.MoveFirst
     
    Next comboboxindex
    
    ***********************************************
    And this is a MODULE and NOT in the form..Also note All of the variables in this module are Global for the entire Project.
    
    
    
    
    UPDATE INVOICE:
    
       Form_AddInvoice.controls(comboBrandName).additem (AddBrand(BrandIndex)), BrandIndex
    So what am I doing wrong. How can I rework the code so it's all in one place? Thanks again!

    Ken L

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Have you step debugged? Do variables get correct values?

    I can't see anything wrong. Although, I use Forms! instead of Form_. Me. should work.

    Suggest you provide db for analysis.
    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. #3
    kleaverjr is offline Advanced Beginner
    Windows 7 64bit Access 2013 64bit
    Join Date
    Apr 2022
    Posts
    53
    Quote Originally Posted by June7 View Post
    Have you step debugged? Do variables get correct values?

    I can't see anything wrong. Although, I use Forms! instead of Form_. Me. should work.

    Suggest you provide db for analysis.
    Yes, I have done all that. I will try using Forms! instead and see if that works. I don't understand why the code works only in the module and I get a Type Mismatch error when it's in the Form itself as I copy and paste the Form_ line of code from the Form to the Module, and it works fine. It's bugs like this that really annoy me because as you said it should work but it doesn't.

  4. #4
    davegri's Avatar
    davegri is online now Excess Access
    Windows 11 Access 2019
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,413
    Perhaps a form can't refer to itself in that manner. Have you tried

    Code:
    Me.controls(comboBrandName).additem (AddBrand(BrandIndex)), BrandIndex

  5. #5
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Form_
    Forms!
    Me.

    All work for me in form module.

    Now I wonder why I got the habit of using Forms!, because it does not trigger intellisense tips and Form_ does.
    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.

  6. #6
    kleaverjr is offline Advanced Beginner
    Windows 7 64bit Access 2013 64bit
    Join Date
    Apr 2022
    Posts
    53
    Quote Originally Posted by davegri View Post
    Perhaps a form can't refer to itself in that manner. Have you tried

    Code:
    Me.controls(comboBrandName).additem (AddBrand(BrandIndex)), BrandIndex
    That seems to have done the trick. I would like to keep this thread "open" for a bit as I'm adding some other code which might create more bugs where the combobox array is concerned.

    Thanks again!

    Ken L.

  7. #7
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Does not hurt to start a new thread. New thread might get more attention.

    You said you already tried Me.Controls - what was different?
    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.

  8. #8
    kleaverjr is offline Advanced Beginner
    Windows 7 64bit Access 2013 64bit
    Join Date
    Apr 2022
    Posts
    53
    Quote Originally Posted by June7 View Post
    Does not hurt to start a new thread. New thread might get more attention.

    You said you already tried Me.Controls - what was different?
    I really don't know why me.controls worked while Forms_ didn't. So far adding to this part of the code hasn't produced any issues, so i guess I'll mark it resolved and if something new comes up, i'll create a new thread/ Just thought Admins might prefer not so many new threads being created for similar issues.

    Thanks for the help.

    Ken L

  9. #9
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    You said you tried Me.Controls and it errored. What did you do different so it worked?
    Last edited by June7; 08-21-2023 at 11:21 AM.
    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. #10
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    Quote Originally Posted by June7 View Post
    Now I wonder why I got the habit of using Forms!, because it does not trigger intellisense tips and Form_ does.
    I think you need to be careful using the Form_ syntax, as I have a sneaky suspicion it can instantiate another hidden instance of the form in some circumstances.
    There is quite a long-winded discussion of it here
    https://www.access-programmers.co.uk.../#post-1747913

    And a discussion of some of the issues
    https://www.accessforums.net/showthread.php?t=84803
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

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

Similar Threads

  1. Replies: 43
    Last Post: 08-31-2021, 04:15 PM
  2. Code only works after form is loaded 2nd time
    By joecamel9166 in forum Forms
    Replies: 1
    Last Post: 05-05-2016, 02:12 PM
  3. Replies: 8
    Last Post: 07-03-2015, 05:03 PM
  4. Replies: 2
    Last Post: 12-19-2014, 02:18 PM
  5. Replies: 6
    Last Post: 09-02-2012, 04:30 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