Results 1 to 5 of 5
  1. #1
    2 Star Lounger
    Join Date
    Nov 2001
    Posts
    192
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Common code for List Boxes in the form (Access 2000)

    I have several list boxes in my form and i made a code for checking whether the customer has chosen from a list.
    To my regret the code is not functioning. May i have some help in checking why the function is wrong ?

    Public Function ListBoxNotClicked(LB As ListBox)
    If IsNull(LB) Then
    DoCmd.Beep
    MsgBox "You have not chosen a student ! "
    End If
    End Function

  2. #2
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Common code for List Boxes in the form (Access 2000)

    You need to provide more information. Are the list boxes bound to underlying fields or are they unbound? How are you passing the listbox into the function and when? What do you mean by the code not functioning? Are you getting an error message, is the function not executing, or are you not seeing the messagebox when you should?
    Charlotte

  3. #3
    4 Star Lounger
    Join Date
    Feb 2002
    Posts
    537
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Common code for List Boxes in the form (Access 2000)

    Thank you for your reply.
    My invoices are bound to queries.I have command buttons where the following code is written:

    If IsNull(Me![list]) Then
    DoCmd.Beep
    MsgBox " Please select Invoice first ! "
    Exit Sub
    End If

    The above code is OK. Actually i want to improve this code and make it applicable to all such buttons, and only to change the name of the list box.
    There is a similar function for control like this:
    Public Function Check(ctrl As Control)
    If IsNull(ctrl) Then
    DoCmd.Beep
    MsgBox " Please select Student ! "
    End If
    End Function
    But how can i rewrite it for the lists boxes?

  4. #4
    Bronze Lounger
    Join Date
    Nov 2001
    Location
    Arlington, Virginia, USA
    Posts
    1,394
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Re: Common code for List Boxes in the form (Access 2000)

    <P ID="edit" class=small>(Edited by MarkD on 07-Sep-03 07:37. Added clarification.)</P>If you are passing Listbox to function as object, you have to test its value. You can use IsNull(obj) for listbox or other control where Value is default property of the control, as VBA does not require you to explicitly specify a default property when referencing the control. I prefer to explicitly reference the default property to avoid any ambiguity.

    Recommend function like this example:

    Private Function ValidateListboxObj(ByRef Lst As ListBox) As Boolean

    If IsNull(Lst.Value) Then
    Beep
    MsgBox "Please select item from list.", vbExclamation, "ITEM NOT SELECTED"
    Lst.SetFocus
    ValidateListboxObj = False
    Else
    ValidateListboxObj = True
    End If

    End Function

    It this case you could also forget about the object references and pass name of listbox to function as string rather than object. Example:

    Private Function ValidateListboxStr(ByRef strListbox As String) As Boolean

    If IsNull(Me.Controls(strListbox).Value) Then
    Beep
    MsgBox "Please select item from list.", vbExclamation, "ITEM NOT SELECTED"
    Me.Controls(strListbox).SetFocus
    ValidateListboxStr = False
    Else
    ValidateListboxStr = True
    End If

    End Function

    Example of use (1st example) with command button that opens report based on selection in listbox (PrintReport is a sub that prints or previews report):

    Private Sub Preview_btn_Click()
    If ValidateListboxObj(Me.Listbox1) Then
    PrintReport acViewPreview
    End If
    End Sub

    Or with 2nd example:

    If ValidateListboxStr("Listbox1") Then
    PrintReport acViewPreview
    End If

    I use functions similar to this on forms with more than one listbox and works OK.

    HTH

  5. #5
    Bronze Lounger
    Join Date
    Nov 2001
    Location
    Arlington, Virginia, USA
    Posts
    1,394
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Re: Common code for List Boxes in the form (Access 2000)

    In further reply, if you want a more generalized function that can be used by any form and any type of control, you can use function like this in a standard code module:

    Public Function IsControlValueValid(ByRef frm As Form, ByRef ctl As Control) As Boolean

    Dim strMsg As String

    If IsNull(frm.Controls(ctl.NAME).Value) Then

    Select Case ctl.ControlType
    Case acComboBox, acListBox
    strMsg = "Please select valid item from list."
    Case acTextBox
    strMsg = "Please enter valid data in box."
    Case acCheckBox, acOptionGroup
    strMsg = "Please check or select valid item."
    Case Else
    strMsg = "Invalid control type specified."
    End Select

    Beep
    MsgBox strMsg, vbExclamation, "USER INPUT REQUIRED"
    frm.Controls(ctl.NAME).SetFocus
    IsControlValueValid = False
    Else
    IsControlValueValid = True
    End If

    End Function

    Example of use on form with listbox:

    If IsControlValueValid(Me, Me.Listbox1) = True Then
    PrintReport acViewPreview
    End If

    Or with textbox:

    If IsControlValueValid(Me, Me.SQL_Text) = True Then
    PrintReport acViewPreview
    End If

    HTH

Posting Permissions

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