Results 1 to 8 of 8
  1. #1
    wolfm is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Dec 2013
    Posts
    93

    query to look within field for a number


    I have a situation where one of my table fields is set as text, even though I am using that field for numbers. Reason being is that sometimes the excel database I am importing from will contain text in that field.
    Now, I am wanting to have my reports total the numbers in that field. This is a new feature I decided to add later. When I try to change the field to text, it tells me it will delete much of my data. I don't want to do that.
    Instead of re-designing the orginal excel database and import steps, is there a way to modify my query so that it searches that particular field for numbers only? (dis-regard text)
    I'm thinking perhaps not, and I would have to add an additional field for numeric only. But, if anyone has any ideas, please advise.

    Thank you!

  2. #2
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows XP Access 2003
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,610
    Will the number of text characters in each field that has them vary.
    Will the position of text characters in each field vary.
    Perhaps a few examples would help.
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  3. #3
    wolfm is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Dec 2013
    Posts
    93
    Yes, number of characters can vary, although never seems to exceed 15 characters or so.
    Position of characters also can vary. Basically, the user entering the original data in Excel can enter anything they want, the way it is set up now. But I can edit before importing the records into Access, if that would help the query. Just trying to limit the amount of editing that I have to do. Thank you!

  4. #4
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,849
    Here is a function derived from a Dev Ashish post. It will take in a string and return only the numeric characters.

    Code:
    '---------------------------------------------------------------------------------------
    ' Procedure : fExtractStrNums
    ' Author    : user
    ' Date      : 2/27/2009
    ' Purpose   :   To extract only numerics from a string of mixed alpha,
    'numerics and special chars.
    '---------------------------------------------------------------------------------------
    '
    Function fExtractStrNums(ByVal strInString As String) As String
    ' From Dev Ashish
    '(Q) How do I extract only characters from a string which has both numeric and alphanumeric characters?
    
    '(A) Use the following function. Note that the If loop can be modified to extract either both Lower and Upper case character or either
    'Lower or Upper case characters.
    
    '************ Code Start **********
    
    Dim lngLen As Long, strOut As String
    Dim i As Long, strTmp As String
    
       On Error GoTo fExtractStrNums_Error
    
        lngLen = Len(strInString)
        strOut = ""
        For i = 1 To lngLen
            strTmp = Left$(strInString, 1)
            strInString = Right$(strInString, lngLen - i)
            
       'ADJUSTED CODE TO GET ONLY NUMERICS
       
            If (Asc(strTmp) >= 48 And Asc(strTmp) <= 57) Then
                strOut = strOut & strTmp
            End If
        Next i
        fExtractStrNums = strOut
          On Error GoTo 0
       Exit Function
    
    fExtractStrNums_Error:
        MsgBox "Error " & Err.number & " (" & Err.Description & ") in procedure fExtractStrNums of Module Module1"
    End Function
    Good luck

  5. #5
    wolfm is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Dec 2013
    Posts
    93
    In design view of my query, I right clicked in criteria under that field, build event, and copied the code into the box. Is this correct? Not really sure how to enter code.
    When I do this, returns the error "The expression you entered contains invalid syntax, or you need to enclose your data in quotes". Thank you.

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,632
    This code needs to be in a general VBA module. Then the query will call the function.

    Open the VBA editor from Database Tools on the ribbon. Create a module (Insert > Module). Copy/paste the posted code.

    In the query, call the function in an expression that will create a field. Do this on the Fields row.

    JustNums: fExtractStrNums([fieldname])

    Be aware if the field has no data, the function will error.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  7. #7
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows XP Access 2003
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,610
    To use the function that orange offered it would need to be pasted into a General module, then called from within a querry. See example db attached:
    Attached Files Attached Files
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  8. #8
    wolfm is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Dec 2013
    Posts
    93
    Thanks All! It works great! You are awesome!!

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

Similar Threads

  1. Replies: 6
    Last Post: 01-24-2013, 10:02 PM
  2. Replies: 2
    Last Post: 05-30-2012, 10:38 AM
  3. Replies: 1
    Last Post: 11-20-2011, 12:11 PM
  4. Replies: 3
    Last Post: 04-19-2011, 06:41 PM
  5. Replies: 1
    Last Post: 10-09-2008, 04:48 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