Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    Middlemarch is offline Competent Performer
    Windows 10 Access 2019
    Join Date
    Mar 2015
    Posts
    348

    Cannot find referenced Form error

    From Googling I'm told Forms![MyForm].Recordsource = strSQL is how to set a forms recordsource. But it doesn't work for me!




    I've tried each of the following without success.
    Code:
    Forms![frmShow].RecordSource = strSQL
    Forms!["frmShow"].RecordSource = strSQL
    Forms!["frmShow"].Form.RecordSource = strSQL
    If I don't include .Form. there is no .Recordsource dropdown. Does that matter ?What am I doing wrong ? frmShow does exist.

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    Is the form open?

    The first should work. The other two are not correct syntax because of quote marks.
    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
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,793
    "doesn't work" means what? Without knowing anything about the db, here's 2 stabs
    - your form is a subform
    - sql variable has no value; i.e it's "" aka zero length string

    I would also have listed "form is not open" but that's been covered.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  4. #4
    Middlemarch is offline Competent Performer
    Windows 10 Access 2019
    Join Date
    Mar 2015
    Posts
    348
    The Form is not open and is not a SubForm. The sql variable is correct/complete.
    "doesn't work" means Access is giving the error message used as the Subject.
    The full wording is Run-time error '2450':Microsoft Access cannot find the referenced form 'frmShow'.
    Does the Form have to be Open for Access to know it Exists? Surely not ?
    I'll try it in a new db with nothing but the one Form

  5. #5
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    The form must be open in some mode to change its properties.
    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
    Middlemarch is offline Competent Performer
    Windows 10 Access 2019
    Join Date
    Mar 2015
    Posts
    348
    @June. OK, but how can you open it if Access can't find it.
    I'll attach the rogue db that can't see what it has. And (sigh) fully expecting it will work for you.
    Attached Files Attached Files

  7. #7
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    Did you try opening form manually from Navigation Pane? What happens? Should see an error message because form is bound to ShowUnique which doesn't exist as a table nor a query. This is why the VBA fails. Textboxes are bound to fields that don't exist.

    Why do you want to programmatically change form RecordSource?
    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
    Middlemarch is offline Competent Performer
    Windows 10 Access 2019
    Join Date
    Mar 2015
    Posts
    348
    @June7, I had removed the ShowUnique. Access indicated it couldn't find the Form, not that there was something wrong with it. At one stage I got the same error when trying to Open it.
    I want to change the recordsource to test a theory. At the moment QueryDefs are used to make a query abd I want to see if that is really needed, i.e. can the Form use just sql.

  9. #9
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    Then how do you expect me test for the behavior you experience?

    I created a table ShowUnique and form opens just fine.

    Still haven't explained why you want to do this. Why do you use QueryDefs to make a query? What are you really trying to accomplish?
    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
    Middlemarch is offline Competent Performer
    Windows 10 Access 2019
    Join Date
    Mar 2015
    Posts
    348
    I didn't know it would error with 'can't find Form" because the recordsource didn't exist. To me, that did not seem a correct error as the Form did exist. I was wanting to establish a new Record source so the current one wasn't relevant.
    But this is interesting, your question Why do you use QueryDefs to make a query puzzles me and I wonder why would not ? So here's my code and perhaps you can show how you would do it ?
    frmDisco's recordsource is qryDisco. This is built at runtime, from the data active at the time. The sql will appear gibberish but that shouldn't matter
    Code:
    Dim myquery As QueryDef  
    qryName = "qryDisco"
        sql = "Select A, B, HandleThis(C) as iC, D from " & MyCompany & " Where " & ActiveBtn & " Like " & Chr$(34) & Dat & Chr$(34) & ";"
        Set myquery = db.QueryDefs(qryName)
        myquery.sql = sql$
    
    
      DoCmd.OpenForm "frmDisco", acFormDS

  11. #11
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,914
    No Option Explicit I see?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  12. #12
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    Certainly can change a form's RecordSource without modifying a query object. I've only had one occasion where I needed to use QueryDefs to modify query object and had nothing to do with setting a form's RecordSource. But as already noted, the form must be open in some mode.

    I suspect that because code dynamically sets the source table, your data structure may not be optimized.


    Learned something new. In posted db, procedure has Dim sql$ but then uses sql. I added Option Explicit line and VBA accepts these variables. However, when I reverse the variables, VBA does not like it.

    Agree, really should have Option Explicit in every module. Review https://excelchamps.com/vba/option-e...0new%20module.
    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.

  13. #13
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,793
    The $ in Dim Sql$ replaces "As String". Personally I don't use/like that syntax. AFAIK, if you use Dim Sql$ you must use Sql$ = something, not Sql =
    OP apparently got away with that because of not having Option Explicit. In that case Sql$ would exist but Sql would be what is assigned a value where Sql = is used.
    Late to the party so I guess I won't upload db sample. As far as I'm concerned, the error is not about whether or not the form exists in the forms collection - it is about whether or not vba can "see" it, which it cannot if it's not open either in design or form view.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  14. #14
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    As I stated, I added Option Explicit and the sql reference is accepted.
    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.

  15. #15
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,793
    Correct. I should have 'listened' to myself
    Dim sql$ is the same as Dim sql As String
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. VBA Unable to find referenced form
    By mdev in forum Programming
    Replies: 5
    Last Post: 03-02-2018, 10:28 AM
  2. Replies: 9
    Last Post: 12-08-2017, 02:21 PM
  3. Replies: 8
    Last Post: 06-30-2016, 02:13 PM
  4. Can't find referenced field error
    By Thompyt in forum Programming
    Replies: 7
    Last Post: 09-04-2015, 09:00 AM
  5. Replies: 1
    Last Post: 03-05-2014, 07:55 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