Results 1 to 8 of 8
  1. #1
    btappan is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2013
    Posts
    43

    querying for dimensions

    I have a field containing dimensions stored like this as Length, Width, Depth:
    6 x 6 x 6
    17 x 14 x 11
    10.25 x 4 x 6.75

    I need 3 different queries so I can return them in separate columns for Length, Width, Depth. Some fields have no dimensions at all, so I need a null/blank value returned for those. I've tried a few things but am having trouble with flexibility with the decimals and without.

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,524
    You can make a form with 3 boxes for users to enter.
    The vb code can filter the continuous form on the values given ,
    but ,how strict do you want to be?
    if user enters W=2.25. ,but the value in the db is 2.24 ,the query will fail.
    would you want to add tolerance? +-N ?

  3. #3
    btappan is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2013
    Posts
    43
    Thanks, the data I'm referring to is in a spreadsheet from a vendor. I'm trying to use access to get it into a different format.

  4. #4
    aytee111 is offline Competent At Times
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    One method:

    Query:
    SELECT Table1.Dimensions, GetWidth([Dimensions]) AS Width, GetLength([Dimensions]) AS Length, GetHeight([Dimensions]) AS Height
    FROM Table1;

    In a new module in VBA:
    Code:
    Public Function GetWidth(Dimensions)
    
        If IsNull(Dimensions) Or Dimensions = "" Then Exit Function
        
        Dim x As Integer
        
        x = InStr(Dimensions, "x")
        GetWidth = Left(Dimensions, x - 2)
    End Function
    Public Function GetLength(Dimensions)
        If IsNull(Dimensions) Or Dimensions = "" Then Exit Function
        
        Dim x As Integer, y As Integer
        
        x = InStr(Dimensions, "x")
        y = InStr(x + 1, Dimensions, "x")
        GetLength = Mid(Dimensions, x + 2, y - 2 - x)
    End Function
    
    Public Function GetHeight(Dimensions)
        If IsNull(Dimensions) Or Dimensions = "" Then Exit Function
        
        Dim x As Integer, y As Integer
        
        x = InStr(Dimensions, "x")
        y = InStr(x + 1, Dimensions, "x")
        GetHeight = Right(Dimensions, Len(Dimensions) - y - 1)
    End Function

  5. #5
    jwhite is offline Competent Performer
    Windows 10 Access 2013 32bit
    Join Date
    Dec 2012
    Location
    North Carolina
    Posts
    349
    If the vendor is consistent in the format of the entries, then:

    strLength = Trim(Split("10.25 x 4 x 6.75","x")(0))
    strWidth =
    Trim(Split("10.25 x 4 x 6.75","x")(1))
    strDepth =
    Trim(Split("10.25 x 4 x 6.75","x")(2))

    Replace the literal string with your variable or field name as applicable.

  6. #6
    aytee111 is offline Competent At Times
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    Nice! Doesn't work in a query - how to make it work?

  7. #7
    jwhite is offline Competent Performer
    Windows 10 Access 2013 32bit
    Join Date
    Dec 2012
    Location
    North Carolina
    Posts
    349
    I happened to see that syntax (the index at the end) format in another post somewhere last week. Didn't try in a query. Perhaps could create a function passing the fieldname and the element desired:
    Code:
    Public Function GetElementString(strIn As String, strDelimiter As String, intElement As Integer) As String
        ' Return value as String
        ' ? GetElementString("1 x 2 x 3","x",1)
        If strIn & vbNullString <> vbNullString Then
            GetElementString = Trim(Split(strIn, strDelimiter)(intElement))
        Else
            GetElementString = ""
        End If
    End Function
    
    
    Public Function GetElementNumber(strIn As String, strDelimiter As String, intElement As Integer) As Variant
        'Return as Variant so result can be set to any numeric data type
        ' ? GetElementNumber("1 x 2 x 3","x",1)
        If strIn & vbNullString <> vbNullString Then
            GetElementNumber = Val(Split(strIn, strDelimiter)(intElement))
        Else
            GetElementNumber = 0
        End If
    End Function
    Note that intElement is 0-based.
    Last edited by jwhite; 04-25-2017 at 08:13 AM. Reason: Added code to first check for empty string

  8. #8
    ssanfu is offline Master of Nothing
    Windows XP Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    SELECT Table1.Dimensions, GetWidth([Dimensions]) AS Width, GetLength([Dimensions]) AS Length, GetHeight([Dimensions]) AS Height
    Just wanted to point out that both WIDTH and HEIGHT are reserved words in Access.......

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

Similar Threads

  1. Replies: 3
    Last Post: 10-20-2015, 02:03 PM
  2. Replies: 6
    Last Post: 10-01-2013, 08:23 AM
  3. Package dimensions query
    By aflamin24 in forum Queries
    Replies: 3
    Last Post: 10-23-2012, 07:26 PM
  4. Querying 2 or more tables
    By NewbieInCT in forum Queries
    Replies: 12
    Last Post: 05-11-2012, 09:49 AM
  5. code to determine correct dimensions
    By mbar in forum Programming
    Replies: 3
    Last Post: 05-03-2012, 04:27 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