Results 1 to 4 of 4

Thread: [Q]Using "instr"

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2006
    Posts
    3

    Question [Q]Using "instr"

    I've finished my customer database program EXCEPT one last thing.

    I got to add products to database field from listbox by putting product names in string variable (ie. strCustom = APPLE LW SELECT360 TONER (OEM), BROTHER MFC2800 PRINTER (Reman.))

    but then I want to reverse it back, so when user wants to edit the product list from the listbox, he/she can pick individual ones.

    from APPLE LW SELECT360 TONER (OEM), BROTHER MFC2800 PRINTER (Reman.)

    to

    APPLE LW SELECT360 TONER (OEM)
    BROTHER MFC2800 PRINTER (Reman.)


    VB Code:
    1. Dim strTempCustom As String
    2.         Dim strTempCustom2 As String
    3.         Dim pos As Integer
    4.        
    5.         rsCustomer.FindFirst "CompanyName= " & Chr(34) & cdTempCustom.strCompany & Chr(34)
    6.        
    7.         strTempCustom = rsCustomer!Products
    8.        
    9.         pos = 1
    10.            
    11.             Do While InStr(pos, strTempCustom, ",")
    12.                 pos = InStr(pos, strTempCustom, ",")
    13.                 lstProduct.AddItem Mid(strTempCustom, pos + 1, InStr(pos, strTempCustom, ",") - pos)
    14.            
    15.             Loop

    my friend me gave me this code, but it just loops infinitely.

    I'm not used to using Instr function..

    any help would be appreciated, thank you

  2. #2
    Hyperactive Member
    Join Date
    Jun 2006
    Posts
    372

    Re: [Q]Using "instr"

    found a few thing i would do differently
    this should help you get on track...
    but you need to modify strTempCustom, or you will always find it, and hence get in an infinite loop.

    not sure how long strTempCustom is, but if its only a few items, you could just strip out the left side up to the comma, and use mid$ to get all the text to the right of the 2nd comma.

    just my 2 cents



    VB Code:
    1. Dim strTempCustom2 As String
    2.             Dim pos As Integer
    3.             Dim pos2 As Long
    4.             Dim tmpLine As String
    5.  
    6.             rsCustomer.FindFirst "CompanyName= " & Chr(34) & cdTempCustom.strCompany & Chr(34)
    7.            
    8.             strTempCustom = rsCustomer!Products
    9.            
    10.             pos = 1
    11.                
    12.         Do While InStr(strTempCustom, ",") > 1
    13.                    
    14.             pos = InStr(strTempCustom, ",") ' first comma
    15.             pos2 = InStr(pos + 2, strTempCustom, ",") - pos  'len till 2nd comma
    16.             tmpLine = Mid$(strTempCustom, pos + 1, pos2) 'text between commas
    17.                
    18.  
    19.             lstProduct.AddItem tmpLine 'add temp text instead of trying to parse
    20.  
    21.  Debug.Print pos, strTempCustom
    22.                
    23.                 Loop

  3. #3
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: [Q]Using "instr"

    it's easier to use split in this situation:
    VB Code:
    1. Dim strTempCustom As String, strParts() As String
    2.     Dim N As Integer
    3.    
    4.     rsCustomer.FindFirst "CompanyName= " & Chr(34) & cdTempCustom.strCompany & Chr(34)
    5.     strTempCustom = rsCustomer!Products
    6.    
    7.     strParts = Split(strTempCustom, ",")
    8.     For N = 0 To UBound(strParts)
    9.         lstProduct.AddItem Trim$(strParts(N))
    10.     Next N

  4. #4
    PowerPoster
    Join Date
    May 2006
    Location
    Location, location!
    Posts
    2,673

    Re: [Q]Using "instr"

    Although bush has said a better alternative, I would just like to point out that in the original code
    VB Code:
    1. Do While InStr(pos, strTempCustom, ",")
    2.                 pos = InStr(pos, strTempCustom, ",")

    Would probably be better as
    VB Code:
    1. pos=InStr(strTempCustom, ",") +1
    2. Do
    3. 'other code here
    4. pos=InStr(pos,strTempCustom, ",") +1
    5. loop while pos<>1

    Less calls to instr, and very little difference in cose size :-)

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