Results 1 to 11 of 11
  1. #1
    Middlemarch is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Mar 2015
    Posts
    373

    Using JOIN and result from function. Will not compile

    My attempted sql is



    Code:
    SELECT tblData2.Prefix, tblData2.LineNum, tblData2.Year, tblComments.comment, tblComments.Address
    FROM tblData2 LEFT JOIN tblComments ON tblData2.LineNum = (NumbersOnly([tblComments].[Address])
    WHERE (((tblData2.Year)<"1990"))
    ORDER BY tblData2.LineNum;
    Code:
    Function NumbersOnly(loc) As String
    Dim RE As Object, REMatches As Object
    Set RE = CreateObject("vbscript.regexp")
    RE.Pattern = "\d+"
        If (RE.Test(loc) = True) Then
            Set REMatches = RE.Execute(loc)
             NumbersOnly = REMatches(0)
        End If
    Set REMatches = Nothing
    Set RE = Nothing
    End Function
    Trust the above shows the problem. Perhaps I cannot use a function in this manner, but maybe there's a workaround?
    Any advice appreciated. Thanks.

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,526
    Use a DLOOKUP in the function...not createObject.

    why is the Year a text? It should be an integer.

  3. #3
    Middlemarch is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Mar 2015
    Posts
    373
    Could you show me what I'd change to use DLOOKUP. I'm new to that, what is the advantage?
    That won't change the error will it ?

    Year is text to avoid '0' in any fields with no year.

  4. #4
    orange's Avatar
    orange is online now Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,742
    Year is a reserved word in Access.
    Why not use Date data type for Dates---that's what they are for?
    See link in my signature for Access functions and examples.

    Good luck.

    PS. I don't use regex too often but thought I'd try your function.
    IT picks up contiguous numerics at start of value. Similar to Val(loc).

  5. #5
    Middlemarch is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Mar 2015
    Posts
    373
    Table design emulated somethings else and there wasn't a lot of choice.
    Val("AA22" ) return s 0 instead of 22. My function also returns 33 for "A33D6" and that's what was wanted.
    Any more advice on DLOOKUP ? If I should change to that I will but have no idea how to apply it.
    Can I refer back to the query again please? It doesn't work, is the concept the problem and/or is there another way?

    Thanks

  6. #6
    orange's Avatar
    orange is online now Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,742
    I just ran your NumbersOnly again. Seems it picks up contiguous numerics.

    Code:
    Function NumbersOnly(loc) As String
    Dim RE As Object, REMatches As Object
    Set RE = CreateObject("vbscript.regexp")
    RE.Pattern = "\d+"
        If (RE.test(loc) = True) Then
            Set REMatches = RE.Execute(loc)
             NumbersOnly = REMatches(0)
        End If
    Set REMatches = Nothing
    Set RE = Nothing
    End Function
    
    Sub testNumbersOnly()
    
    Dim locn As Variant
    locn = "2345rt65"
    Debug.Print NumbersOnly(locn)
    End Sub
    Result is 2345 contiguous numerics
    Val() picks up only the leftmost contiguous numerics.

    Thanks for posting back.

  7. #7
    Middlemarch is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Mar 2015
    Posts
    373
    It's intended to extract a set numbers only from a particular string format e.g xxNNNxxxYY where x is text and N & Y numbers
    It will get the leftmost contiguous numbers if there's no leading text. Also ignore YY if preceded by text.

  8. #8
    orange's Avatar
    orange is online now Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,742
    OK, back to your original question.
    Is tblData2.Year a Date data type? I asked because dates need #1990# (dates encapsulated with octothorpes/hashes)
    tblData2.Year is a number then you don't need the "1990". For numerics you use just the number 1990

    Re DLookup see http://www.techonthenet.com/access/f...in/dlookup.php

  9. #9
    orange's Avatar
    orange is online now Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,742

  10. #10
    Rawb is offline Expert
    Windows 7 64bit Access 2010 32bit
    Join Date
    Dec 2009
    Location
    Somewhere
    Posts
    875
    You might not be able to use a Function like that. If that's the case, then you can get around it by creating a Query of tblComments with NumbersOnly([tblComments].[Address]) as a calculated field.

    However, I see an extra open paren in your Query. Try this and see if it solves the problem for you:
    Code:
    SELECT tblData2.[Prefix], tblData2.[LineNum], tblData2.[Year], tblComments.[comment], tblComments.[Address]
    FROM tblData2 LEFT JOIN tblComments ON tblData2.[LineNum] = NumbersOnly(tblComments.[Address])
    WHERE (((tblData2.[Year])<"1990"))
    ORDER BY tblData2.[LineNum];

  11. #11
    Middlemarch is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Mar 2015
    Posts
    373
    Many thanks for the responses. The problem was a missing bracket on the FROM clause. And I missed
    that completely thinkinbg the join & function were wrong.

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

Similar Threads

  1. Compile Error Sub or function not defined
    By Ray67 in forum Reports
    Replies: 3
    Last Post: 07-02-2012, 04:11 PM
  2. Replies: 6
    Last Post: 11-24-2011, 08:38 PM
  3. Compile error: code or function not defined
    By GeorgeBrown in forum Access
    Replies: 1
    Last Post: 09-19-2011, 10:25 AM
  4. INNER JOIN to a split(array) function?
    By kman42 in forum Queries
    Replies: 8
    Last Post: 05-09-2011, 01:53 PM
  5. Compile error. Sub of function not defined
    By plavookins in forum Reports
    Replies: 7
    Last Post: 04-22-2011, 10:15 AM

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