Results 1 to 15 of 15
  1. #1
    lmarconi is offline Advanced Beginner
    Windows 10 Office 365
    Join Date
    Sep 2020
    Location
    Muscat
    Posts
    74

    Error 91 - Why ?

    Good morning all. I have the following issue, it seems very stupid but I'm becoming crazy to understand why I got it:


    In a Private Sub a declare 2 variables as follow :

    Dim dbs As DAO.Database
    Dim tmpl As Recordset


    Then, as part of the program, i have the following script:

    Set tmpl = dbs.OpenRecordset(" SELECT *" & _
    " FROM template-FLOC" & _
    " WHERE [Level] = '4'", dbOpenDynaset)

    Do Until tmpl.EOF
    (etc. etc)
    ........................
    tmpl.MoveNext
    Loop


    Regardless the program is well compiled, it seems the variable tmpl is not set and, during the debug (step by step) when i try to verify the tmpl.Recordset value, i got Run-time Error '91' - Object variable or With block variable not set.

    I really cannot understand why, i don't see any issue in the program but the Set looks like doesn't work.

    Any suggestion from your experience ?

    Thank you,
    Cheers
    Leo

  2. #2
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,929
    Looks to me like you haven’t set dbs to anything. Also better do declare the type - DAO or ADO to avoid confusion

  3. #3
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,550
    there is no need to write code to do this when you can just make a query and open it.
    what are you doing with this loop that a query cant do?

  4. #4
    lmarconi is offline Advanced Beginner
    Windows 10 Office 365
    Join Date
    Sep 2020
    Location
    Muscat
    Posts
    74
    Quote Originally Posted by Ajax View Post
    Looks to me like you haven’t set dbs to anything. Also better do declare the type - DAO or ADO to avoid confusion
    Thanks for the answer. Regarding the set of dbs is set, i just forget to mention :
    Set dbs = CurrentDb()

    as suggested (even if originally i did already, then i change) i change the declaration of the tmpl to:
    dim tmpl as DAO.Recordset

    But same issue.

    Any other suggestion ?
    Cheers

  5. #5
    lmarconi is offline Advanced Beginner
    Windows 10 Office 365
    Join Date
    Sep 2020
    Location
    Muscat
    Posts
    74
    Quote Originally Posted by ranman256 View Post
    there is no need to write code to do this when you can just make a query and open it.
    what are you doing with this loop that a query cant do?
    Thanks for the answer.
    The reason why i didn't use a query is because i use this loop for 5 levels (L3, L4, L4A, L4AA and L5) so i don't want to create 5 different queries and keep it dynamic.

    Cheers,
    L.

  6. #6
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,929
    forget to mention :
    perhaps there is something else you forgot to mention? supply all to code, at least to where you get the error - and indicate which line is causing the error. At the moment the assumption has been it is the set line, but perhaps it is somewhere else

  7. #7
    lmarconi is offline Advanced Beginner
    Windows 10 Office 365
    Join Date
    Sep 2020
    Location
    Muscat
    Posts
    74
    This is the Declaration:

    Click image for larger version. 

Name:	Declaration.JPG 
Views:	16 
Size:	76.6 KB 
ID:	47887

    This is where i count if i have records for each levels:

    Click image for larger version. 

Name:	Count floc.JPG 
Views:	16 
Size:	29.4 KB 
ID:	47888

    And this is what i use for each level (in the script i added only 2 levels), if records is > 0 going trough otherwise exit from IF and goes to next level exactly the same changing only the set of tmpl variable.:

    Click image for larger version. 

Name:	Loop.JPG 
Views:	16 
Size:	124.4 KB 
ID:	47889

    Click image for larger version. 

Name:	Loop-1.JPG 
Views:	16 
Size:	127.6 KB 
ID:	47890

    The program don't stop with error, simple go out from the IF because doesn't select record because the Set not works. When i debug the program step by step and i verify the variable tmpl.recordcount, i get that error.

    Hope is more clear.
    Thank you.
    Cheers

  8. #8
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,553
    Does the sql work in the sql window?
    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

  9. #9
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,929
    some inconsistencies

    in your original post you had tmpl (L) now you have tmp1

    flc_rec is not declared as a DAO.Recordset

    don't see anywhere where you are verifying tmp1.recordcount (your text says tmpl.recordcount anyway)

    what is the benefit of using your dcount function? so still don't know where your error is being generated

  10. #10
    lmarconi is offline Advanced Beginner
    Windows 10 Office 365
    Join Date
    Sep 2020
    Location
    Muscat
    Posts
    74
    The used variable is always "tmpl", where you can see tmp1?
    flc_rec agree, i changed;
    tmpl.recordcount is not in the script, i use it during the debug to check if the records where selected (the only way i know to find the error);
    about the dcount, agree with you, i can test directly in the IF statement;

    However i find the problem: as suggested by Welshgasman, i tested the sql in sql window and it seams didn't work. My guilty, it should be the first to be checked. Changing the sql as below, it seems work works fine now.

    Set tmpl = dbs.OpenRecordset(" SELECT [template-FLOC].*" & _
    " FROM [template-FLOC]" & _
    " WHERE [Level] = '5';", dbOpenDynaset)

    Thank you all for usual and kindly support.
    Cheers.

  11. #11
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,929
    you have screenshot your code rather than copy/pasting so looks like tmp1 to me. But no matter. Reason is you have non alphanumeric characters in the name so you have to surround with square brackets.

  12. #12
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,553
    Looks lke 1 to me as well now from that pic.

    Tell me you have Option Explicit in your modules?
    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

  13. #13
    lmarconi is offline Advanced Beginner
    Windows 10 Office 365
    Join Date
    Sep 2020
    Location
    Muscat
    Posts
    74
    Quote Originally Posted by Welshgasman View Post
    Looks lke 1 to me as well now from that pic.

    Tell me you have Option Explicit in your modules?
    Yes i have it. This is a private Sub used in a Form Button and in the module there is Option Explicit.

  14. #14
    orange's Avatar
    orange is offline Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,870

  15. #15
    lmarconi is offline Advanced Beginner
    Windows 10 Office 365
    Join Date
    Sep 2020
    Location
    Muscat
    Posts
    74
    As mentioned, problem solved in this way:

    Set tmpl = dbs.OpenRecordset(" SELECT [template-FLOC].*" & _
    " FROM [template-FLOC]" & _
    " WHERE [Level] = '5';", dbOpenDynaset)

    Thank you all.
    Cheers.

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

Similar Threads

  1. Replies: 5
    Last Post: 07-15-2019, 10:20 AM
  2. Replies: 1
    Last Post: 09-22-2016, 05:05 AM
  3. Replies: 3
    Last Post: 01-23-2014, 07:49 AM
  4. Replies: 0
    Last Post: 07-16-2012, 05:42 AM
  5. Replies: 6
    Last Post: 05-30-2012, 12:32 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