PDA

Click to See Complete Forum and Search --> : Need help with listbox


hlieu
May 18th, 2000, 06:11 AM
Hi All,

I have a listbox containing and lastname and firstname(Smith,John). Is there a way to save them in to two different fields, Smith would go to field(lastname) and John would go to filed(firstname)with one click of save button?

I am using access table for database.

Thaks.

May 18th, 2000, 01:44 PM
Well if it always has a comma seperating the first name from the last name you could write a simple function to break it appart just dim a couple of variables


Private Sub Command1_Click()
dim lname as string
dim fname as string
dim loopcount as long
loopcount = 1
do while loopcount <= listbox1.length
if left(listbox1.text,loopcount) = "," then
loopcount = loopcount + 1
if left(listbox1.text,loopcount) = " " then
loopcount = loopcount + 1
end if
do while loopcount <= listbox1.length
fname = fname & left(listbox1.text,loopcount)
loopcount = loopcount + 1
loop
else
lname = lname & left(listbox1.text,loopcount)
loopcount = loopcount + 1
end if
loop

End Sub

Then before the end of the sub just load the lname and fname into there respective fields call an update and your done... there may be bugs in the function since I wrote just now off the top of my head but that should give you a good idea of what you have to do...

MartinLiss
May 19th, 2000, 05:04 AM
If you have VB6 you can do something much simpler using the Split function. Here is an example with some hardcoded names. The On Error statement is required because there is no First name for the last name. Dim MyArray(2) As String
Dim ResultArray() As String
Dim intIndex As Integer

MyArray(0) = "Smith, John"
MyArray(1) = "Jones, E."
MyArray(2) = "Liss"

On Error Resume Next
For intIndex = 0 To 2
ResultArray() = Split(MyArray(intIndex), ",")
Debug.Print "First name: " & ResultArray(1)
Debug.Print "Last name: " & ResultArray(0)
Next

May 19th, 2000, 11:20 AM
Take Marty's advice much simpler...

That's even one I'll stick in my mental notes...