Results 1 to 13 of 13
  1. #1
    lawdy is offline Competent Performer
    Windows 7 32bit Access 2013
    Join Date
    Jan 2013
    Posts
    216

    How to use refresh in a module


    It is real common to use me.refresh in a sub of the form. I need to use refresh in function. Instructions says to use form.refresh. But when I put formName.refresh all I get is an error. Can someone please help me.

  2. #2
    isladogs's Avatar
    isladogs is offline Access MVP / VIP
    Windows 10 Office 365
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    6,204
    Form code uses requery, recalc or refresh to update the display in different ways.

    However, what would you need to refresh in a function?
    Colin Riddington, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I know I don't know, I keep quiet!

  3. #3
    lawdy is offline Competent Performer
    Windows 7 32bit Access 2013
    Join Date
    Jan 2013
    Posts
    216
    To answer your question isladogs, I right click a area on a calendar and then choose a selection from a list. This puts me in a function where some of the fields are emptied. The form then needs to reflect the changes. I hope this answers your question.

  4. #4
    lawdy is offline Competent Performer
    Windows 7 32bit Access 2013
    Join Date
    Jan 2013
    Posts
    216
    isadogs, the function I mentioned above is in a module.

  5. #5
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,552
    I would like to know that as well.

    You would need to refer to the form by it's full name Forms!FormName.Refresh, however that is then tied to one form, so I assuming you are trying to do something for a few form procedures?
    Or pass it it in as an object and use that object reference.

    I only ever use functions when I want to return something, else I would use a Sub, to which you can still pass in the form.
    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

  6. #6
    lawdy is offline Competent Performer
    Windows 7 32bit Access 2013
    Join Date
    Jan 2013
    Posts
    216
    Welshgasman, that worked. I did not know to put Forms! in front of my form name. You mentioned once before about only using functions when a value is returned. Otherwise you use a sub. I write most code on modules. Does it make any difference whether it a sub or a function?

  7. #7
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,552
    TBH, if you can refer to the form by Form!, what is the point of putting it in a sub/function?
    Normally those are used to run a section of code from multiple calls.

    As I understand it, a function is there to return a value. Subs do not, though you can do it by passing in by a certain reference method. I stick to subs for common routines. Functions for common routines that need to return a value.

    Same applies in Excel, where I use a function to get the last row of a sheet and perhaps a particular column. Then I can use that from anywhere, as I would always forget the syntax.

    Code:
    Public Function GetLastRow(pstrSheet As String, Optional pstrColumn As String) As Long
    ' Return last used row for sheet and column passed in
    Dim lngLastRow As Long
    Dim sht As Worksheet
    
    
    Set sht = Sheets(pstrSheet)
    If pstrColumn = "" Then pstrColumn = "A"
    
    
    lngLastRow = sht.Cells(ActiveSheet.Rows.Count, pstrColumn).End(xlUp).Row
    GetLastRow = lngLastRow
    Set sht = Nothing
    
    
    End Function
    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

  8. #8
    lawdy is offline Competent Performer
    Windows 7 32bit Access 2013
    Join Date
    Jan 2013
    Posts
    216
    Thank you Welsjgasman for your help & advice. I gonna try to switch

  9. #9
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,421
    I only ever use functions when I want to return something, else I would use a Sub
    Except that there are places from which you cannot call a sub, so then it has to be a function whether or not you return anything.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  10. #10
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,552
    Quote Originally Posted by Micron View Post
    Except that there are places from which you cannot call a sub, so then it has to be a function whether or not you return anything.
    True. I have had to done that from the standard switchboard, as it will not run a sub. Then that is just a function that runs a sub. I forgot about that as my memory is not great these days.
    Code:
    Function CreateBackupFE()
    ' Have to do it this way as Switchboard does not allow parameters.
    CreateBackup ("FE")
    End Function
    Function CreateBackupBE()
    ' Have to do it this way as Switchboard does not allow parameters.
    CreateBackup ("BE")
    End Function
    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

  11. #11
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,928
    Also subs cannot be called in queries, only functions

  12. #12
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,552
    Quote Originally Posted by CJ_London View Post
    Also subs cannot be called in queries, only functions
    Why would you call a sub?, surely you use the function to get a value in the query?
    It has never occurred to me to even try and call a sub?
    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
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,928
    Not always - yes it returns a value if that is what the function does. The query might do something you would otherwise achieve by looping through a recordset

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

Similar Threads

  1. Replies: 4
    Last Post: 02-09-2015, 10:16 AM
  2. How do I use a MODULE in a Cross-Tab Query?
    By timo1999 in forum Modules
    Replies: 2
    Last Post: 12-13-2014, 04:51 PM
  3. class module vs regular module
    By Madmax in forum Modules
    Replies: 1
    Last Post: 05-01-2012, 03:44 PM
  4. Access 2010 Refresh VS Refresh ALL
    By Snwboarder1982 in forum Access
    Replies: 1
    Last Post: 09-09-2011, 04:07 PM
  5. Replies: 4
    Last Post: 05-16-2011, 04:58 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