Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Keeping a list box default value

Status
Not open for further replies.

erictheman

Programmer
Jul 16, 2002
12
CA
I need some help. I have a list box field with two values in it. "Active" and "Deleted". I use code for the form on current property. [Jobstatus] = "Active" It works fine. Every record I go to it shows Active. But the problem is that if I choose Deleted on a record, when I come back to it, it changes to Active. How do I keep it default as Active but if I change it to Delete it will stay that way not turn back to Active? Thank you for your help!
 
In your form's code you need to be able to tell the difference between creating a new record, and editing an existing record. Use a Property to pass a flag in:
Code:
Public Property Let IsNew (RHS as boolean)
  m_bIsNew = RHS
End Property

Then, if it's a new record, you allow the control to use the default value (presumably the first item in the listbox).

If it's not a new record (i.e. you're editting an existing record), then you would set the ListIndex property to the value from the database. Note that ListIndex is zero-based, not one-based (setting it to 2 selects the third item). Also note that the ListIndex is a Long, not a string. If you want to find a value based on it's name, you'll need a function like this:
Code:
Private Sub SelectListByValue(byref TheList as ListBox, byval TheValue as string)

Dim i as long

   For i = 0 to TheList.ListCount
      If ucase$(trim$(TheList.List(i))) = ucase$(trim$(TheValue)) then
         TheList.ListIndex = i
         Exit For
      End If
   Next i
End Sub

And you'd call this sub for every listbox that needed being set.

Chip H.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top