Results 1 to 6 of 6
  1. #1
    Sla is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Aug 2014
    Posts
    5

    Closing a form only after a combo box selection is made.

    Hello,


    I'm pretty new to VBA. I have a form with a combo box, in the combo box is a list of other forms you can open. If someone tries to continue without making a selection I have a message box pop up prompting them to make a selection. The problem I'm having is the form closes before the message box pops up, which I don't want to happen. I've tried removing the DoCmd.Close in the code but then the menu doesn't close after a selection is made and that's the only time I want it to close, is after the user has selected a valid field and pressed continue.

    Here's a stripped down version of the code.


    On Error GoTo Err_Command65_Click
    Dim intOKCancel As Integer
    Dim strForm As String

    If Me.combobox= "-------" Then
    strForm = "--------"
    ElseIf Me.
    Me.combobox = "---------" Then
    strForm = "------------"
    ElseIf Me.
    Me.combobox = "-------------" Then
    strForm = "--------------"
    ElseIf Me.
    Me.combobox = "-------------" Then
    strForm = "-----------"
    ElseIf Me.
    Me.combobox = "--------------" Then
    strForm = "--------------"
    ElseIf Me.
    Me.combobox = "--------------" Then
    strForm = "--------------"
    ElseIf Me.
    Me.combobox = "--------------" Then
    strForm = "------------------"
    ElseIf Me.
    Me.combobox = "--------------" Then
    strForm = "--------------"
    ElseIf Me.
    Me.combobox = "---------------" Then
    strForm = "-------------"
    End If


    DoCmd.GoToRecord , , acNewRec
    DoCmd.Close
    DoCmd.OpenForm strForm
    Exit_Command65_Click:
    Exit Sub


    Err_Command65_Click:
    intOKCancel = MsgBox("Please Make Selection", vbOKOnly)
    Resume Exit_Command65_Click:



    I've sat here for nealry 3 hours trying everything and anything I could think of and find from using google. And as I said I'm still learning so I'm absolutely open to any suggestions.

  2. #2
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,722
    You may want to check for the largest string "-------------" first, since the shorter string will be found in the longer.
    It might also help readers if you described what you are trying to do in plain English.
    It looks like some strings being tested are the same values.

  3. #3
    John_G is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    The problem is you are executing these three lines:

    DoCmd.GoToRecord , , acNewRec
    DoCmd.Close
    DoCmd.OpenForm strForm


    before you are checking that strForm has been given a value.

    You need to check that strForm has a valid value right after the Endif.

    e.g.

    If len(strForm) = 0 then
    MsgBox("Please Make Selection", vbOKOnly)
    exit sub
    endif

    HTH

    John

  4. #4
    Sla is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Aug 2014
    Posts
    5
    There are actual words that I replaced with ------------. As this is a database for work that I'm working on and I'm trying to be careful as to how much I put out there. I'm sorry if it makes things more difficult to understand what I'm trying to do. What I want is when someone makes one of the selections from the strings I want that form to open and the menu to close. But if they don't make a selection I want the menu to stay open until they make a selection. I don't know if this helps clear anything up.

    This is an example of the menu that I just made.

    Attachment 17656
    Here is a little bit better of an example of the code that I rewrote to match the example picture

    Fake values but it's the exact code I pulled out. I didn't write this code, someone else did, I'm just trying to tweak it to do the above.

    On Error GoTo Err_Command65_Click
    Dim intOKCancel As Integer
    Dim strForm As String

    If Me.combobox= "FormnameA" Then
    strForm = "FormA"
    ElseIf Me.Me.combobox = "FormnameB" Then
    strForm = "FormB"
    ElseIf Me.Me.combobox = "FormnameC" Then
    strForm = "FormC"
    End If


    DoCmd.GoToRecord , , acNewRec
    DoCmd.Close
    DoCmd.OpenForm strForm
    Exit_Command65_Click:
    Exit Sub


    Err_Command65_Click:
    intOKCancel = MsgBox("Please Make Selection", vbOKOnly)
    Resume Exit_Command65_Click

  5. #5
    Sla is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Aug 2014
    Posts
    5
    Thank you I'll try that.

  6. #6
    Sla is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Aug 2014
    Posts
    5
    I fount that deleting the code lines:

    On Error GoTo Err_Command65_Click
    Dim intOKCancel As Integer
    &
    Err_Command65_Click:
    intOKCancel = MsgBox("Please Make Selection", vbOKOnly)
    Resume Exit_Command65_Click:



    That were previously in there and adding

    If Me!combobox.ListIndex = "-1" ThenMsgBox "A selection is required", vbCritical, "Selection Required"
    Me!combobox.SetFocus
    Exit Sub
    End If

    right at the start has solved the issue I was having.


    Thank you for the replies!

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

Similar Threads

  1. Replies: 10
    Last Post: 01-31-2014, 09:47 AM
  2. Replies: 2
    Last Post: 09-10-2013, 09:10 AM
  3. Replies: 3
    Last Post: 07-03-2013, 10:38 AM
  4. Replies: 2
    Last Post: 04-04-2013, 05:56 PM
  5. Replies: 3
    Last Post: 12-11-2012, 09:12 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