Results 1 to 3 of 3
  1. #1
    ssalem is offline Advanced Beginner
    Windows 7 32bit Access 2007
    Join Date
    Feb 2013
    Posts
    39

    Simple Instr Function returning 0 when string is there

    Ok this seems like the simplest thing but it is not working in my code. I have tried everything but just can't figure out why my instr function is not finding my string when it is right there. Here is my code:

    If Not rs1.EOF And Not IsNull(msub) Then
    lpos = InStr(rs1!sub, msub)
    If lpos = 0 Then
    msub = msub & ", " & rs1!sub
    End If


    nqty = nqty + Nz(rs1!Farm_M_C_Qty, 0) + Nz(rs1!MTT_2pg_C, 0) + Nz(rs1!MTT_4pg_C, 0)
    npostage = npostage + Nz(rs1!FARM_M_C_POSTAGE, 0) + Nz(rs1!MTT_2PG_POSTAGE, 0) + Nz(rs1!MTT_4PG_POSTAGE, 0)
    End If
    End If

    The sub contains a 4 digit text number of the month and day of a date. EX: 0706. I am concantenating a field of subs but do not want to include the same sub twice. When it goes through the loop my first value is "0706", msub becomes "0706", the next time through it rs1!sub = "0707" so msub becomes "0706, 0707", the next time through rs1!sub = "0707" again. the instr function should return 6 for lpos and skip doing the f statement to concantenate msub but lpos returns 0 and I end up with "0706, 0707, 0707". I have tried defining a start position and I have tried defining it as binary and text but I keep getting 0 as the result like it never finds it in the string. EX: lpos = Instr(1, rs1!sub, msub, 1) still returns 0. What am i missing?

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    I think you have it backwards, you have
    lpos = InStr(rs1!sub, msub)

    but msub keeps getting bigger and will never be found in rs1!sub.
    I think you want
    lpos = InStr(msub,
    rs1!sub)


  3. #3
    ssalem is offline Advanced Beginner
    Windows 7 32bit Access 2007
    Join Date
    Feb 2013
    Posts
    39
    Ok, now I feel stupid. Now it works. ha. Sometimes the simple things are the things that will drive you crazy..lol. That was too obvious.

    Thanks, for making my day go easier for a Monday.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Returning part of a string
    By dr4ke in forum Queries
    Replies: 7
    Last Post: 01-15-2013, 11:21 AM
  2. Multi Select Simple Returning No Results
    By jsimard in forum Programming
    Replies: 11
    Last Post: 01-24-2012, 10:28 PM
  3. Function - Returning a Value
    By dreamnauta in forum Programming
    Replies: 3
    Last Post: 01-05-2012, 03:37 PM
  4. Simple Query not returning any data.
    By psoli in forum Queries
    Replies: 4
    Last Post: 06-16-2011, 11:07 AM
  5. Calling a function and returning a value
    By 3dmgirl in forum Programming
    Replies: 0
    Last Post: 04-23-2007, 02:20 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums