Results 1 to 4 of 4

Thread: Need help with listbox

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Feb 2000
    Location
    Edmonton,AB,Canada
    Posts
    28
    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.

  2. #2
    Guest
    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...

  3. #3
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    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.
    Code:
        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

  4. #4
    Guest

    Wink

    Take Marty's advice much simpler...

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


Posting Permissions

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



Click Here to Expand Forum to Full Width