469,073 Members | 1,791 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,073 developers. It's quick & easy.

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_Click()
Dim Title As String
Dim Msg As String

On Error GoTo Err_cmdEditAcct_Click

DoCmd.RunMacro "mcrEditAcct" 'open form
[Forms]![fdlgEditAccount]![txtUsername] = [Forms]![frmEdit]![txtUsername]
Exit_cmdEditAcct_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_cmdEditAcct_Click
Case Else
MsgBox Error$
Resume Exit_cmdEditAcct_Click
End Select
End sub
Dec 2 '09 #1
5 1664
missinglinq
3,532 Expert 2GB
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 AllAccountsListbox with the actual name of your listbox:
Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdEditAcct_Click()
  2. Dim Title As String
  3. Dim Msg As String
  4.  
  5. If IsNull(Me.AllAccountsListbox) Then
  6.   Title = "Select Account"
  7.   Msg = "You must first select an Account to make correction."
  8.   Msg = Msg & " Please click on an Account in the All Accounts listbox."
  9.   Beep
  10.   MsgBox Msg, vbExclamation, Title
  11.   Me.AllAccountsListbox.SetFocus
  12. Else
  13.  DoCmd.RunMacro "mcrEditAcct" 'open form
  14.  [Forms]![fdlgEditAccount]![txtUsername] = [Forms]![frmEdit]![txtUsername]
  15. End If
  16. End sub
Linq ;0)>
Dec 2 '09 #2
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!
Dec 2 '09 #3
In addition, it open a blank form with the error 2448.
thanks!
Dec 2 '09 #4
missinglinq
3,532 Expert 2GB
If you replaced AllAccountsListbox in the code I gave you with the actual name of your listbox,

DoCmd.RunMacro "mcrEditAcct"

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.
Dec 2 '09 #5
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
Dec 3 '09 #6

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by Pachydermitis | last post: by
1 post views Thread by Phillip | last post: by
3 posts views Thread by Nikhil Patel | last post: by
reply views Thread by Joost | last post: by
1 post views Thread by Tan Hong Giap | last post: by
10 posts views Thread by k.jayachandran | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.