Results 1 to 11 of 11
  1. #1
    blueraincoat is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Mar 2011
    Location
    Shanghai,CHN
    Posts
    99

    Use MsgBox to open different forms

    Hi All, I have two forms for user to search for some information in different ways, and i made a form using MsgBox to let user to select which way is wanted. The code is as below

    Private Sub Form_Open(Cancel As Integer)
    Dim intSelection As Integer


    intSelection = MsgBox("Click Yes To View Parts Information By PN,Click No To View By MfgNumber.", vbYesNo, "Please Respond")
    Select Case intSelection
    Case vbYes
    DoCmd.OpenForm "frmPartsInfoByPN"
    Case vbNo
    DoCmd.OpenForm "frmPartsInfoByMfg"
    End Select
    Cancel = True
    End Sub

    At first I missed "Cancel As Integer" and "Cancel = True", and after several tries it works, but still i didn't understand it very well.
    Could somebody explain it for me?
    Thanks in advance.

  2. #2
    Buakaw is offline Absolute novice
    Windows XP Access 2007
    Join Date
    Jan 2011
    Posts
    112
    I don't quite understand your question but I'll try anyway.

    I'm new at this as well, but I don't think Cancel=True is going to do anything because this value is passed in by value, not by reference, so once the Sub exits, the value is going to be garbage anyway.

    If you're trying to modify the variable passed in as Cancel and you want the change to remain when you exit this Sub, you need to specify the Sub as

    Private Sub Form_Open(ByRef Cancel As Integer)

    I hope that's what you're asking?

  3. #3
    blueraincoat is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Mar 2011
    Location
    Shanghai,CHN
    Posts
    99
    Thank you Buakaw.
    My intention is when i open a form let's say FormC, it will give you an option to select which form(FormA or FormB) to open,and after the user makes selection the FormC will be closed. At first i missed the "Cancel as Integer" and "Cancel = True", and it did not work. After adding that two sentences it works as i want. I do not understand that 2 sentences.
    I'm a new bie also, i was reading a book, and most of the code was from that book i just modified a little.

  4. #4
    thhui is offline Competent Performer
    Windows XP Access 2002
    Join Date
    Feb 2009
    Posts
    235
    Adding the code
    Cancel = true

    enables cancelling your options selection by pressing escape-key before you choose the action.

  5. #5
    blueraincoat is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Mar 2011
    Location
    Shanghai,CHN
    Posts
    99
    Thank you thhui.
    I once tried to add DoCmd.Close in both case,and it did not work
    and cancel = true seems to be the solution.

  6. #6
    ansentry's Avatar
    ansentry is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    May 2009
    Location
    Melbourne, Australia
    Posts
    67
    Have a look at the attached sample, I think this is what you may be trying to do.

  7. #7
    blueraincoat is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Mar 2011
    Location
    Shanghai,CHN
    Posts
    99
    Thank you ansentry
    Before your suggestion my code was DoCmd.Close acForm, FrmMain and this does not work.
    your code DoCmd.Close acForm, Me.Name works fine.
    It seems i need to work from the basis.

  8. #8
    ansentry's Avatar
    ansentry is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    May 2009
    Location
    Melbourne, Australia
    Posts
    67
    Code:
    DoCmd.Close acForm, FrmMain
    Should be:

    Code:
    DoCmd.Close acForm, "FrmMain"
    Hope this explains why it did not work.

  9. #9
    blueraincoat is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Mar 2011
    Location
    Shanghai,CHN
    Posts
    99
    Thank You ansentry, I see why that code does not work.

  10. #10
    ansentry's Avatar
    ansentry is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    May 2009
    Location
    Melbourne, Australia
    Posts
    67
    The other problem with your original code was that you did not have a button for the user if they canceled the operation (did not want to use either of your choices "Yes - No".

    With my code I have added the extra command button "Cancel", when you add the extra button then you must also write code to handle that Select Case "vbCancel"

    Good luck with your project.

  11. #11
    blueraincoat is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Mar 2011
    Location
    Shanghai,CHN
    Posts
    99
    yes i noticed that.
    Thank you.

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

Similar Threads

  1. MsgBox Placement
    By tpcervelo in forum Programming
    Replies: 2
    Last Post: 12-27-2010, 07:36 AM
  2. Command buttons to Open Forms
    By Ace Pioneer in forum Access
    Replies: 1
    Last Post: 11-30-2010, 06:32 AM
  3. Replies: 1
    Last Post: 09-15-2010, 07:34 PM
  4. Open Multiple forms in runtime access
    By Tifa in forum Access
    Replies: 4
    Last Post: 08-20-2010, 06:47 AM
  5. too many forms open
    By Jaad in forum Database Design
    Replies: 0
    Last Post: 02-22-2010, 08:39 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