Results 1 to 10 of 10
  1. #1
    Join Date
    Feb 2009
    Posts
    37

    Unanswered: suppress error msg or adjusting script

    Hi all,

    I have a form (Sightings) with a listbox (lstBehaviour) on it.
    At different values different forms should be opened but at some values no form should be opened.

    For example the value 'Hunting' should open the form 'Hunting' and the value 'Social Interaction' should open the form 'Social Interaction' but with the value 'Resting' nothing should happen.

    I started with this code:
    Code:
    public sub MyComboBox_AfterUpdate()
    docmd.openform MyComboBox.Value
    End Sub
    This works for 'Hunting' and 'Social Interaction' because the name in the listbox is the same as the name of the form.
    But this gives an error message for values which don't correspond to a form.
    This is error message 2102.

    How to best solve this and how?

    Suppress the error message? A bit dirty I guess. I've looked around but I can't figure out how to do that.

    Adjust the code (or use a new code) so it only works with specified values? I have no idea how to do that...

    Any help/advice from you guys (and girls)?

  2. #2
    Join Date
    May 2005
    Posts
    1,191
    Quote Originally Posted by VB noob
    Code:
    public sub MyComboBox_AfterUpdate()
    docmd.openform MyComboBox.Value
    End Sub
    Just use some error handling, a-like-so:
    Code:
    public sub MyComboBox_AfterUpdate()
    
        on error goto ErrorHandler
    
        docmd.openform MyComboBox.Value
    
    ExitProc:
        Exit Sub
    
    ErrorHandler:
        on error resume next
        if error.number = 2102 then
            msgbox "That selection doesn't have a form.", _
                        vbexclamation, _
                        "No Matching Form"
        else
            msgbox error.number & vbnewline & _
                            error.description & vbnewline & vbnewline & _
                            "Aborting operation.", _
                        vbcritical, _
                        "Cricital Error " & error.number
        end if
        goto ExitProc
    
    End Sub
    HTH!

    EDIT: Had a dsyletcic moment and typed wrong error code number.
    Last edited by nckdryr; 03-11-09 at 16:41.
    Me.Geek = True

  3. #3
    Join Date
    Feb 2009
    Posts
    37
    Thanks Nick!
    I've put your code in and it seems to work for error message 2102 but now I get error message 424 (object required).
    Can I kill that one as well or is it wiser to change my code so it only opens specified values?

  4. #4
    Join Date
    Feb 2009
    Posts
    37
    I did some more searching and came up with this code which works fine

    Code:
    Public Sub LstBehaviour_AfterUpdate()
    If Me.lstBehaviour.Value = "Hunting" Then
    DoCmd.OpenForm "Hunting"
    ElseIf Me.lstBehaviour.Value = "Social Interaction" Then
    DoCmd.OpenForm "Social Interaction"
    End If
    End Sub

  5. #5
    Join Date
    Mar 2009
    Posts
    5,442
    Provided Answers: 14
    The best way to solve this would be to add a column (it can be hidden) in the listbox, so instead of having your listbox fetching data like this:

    Code:
    SELECT FormName FROM FormNameTable ...
    You would have:

    Code:
    SELECT Open, FormName FROM FormNameTable
    with Open being a boolean (True or False)

    Now you can write:
    Code:
    Public Sub LstBehaviour_AfterUpdate()
    If Me.lstBehaviour.Value = True Then ' LstBehaviour now returns True or False
    DoCmd.OpenForm Me.lstBehaviour.Column(1).Value 
    End If
    End Sub
    See: The Access Web - Welcome

    Its about comboboxes but it also works with listboxes.

    Have a nice day!

  6. #6
    Join Date
    Feb 2009
    Posts
    37
    Thanks, I already read it's not wise to use nested if statements.
    However your code not totally works and I'm not sure why not.

    I added a second column to the listbox with 0's and 1's.

    The code opens a form for the field with a '1'.
    The first value in the listbox has a '0' and it correctly doesn't open a form and there is no error message.
    However...for all the other values with a '0' it gives the error message 2102 again

  7. #7
    Join Date
    Feb 2004
    Location
    One Flump in One Place
    Posts
    14,912
    Quote Originally Posted by VB noob
    Thanks, I already read it's not wise to use nested if statements.
    It's also not wise to make generalisations Nested If statements are perfectly fine when appropriate.
    Testimonial:
    pootle flump
    ur codings are working excelent.

  8. #8
    Join Date
    Mar 2009
    Posts
    5,442
    Provided Answers: 14
    Oops! Sorry .Value is incorrect, simply use:
    Code:
    DoCmd.OpenForm Me.lstBehaviour.Column(1)
    Have a nice day!
    Attached Files Attached Files

  9. #9
    Join Date
    Feb 2009
    Posts
    37
    I already corrected for that.
    My problem was that I was using a typed list instead of a table.
    Made a table now because it's not unlikely more forms will be added which have to be opened through this control.
    And after doing that I found out it didn't work with a list box. It opens the forms alright but selecting 'Social Interaction' for instance opens the right form, but in the listbox the value jumps back to 'Hunting' (the first value in the list to open a form). The same for the values who don't open a form. They don't open a form but the values jump back to the first value who shouldn't open a form.

    So I now created a combo box and that works fine.

  10. #10
    Join Date
    Mar 2009
    Posts
    5,442
    Provided Answers: 14
    This is strange because it works with a list here. Did you try the form Main of the sample file I posted?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •