Results 1 to 8 of 8
  1. #1
    MidNite is offline Novice
    Windows 10 Access 2002
    Join Date
    Mar 2021
    Posts
    10

    Call a function in instances of a form

    Hi

    This code works perfectly to call a function in open forms:

    Private Sub cmdRandomizeAll_Click()
    On Error Resume Next


    Dim intForm As Integer




    For intForm = 1 To 30
    Call Forms(intForm).Rand
    Next

    End Sub

    It does not work on form that I create on the fly using the New keyword as in the following example:
    Set frmInstance = New [MyForm]
    frmInstance.Visible = True

    How can I call the function in all new instances of the form as well?

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,524
    if the function is in the form, remove it and put it in a MODULE.
    change word PRIVATE to PUBLIC.

    Then all forms can run the function.

  3. #3
    MidNite is offline Novice
    Windows 10 Access 2002
    Join Date
    Mar 2021
    Posts
    10
    Quote Originally Posted by ranman256 View Post
    if the function is in the form, remove it and put it in a MODULE.
    change word PRIVATE to PUBLIC.

    Then all forms can run the function.
    Thank you for responding.

    This is still not working.

    The function in the forms that I call is below. It simply moves to the next record.
    In my earlier post I called the function, "Rand", because the records are sorted randomly.
    So to simplify my explanation of what I am trying to do, for this example, I am calling the function, "MoveToNextRecord"

    Public Function MoveToNextRecord()
    DoCmd.GoToRecord acDataForm, Me.Name, Record:=acNext, Offset:=Val(Me.lstRecordsToMove)
    End Function

    So, taking your suggestion, I placed the entire functionality into a function in a standard module. So I am no longer calling the function, "MoveToNextRecord", in the form.

    'This is the function I created in a standard module:

    Public Sub MoveNextRecord()
    On Error Resume Next


    Dim intForm As Integer


    For intForm = 1 To 30
    Forms(intForm).SetFocus
    DoCmd.GoToRecord acDataForm, Forms(intForm).Name, Record:=acNext, Offset:=Val(Forms(intForm).lstRecordsToMove)
    Next


    End Sub

    This makes all open forms move to the next record, but it does not makes any new instances move to the next record.
    It seems that new instances of the forms are not members of the Forms() collection.
    or may be it has something to do with the DoCmd.GoToRecord command.
    I don't know what to do.

    Thanks again

  4. #4
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,651
    What event are you using to fire the code?

    I would think you'd need something like:
    Code:
    Public Sub MoveNextRecord(frm as form)
    . . . 
    
    DoCmd.GoToRecord acDataForm, frm.Name, ...
     
    . . .
    
    
    and in the form use an event like OnCurrent or OnOpen to call the function

    Code:
    Private Sub Form_Current()
         MoveNextRecord Me
    End Sub
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  5. #5
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,793
    Methinks some code that you think is running is not. A private function in a form module should be available to all instances of that form. Step through your code and make sure it's doing what you think it is, or copy db, compact, zip and post here. Remove all but what is needed to demonstrate your issue if that helps.
    EDIT - best to copy & paste code to eliminate some questions (please use code tags (#) on posting toolbar). Can't tell if you've declared your form variable at the module level or not.
    Also, I've tested now and experience the same thing. However, the built in navigation controls work independently so foregoing custom navigation is probably how I'd fix this.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  6. #6
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,793
    see if this helps. Note the section on 'managing'
    http://allenbrowne.com/ser-35.html

    or this
    https://www.tek-tips.com/viewthread.cfm?qid=1753790
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  7. #7
    apr pillai's Avatar
    apr pillai is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    May 2010
    Location
    Alappuzha, India
    Posts
    209
    When you open a new instance of the same Form in memory, you can only address the new instance with it's Index number like Forms(2).GetfirstName().

  8. #8
    apr pillai's Avatar
    apr pillai is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    May 2010
    Location
    Alappuzha, India
    Posts
    209

    Lightbulb

    Quote Originally Posted by apr pillai View Post
    When you open a new instance of the same Form in memory, you can only address the new instance with it's Index number like Forms(2).GetfirstName().
    Correction to the above statement.

    Code:
    Dim frm as Form
    Docmd.OpenForm "Form1", acNormal
    set frm = Form_Form1
    Code:
    ? Forms(0).Name
    Result: Form1
    
    ? frm.Name
    Result: Form1
    
    Call frm.Rand
    Define a new Form Object with a User-defined Name and attach the new Instance of the Form to that Object. Access the Properties and Methods with the new form object name.
    Last edited by apr pillai; 04-26-2021 at 11:24 AM. Reason: correction to text

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

Similar Threads

  1. Can I do this in a function call itself
    By greatwhite in forum Programming
    Replies: 5
    Last Post: 07-22-2019, 10:59 AM
  2. Listbox Call Function
    By New.User in forum Forms
    Replies: 9
    Last Post: 04-20-2018, 10:23 AM
  3. Call function is not executed
    By fluffyvampirekitten in forum Access
    Replies: 12
    Last Post: 11-04-2015, 10:11 AM
  4. Call A function
    By aspen in forum Programming
    Replies: 10
    Last Post: 03-16-2014, 12:57 PM
  5. Form command button to do something/call function
    By shubhamgandhi in forum Programming
    Replies: 2
    Last Post: 07-27-2011, 04:45 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