Results 1 to 11 of 11
  1. #1
    rwahdan1978 is offline Advanced Beginner
    Windows 11 Access 2016
    Join Date
    Jun 2024
    Posts
    57

    Update form from another form

    I have a main form that displays an image of one person. Based on that I am clicking a button that will take me to another form. The other form I am doing something else and when done:



    I want to go back to the first form and run the load event again (note: the first form was left open when going to the second form).

    How to do that?

    Code:
         Dim rs2 As Recordset
         If (thecounterAll = 6) Then
            DoCmd.OpenQuery "reset UserChoice"
            thecounterAll = 1
            Set rs2 = CurrentDb.OpenRecordset("randomUser")
            Me.theuserid.Value = rs2!UserID
            Me.thename.Value = rs2!UserName
            Me.Image6.Picture = rs2!Photo
            DoCmd.OpenQuery "updateUserChoice"
            thecounterAll = thecounterAll + 1
            rs2.Close
            Set rs2 = Nothing
        Else
            Set rs2 = CurrentDb.OpenRecordset("randomUser")
            Me.theuserid.Value = rs2!UserID
            Me.thename.Value = rs2!UserName
            Me.Image6.Picture = rs2!Photo
            DoCmd.OpenQuery "updateUserChoice"
            thecounterAll = thecounterAll + 1
            rs2.Close
            Set rs2 = Nothing
        End If
    Here where i move to the other form:
    Code:
    Private Sub ahmad100_Click()
        askedAhmadNum = 1
        DoCmd.OpenForm "form1"
    End Sub
    it is only done once as a page load but I want to reload the page before entering from the other form, here is what i did in the other form:
    Code:
    If (closetheform = -1) Then
            DoCmd.Close acForm, "form1"
            Forms![Main].Requery
    End If
    where the first form is called Main. Requery is not acting as a reload, I also did Refresh as well no lock!

  2. #2
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows 10 Access 2021
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,614
    You could open the second form as Dialog. The code would stop running when the second form opens and would resume when the second form closes. Then call the first forms load event:-
    Code:
    Private Sub ahmad100_Click()
        askedAhmadNum = 1
        DoCmd.OpenForm "form1" , , , , , acDialog 
        Call Form_Load
    End Sub
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  3. #3
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,556
    If you want to run the load event of the first form again, why not just close it when you go to the second form and then open it when you close the second 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

  4. #4
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    I don't see how a reload is going to help if a requery doesn't.
    What is the difference between the If and the Else? It just looks like a repeat to me.
    Shouldn't matter, but once the choice is made doesn't it make more sense to requery the main form then close form1 rather than close and hope the requery line still executes?
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  5. #5
    rwahdan1978 is offline Advanced Beginner
    Windows 11 Access 2016
    Join Date
    Jun 2024
    Posts
    57
    Quote Originally Posted by Micron View Post
    I don't see how a reload is going to help if a requery doesn't.
    What is the difference between the If and the Else? It just looks like a repeat to me.
    Shouldn't matter, but once the choice is made doesn't it make more sense to requery the main form then close form1 rather than close and hope the requery line still executes?
    There is a difference as the counter is less than 6.

  6. #6
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    What I'm saying is, form load event loads records from the form recordsource. A requery does that too. If what you show in that code is the load event, then you should make that clear. For all I know, it's something else. Either way, it looks like you load the same recordset, so no need to type that out 2x.

    If you want to run a control, form or report event from some other form, usually all you have to do is make it Public, and not Private, which it is by default. I can't recall the exact syntax of the call. I think it follows the form name as shown in the vbe project pane, like form_frmMain (or whatever the form name is). I believe you must leave off the () at the end of the procedure name.

    EDIT - BTW, if counter is 6 and you reset to 1, do you immediately want it to become 2? That's what you'll be doing.
    Last edited by Micron; 07-24-2024 at 12:56 PM. Reason: clarification
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  7. #7
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,556
    Any time you repeat code, there is normally a better clearer way.
    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
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,879
    There is a difference as the counter is less than 6.
    What's the difference?

    Another slightly more complicated method and probably overkill is to use custom events.
    All the code below goes in the main form and the events are handled in the main forms module.

    Code:
    Dim WithEvents MyForm as access.form   ' this goes in the header of form Main
    
    Private Sub ahmad100_Click()
     
         DoCmd.OpenForm "form1"   'open the form
    
         Set MyForm = Forms("form1")  'create an instance
    
         MyForm.OnClose = "[Event Procedure]"  'ensure an event.  You can use most form events here instead of OnClose
    
    End Sub
    
    Private Sub MyForm_Close()
    
         'do what you want here when form1 closes
    
         Me.Requery
    
         Set MyForm = Nothing  ' destroy instance
    
    End sub
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  9. #9
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    What's the difference?
    A query is opened: DoCmd.OpenQuery "reset UserChoice" and the counter becomes 2 instead of just incrementing. That doesn't make it necessary to write the rest of it 2x.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  10. #10
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,879
    Quote Originally Posted by Micron View Post
    A query is opened: DoCmd.OpenQuery "reset UserChoice" and the counter becomes 2 instead of just incrementing. That doesn't make it necessary to write the rest of it 2x.
    Re-formatted and now I see it.

    Code:
          Dim rs2 As Recordset    
    
         Set rs2 = CurrentDb.OpenRecordset("randomUser")
         
        If (thecounterAll = 6) Then
         
            DoCmd.OpenQuery "reset UserChoice"
            
            thecounterAll = 1
                    
        End If
         
        DoCmd.OpenQuery "updateUserChoice"
        
         thecounterAll = thecounterAll + 1
        
        Me.theuserid.value = rs2!UserId
        Me.thename.value = rs2!UserName
        Me.Image6.Picture = rs2!Photo
    
    
        rs2.Close
        Set rs2 = Nothing
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  11. #11
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,879
    As far as returning to the main form, are you trying to do something like this?

    Open the form1, enter some info, and then return to the same record on form main with the new info showing?
    Attached Files Attached Files
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

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

Similar Threads

  1. Replies: 1
    Last Post: 02-27-2018, 11:39 PM
  2. Replies: 4
    Last Post: 09-05-2017, 04:25 AM
  3. Replies: 8
    Last Post: 03-20-2017, 07:22 PM
  4. Replies: 7
    Last Post: 07-19-2016, 05:28 PM
  5. Replies: 7
    Last Post: 09-21-2012, 11:09 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