Results 1 to 13 of 13
  1. #1
    TEE45SEE is offline Novice
    Windows 10 Access 2003
    Join Date
    Jul 2023
    Location
    Ontario, Canada
    Posts
    4

    LEFT() Function

    the syntax for the above is LEFT(string, number_of_chars) which is quite straightforward. However I have a case where the (number of chars) will vary from case to case. I have tried using a lngValue rather than a hard number 4 for example but without success. Is there something I am missing or is there a workaround for this?

    Thanks

  2. #2
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    Usually it involves other functions nested somehow. Impossible to be even remotely specific about that without some kind of sample data and a good idea of how representative the sample is compared to the full data set. In other words, what's also important is if there is some kind of key that can be used, such as 'always return/extract all/n of the characters after the first 0 on the left/right'. Seldom are these issues fully explained at the outset - it takes a few tries to nail down a pattern and thus a solution.

    EDIT - afterthought: if you're saying that you tried to use a variable and a function would not recognize a value for that variable, then that is another issue. Variables will work in functions such as Left as long as they contain a value that the function will accept.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  3. #3
    isladogs's Avatar
    isladogs is offline Access MVP / VIP
    Windows 10 Office 365
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    6,204
    Try something like Left ("YourString", InStr(YourString", "SearchCharacter"))

    For example
    ?Left("asxzde",instr("asxzde","x"))
    asx
    Colin Riddington, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I know I don't know, I keep quiet!

  4. #4
    orange's Avatar
    orange is offline Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,870
    Perhaps you could describe what is causing the problem/confusion/unexpected action with an example or 2 to provide more context.

  5. #5
    TEE45SEE is offline Novice
    Windows 10 Access 2003
    Join Date
    Jul 2023
    Location
    Ontario, Canada
    Posts
    4
    Quote Originally Posted by TEE45SEE View Post
    the syntax for the above is LEFT(string, number_of_chars) which is quite straightforward. However I have a case where the (number of chars) will vary from case to case. I have tried using a lngValue rather than a hard number 4 for example but without success. Is there something I am missing or is there a workaround for this?

    Thanks
    Here is the code that I am using. If I just insert either 2 or 3 or or100 ..... in pace of lngConLen it works like a charm. Which points me back to my original question. Many thanks
    Private Sub cboConversation_AfterUpdate()

    Dim strSearchItem As String
    Dim strSQL As String
    Dim lngConLen As Long 'ConversationLength

    strSearchItem = Me.cboConversation.Column(0) & ":" ' Colon is added here in order to be used by InStr
    Debug.Print strSearchItem

    strSearchItem = Left(strSearchItem, InStr(strSearchItem, ":") - 1)
    Debug.Print strSearchItem

    lngConLen = Len(strSearchItem)
    Debug.Print lngConLen

    strSQL = "SELECT * FROM qryComments" _
    & " WHERE Left ([memComment],lngConLen) Like " & """" & strSearchItem & """"
    Debug.Print strSQL
    Me.RecordSource = strSQL
    End Sub

  6. #6
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    Beyond saying you've not had success I've yet to read what the problem is. I surmise that everything looks ok in the variable debugs so perhaps your issue is in the sql debug or the sql returns no records.

    First, there's no point in using Like without wildcards. You might as well just use equals ( = ).
    Second, the first column (0) of a combo usually holds the long integer (e.g. autonumber) pk value of a record. If you have designed that way, you're saying "LIKE 5" or some other number.

    You need to be clear on what the issue is beyond saying you've not had success with something.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  7. #7
    isladogs's Avatar
    isladogs is offline Access MVP / VIP
    Windows 10 Office 365
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    6,204
    I'm also unclear what the issue is.
    However, in your code you place a colon at the end of a search string then search for everything to the left of the colon...which means it returns the original search string.
    Somewhat pointless, methinks.

    Agree with comments re use of Like
    Colin Riddington, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I know I don't know, I keep quiet!

  8. #8
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,932
    I suspect the issue is with the construction of the sql

    strSQL = "SELECT * FROM qryComments" _
    & " WHERE Left ([memComment],lngConLen) Like " & """" & strSearchItem & """"

    think it should be
    strSQL = "SELECT * FROM qryComments" _
    & " WHERE Left ([memComment],” & lngConLen & “) Like " & """" & strSearchItem & """"

  9. #9
    TEE45SEE is offline Novice
    Windows 10 Access 2003
    Join Date
    Jul 2023
    Location
    Ontario, Canada
    Posts
    4
    Thanks again for the responses.
    Simply put the question is "why will my code within the LEFT() work correctly with a digit like 4 but will not work with the variable (lngConLen) which has the value of 4"

    Something I have just noticed. Running through my code the DebugPrint lngConLen has a value of 4 when I hover over it but it shows no value when I hover over it within the SQL. It is also asking me to "Enter Parameter Value" which, if I enter 4 it works as it should.

  10. #10
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2013 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,250
    You need to isolate the variable inside the strSQL string as shown by CJ_London in post # 8.
    https://learn.microsoft.com/en-us/of...s-and-controls
    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  11. #11
    TEE45SEE is offline Novice
    Windows 10 Access 2003
    Join Date
    Jul 2023
    Location
    Ontario, Canada
    Posts
    4
    Thanks again for ALL responses. Vlad was correct that CJ_London had it right. The only issue with that was the different double quotes CJ used. ” & lngConLen & “ When I changed it to " & lngConLen & " it worked worked perfectly.

    I have also removed the addition of the colon (pointless indeed) as well as changing the "LIKE" to =
    The code now stands as follows
    Private Sub cboConversation_AfterUpdate()

    Dim strSearchItem As String
    Dim strSQL As String
    Dim lngConLen As Long 'ConversationLength

    strSearchItem = Me.cboConversation.Column(0)
    lngConLen = Len(strSearchItem)
    strSQL = "SELECT * FROM qryComments" _
    & " WHERE Left ([memComment]," & lngConLen & ") = " & """" & strSearchItem & """"
    Me.RecordSource = strSQL
    End Sub

  12. #12
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2013 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,250
    Much easier on the eyes !
    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  13. #13
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,932
    [QUOTE]The only issue with that was the different double quotes CJ used[/QUOTE]

    it’s because I was on my phone- different character set

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

Similar Threads

  1. Replace Function with Left function
    By Khalil Handal in forum Access
    Replies: 21
    Last Post: 04-13-2019, 03:25 PM
  2. left Function on an import
    By Drew101 in forum Access
    Replies: 3
    Last Post: 06-06-2017, 01:33 PM
  3. using the left function within a DLookup
    By crowegreg in forum Forms
    Replies: 1
    Last Post: 09-03-2013, 05:34 PM
  4. mid,left function
    By revned12 in forum Queries
    Replies: 2
    Last Post: 07-06-2012, 12:09 AM
  5. Update with left function
    By yerromnitsuj in forum Access
    Replies: 3
    Last Post: 01-09-2012, 03:41 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