How to make the Forms.Move method work?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Petrol
    New Member
    • Oct 2016
    • 248

    How to make the Forms.Move method work?

    I have a form which, if an error condition is encountered, is to produce a MsgBox and open a subform giving further details. I want the subform to be positioned just below the message box. However I can't get my Form.Move method to work.

    My code is below, the critical bits being in Bold:
    Code:
    Private Sub Surname_AfterUpdate()
    
    '  Routine to check whether new person (or someone else with the same name) is already on file
    
    Dim Name_FnSn As String, Message As String, Response As Integer
    'On Error GoTo ErrorHandler
    
    Name_FnSn = Me.First_name & " " & Me.Surname
    
    If DCount("[Name_FN-SN]", "People", "[Name_FN-SN]='" & Name_FnSn & "'") > 0 Then
       ' Person of this name is already on file.  Display their address details so user can decide if it's the same person.
    [B]  [B] DoCmd.OpenForm "subfrm_ContactInfo", acNormal, , "[Name_FN-SN]='" & Name_FnSn & "'", acFormReadOnly, acDialog[/B]
      [B] Forms.subfrm_ContactInfo.Move 1000, 4000[/B]
       [B]Message = "We already have a person named " & Name_FnSn & " on file.  See details below.  Is this the same person?"[/B]
      [B] Response = MsgBox(Message, vbYesNo + vbQuestion + vbDefaultButton2)[/B][/B]
       If Response = vbYes Then                                '   User says it is the same person as already on file
          Me.Undo                                              '   So scrap the new entry, clean up and go home.
          DoCmd.Close acForm, "subfrm_ContactInfo", acSaveNo
          DoCmd.Close acForm, "Form_11: Enter new Applicant", acSaveNo
          GoTo ExitSub
       Else
          ' Not the same person, just someone with the same name, so clean up and carry on with the new record entry
          DoCmd.Close acForm, "subfrm_ContactInfo", acSaveNo
       End If
    Else
    '  We didn't find a duplicate, so we can carry on with the new record entry
    End If
    
    
    ExitSub:
       Exit Sub
       
    ErrorHandler:
       MsgBox "Error " & Err.Number & ": " & Err.Description
       
       End Sub
    In practice the subform opens but only at the top left of the screen. I have tried various variants - omitting the Forms. qualifier, changing the 1000,4000 measurements to Left:=1000, Top:=4000, making the subform mode acDSialog (this makes it worse: not only does the form not move, but because the subform is now modal the MsgBox doesn't even display) and depending on the variant I either get Error 438 (Object doesn't support this property or method) or Error 2465 (I think) or some other error, or it simply doesn't work.

    Has anyone figured out how to make Form.Move work successfully?
  • PhilOfWalton
    Recognized Expert Top Contributor
    • Mar 2016
    • 1430

    #2
    Three thoughts
    1) It would appear that you are opening a form (not a subform), so references to it should be something like

    Code:
    Dim frm as form
    
    Set frm = "subfrm_ContactInfo"
    Frm.movesize Right, Down, Width, Heigh
    Note use DoCmd.MoveSize.

    If you really do want to use a subform (as part of the main form) change it's visible property from false to true to show it, and back to false to hide it.

    My preference would be to show all the relevant information (currently on your "Subform??? ??") in the message box.

    I actually use a rich text version of a message box which allows different fonts & colours to emphasise information.

    Phil

    Comment

    • Petrol
      New Member
      • Oct 2016
      • 248

      #3
      Thank you, Phil. However I ran into trouble on the statement "Set Frm = "Subfrm_Contact Info"; it bombed with Compile Error: Type mismatch.
      I see you have declared Frm as Form. I've seen this done before, but when I tried to read up on what type of data it wanted I couldn't find any documentation on it. It's not listed in the Dim documentation, for example, not in the Data Tyep Summary. Do you know where I can read up about it?

      And in any case, how would I get around the type mismatch?

      Finally, can you clarify how to use the MoveSize statement? In the example you used Frm.MoveSize, then below you suggested using DoCmd.MoveSize. I'm not expert enough to figure out how to resolve this. Perhaps it's DoCmd.Frm.MoveS ize?

      Comment

      • PhilOfWalton
        Recognized Expert Top Contributor
        • Mar 2016
        • 1430

        #4
        Sorry my mistake

        Try
        Code:
        Set frm = Forms("subfrm_ContactInfo")
        I suggest you read the help file on Movesize. Basically it needs 4 numbers - Right, Down, Width & Height

        Measurements are in Twips (1440 to the inch, 567 to the Cm)

        So
        Code:
        Frm.movesize 1000, 4000
        would move the form down about 2cms, right about 7cms FROM THE TOP LEFT of the Access Window.

        If you want to position it relative to your main form, then you need to add the main form's left & top positions to those numbers (Also in Twips)

        I still would prefer the msgBox option.

        Phil

        Comment

        • Petrol
          New Member
          • Oct 2016
          • 248

          #5
          Getting closer. It now accepts the Set statement (Thank you!) but bombs on the Frm.MoveSize statement, with the enigmatic message "Run-time error 2465: Application-defined or object-defined error". I've googled the error number but most entries seem to be about another message with the same error number.

          Incidentally the reasons I use a subform rather than putting it all in the diagnostic message are
          (1) I want to display all the contact info - title, name, address (from 4 fields), several phone numbers, email address etc - too much for a standard message box
          (2) I have the relevant form, and the query it rests upon, already written; and
          (3) the users will be familiar with it because it's used (as a genuine subform!) in another place. (That's why I call it a subform, though as you say, in this instance it's being used as a form. I think the difference is more in how it's used than in anything intrinsic to the object).

          Any idea why I get the error 2465?

          Comment

          • PhilOfWalton
            Recognized Expert Top Contributor
            • Mar 2016
            • 1430

            #6
            Sorry, must be getting senile.

            Try

            Code:
            Dim frm as form
             
            Set frm = "subfrm_ContactInfo"
            DoCmd.SelectObject acForm, frm.Name
            DoCmd.MoveSize 1000, 4000
            Phil

            Comment

            • Petrol
              New Member
              • Oct 2016
              • 248

              #7
              The last part of line 6 seems to have been blocked out. Can you give it to me as text?

              So it's now DoCmd.MoveSize?

              Comment

              • PhilOfWalton
                Recognized Expert Top Contributor
                • Mar 2016
                • 1430

                #8
                DoCmd.MoveSize 1000, 4000

                That should be OK.

                It means move the form, but don't alter the size as the last 2 figures (Width & Height) aren't specified.

                What problem are you getting?

                Phil

                Comment

                • Petrol
                  New Member
                  • Oct 2016
                  • 248

                  #9
                  Sorry, I gave you the wrong line number. The problem is that I can't read the last part of line 5. On my screen I just get
                  DoCmd.SelectObj ect acForm, frm.Name
                  and then a block which seems to obscure the rest of the line (if any).

                  Comment

                  • PhilOfWalton
                    Recognized Expert Top Contributor
                    • Mar 2016
                    • 1430

                    #10
                    That is correct

                    DoCmd.SelectObj ect acForm, frm.Name

                    That just ensures that you move the correct form ("subfrm_Contac tInfo") not your main form

                    Phil

                    Comment

                    • Petrol
                      New Member
                      • Oct 2016
                      • 248

                      #11
                      Ah, I see. Thanks.
                      Well, everything now runs without error, but it still doesn't move the subform! I increased the twips measurements to make sure I was putting it in a visible part of the screen, but to no avail.

                      The code is now
                      Code:
                      Private Sub Surname_AfterUpdate()
                      
                      '  Routine to check whether new person (or someone else with the same name) is already on file
                      
                      Dim Name_FnSn As String, Frm As Form, Message As String, Response As Integer
                      'On Error GoTo ErrorHandler
                      
                      Name_FnSn = Me.First_name & " " & Me.Surname
                      
                      If DCount("[Name_FN-SN]", "People", "[Name_FN-SN]='" & Name_FnSn & "'") > 0 Then
                         ' Person of this name is already on file.  Display their address details so user can decide if it's the same person.
                         DoCmd.OpenForm "subfrm_ContactInfo", acNormal, , "[Name_FN-SN]='" & Name_FnSn & "'", acFormReadOnly
                         Set Frm = Forms("Subfrm_ContactInfo")
                         DoCmd.SelectObject acForm, "Subfrm_ContactInfo"
                         DoCmd.MoveSize 8000, 10000
                         Message = "We already have a person named " & Name_FnSn & " on file.  See details above.  Is this the same person?"
                         Response = MsgBox(Message, vbYesNo + vbQuestion + vbDefaultButton2)
                         If Response = vbYes Then                                '   User says it is the same person as already on file
                            Me.Undo                                              '   So scrap the new entry, clean up and go home.
                            DoCmd.Close acForm, "subfrm_ContactInfo", acSaveNo
                            DoCmd.Close acForm, "Form_11: Enter new Applicant", acSaveNo
                            GoTo ExitSub
                         Else
                            ' Not the same person, just someone with the same name, so clean up and carry on with the new record entry
                            DoCmd.Close acForm, "subfrm_ContactInfo", acSaveNo
                         End If
                      Else
                      '  We didn't find a duplicate, so we can carry on with the new record entry
                      End If
                      
                      
                      ExitSub:
                         Exit Sub
                         
                      ErrorHandler:
                         MsgBox "Error " & Err.Number & ": " & Err.Description
                         
                         End Sub
                      Unfortunately it's now 12:30am here, and I have to get up at 6am, so I'll have to leave it for now. I appreciate all your patience and perseverance. I'll have another go at it tomorrow (er, I mean later today!)

                      Comment

                      • PhilOfWalton
                        Recognized Expert Top Contributor
                        • Mar 2016
                        • 1430

                        #12
                        Hope you slept well

                        If you comment out the DoCmd.MoveSize statement does anything different happen?

                        Put a breakpoint on the line
                        Message = "We already have a person named " & Name_FnSn & " on file. See details above. Is this the same person?"

                        now type
                        Code:
                        DoCmd.MoveSize 8000, 10000
                        in the immediate wondow.

                        What happens

                        This certainly works for me, though the 10000 value puts it low on the screen.

                        Phil

                        Comment

                        • Petrol
                          New Member
                          • Oct 2016
                          • 248

                          #13
                          At last I can get back to this briefly. I love working on the project, but the rest of life sometimes intrudes ...


                          Yes I tried the MoveSize command in the immediate window. It took not the slightest bit of notice. I also tried making the form Popup, making the form Modal, making it open in Print Preview, making it open in acFormEdit mode, replacing it with a dummy form, using different values for Right and Down, setting the Width and Height parameters ... nothing made any difference. The subform doesn't blink an eyelid. Very strange.

                          I guess I can always leave it in the top left corner. It conveys the message, it just looks a bit silly up there ...

                          Comment

                          • jforbes
                            Recognized Expert Top Contributor
                            • Aug 2014
                            • 1107

                            #14
                            Are just trying to get the Form to show up closer to Access instead of off by itself? If so, try setting the Dialog Form's AutoCenter Property to True, AutoResize to True, and FitToScreen to False.

                            Comment

                            • Petrol
                              New Member
                              • Oct 2016
                              • 248

                              #15
                              That seems to have done the trick. Thank you very much, J... . I wasn't actually just trying to get it to show up closer to Access (see my original post), but by making the changes you mentioned the MoveSize command now works and I can position it where I want it.

                              I now have another quandary. It wouldn't have worked without the corrections to my code that Phil gave, but I also needed your FitToScreen suggestion. But I can only mark one "Best answer"! Thank you both for your willingness to help a learner learn.

                              I would hope one day to be able to help others the way you experts do ... but it looks like I'm a fair way off as yet!

                              Comment

                              Working...