Handling error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bluemoon9
    New Member
    • Oct 2008
    • 56

    Handling error

    Hi all,
    I have a listbox which lists all the accounts, and I would like to handle error when user did not select any account in the list box but then click on "view/edit" button. The following is my code. However, I keep on having the blank account form open on the top of the error message. Any idea? thanks

    Private Sub cmdEditAcct_Cli ck()
    Dim Title As String
    Dim Msg As String

    On Error GoTo Err_cmdEditAcct _Click

    DoCmd.RunMacro "mcrEditAcc t" 'open form
    [Forms]![fdlgEditAccount]![txtUsername] = [Forms]![frmEdit]![txtUsername]
    Exit_cmdEditAcc t_Click:
    Exit Sub

    Err_cmdEditAcct _Click:
    Select Case Err
    Case 2448 'No account selected
    Title = "Select Account"
    Msg = "You must first select an Account to make correction."
    Msg = Msg & " Please click on an Account in the All Accounts listbox."
    Beep
    MsgBox Msg, vbExclamation, Title
    Resume Exit_cmdEditAcc t_Click
    Case Else
    MsgBox Error$
    Resume Exit_cmdEditAcc t_Click
    End Select
    End sub
  • missinglinq
    Recognized Expert Specialist
    • Nov 2006
    • 3533

    #2
    Error Code 2448 is an Access defined error: "You cannot assign a value to this
    object." You cannot simply use it to mean whatever you want! Any error handled in this manner has to be an Access defined error, and there is no Access defined error for something like this! You have to use some custom code, something like this, replacing AllAccountsList box with the actual name of your listbox:
    Code:
    Private Sub cmdEditAcct_Click()
    Dim Title As String
    Dim Msg As String
    
    If IsNull(Me.AllAccountsListbox) Then
      Title = "Select Account"
      Msg = "You must first select an Account to make correction."
      Msg = Msg & " Please click on an Account in the All Accounts listbox."
      Beep
      MsgBox Msg, vbExclamation, Title
      Me.AllAccountsListbox.SetFocus
    Else
     DoCmd.RunMacro "mcrEditAcct" 'open form
     [Forms]![fdlgEditAccount]![txtUsername] = [Forms]![frmEdit]![txtUsername]
    End If
    End sub
    Linq ;0)>

    Comment

    • bluemoon9
      New Member
      • Oct 2008
      • 56

      #3
      Hi Ling,
      I've tried your code, but it still open the form even though I did not choose anything in the listbox.

      Any help?
      thanks!

      Comment

      • bluemoon9
        New Member
        • Oct 2008
        • 56

        #4
        In addition, it open a blank form with the error 2448.
        thanks!

        Comment

        • missinglinq
          Recognized Expert Specialist
          • Nov 2006
          • 3533

          #5
          If you replaced AllAccountsList box in the code I gave you with the actual name of your listbox,

          DoCmd.RunMacro "mcrEditAcc t"

          should not run if no selection has been made from the listbox.

          You need to post the exact code you're currently using.

          Linq ;0)>

          P.S. You do have the Multi-select Property of the Listbox set to No, don't you? It has to be for this kind of thing.

          Comment

          • bluemoon9
            New Member
            • Oct 2008
            • 56

            #6
            Yes, I did. I did put the lstPtAccount list box. I also set the multi selection to No and it did not work.
            My form is like this, when a user select a cutomer ID from the cboCustomer, it will show all the accounts in the listbox under the selected customer. So eveytime the user selects a new customerID from the customer combo box, it requery the listbox. Would this make the listbox to be "NotNull" all the time since the user might have selected an account in the list box for the 1st selected customer? So when it comes to the second customer, even though nothing has been selected in the listbox, the listbox will also "is not null"?

            thanks!

            Elaine

            Comment

            Working...