2015-08-31 48 views
1

我想在MS Access中对数据进行排序,并为此使用查询。在我的表中的数据是这样的:在MS Access中排序

RadButtonNo 
------------------- 
    AA001056  
    AA001579  
    B000049 
    AA001261 
    AA001158 
    AA001108 
    AA001166 
    AA001165 
    AA001164 
    AA001163 
    AA001162 

对于我的输出,我想第一个数据是其中由字母的数据。之后,我想显示字母和数字。

AAAAAAA 
    AABBBBB 
    AAZZZZZ 
    ABA1001 

我使用下面的查询:

SELECT RadButtonNo, ShortName, InspectionDate, Findings, Status, QueryForNot1.Initials, DeptName, Lost, TableApron.InServelDate, TableApron.RemovedDate, 
    TableApron.PrivateUserName, TableApron.PrivateUserEmail, TableApron.ApronType, TableApron.Manufacturer 
    FROM TableApron 
    LEFT JOIN QueryForNot1 ON TableApron.RadButtonNo=QueryForNot1.RadButtonNoI 
    WHERE (((TableApron.Lost)="N" Or (TableApron.Lost)=[@Lost]) 
    ORDER BY LEN(TableApron.RadButtonNo) DESC , TableApron.RadButtonNo; 

有人能解决这个问题,这样会产生我想要的输出,所以,它会是这个样子?通过附加一个字符/或多个与数字条目

ORDER BY LEN(TableApron.RadButtonNo) DESC, TrimTxtString([TableApron].[RadButtonNo]), TrimNumString([TableApron].[RadButtonNo]); 
+0

究竟是什么问题?请重新阅读和编辑您的问题,这是难以理解的。 – Andre

+0

@ Andre451我编辑了这个问题,你能理解它吗 –

+0

一个正常的字段将排序为你的榜样,请问你的问题是什么? – Gustav

回答

0

您可以使用这两个函数:

Public Function TrimNumString(_ 
    ByVal strNumString As String, _ 
    Optional ByVal strDecimalChr As String, _ 
    Optional ByVal booAcceptMinus As Boolean) _ 
    As String 

' Removes any non-numeric character from strNumString including hexadecimal characters. 
' If strDecimalChr is specified, first occurrence of this is not removed. 
' If booAcceptMinus is True, a leading or trailing minus sign is accepted. 
' 
' 1999-08-27. Cactus Data ApS, CPH. 
' 2001-06-21. Speed optimized for large string (64 K). 
' 2003-12-10. intOffset changed to lngOffset. 

    Const cbytNeg As Byte = 45 ' "-" 

    Dim lngPos  As Long 
    Dim lngLen  As Long 
    Dim lngOffset As Long 
    Dim booDec  As Boolean 
    Dim booNeg  As Boolean 
    Dim bytChr  As Byte 
    Dim bytDec  As Byte 
    Dim strNum  As String 

    strNumString = Trim(strNumString) 
    lngLen = Len(strNumString) 
    If lngLen > 0 Then 
    If Len(strDecimalChr) > 0 Then 
     bytDec = Asc(strDecimalChr) 
    End If 
    ' Create empty result string of maximum possible length. 
    strNum = Space(lngLen) 

    For lngPos = 1 To lngLen 
     bytChr = Asc(Mid(strNumString, lngPos, 1)) 
     Select Case bytChr 
     Case 48 To 57 
      ' Digit. 
     Case bytDec 
      ' Decimal point. 
      If booDec = False Then 
      ' One decimal point only. 
      booDec = True 
      End If 
     Case cbytNeg 
      ' Minus sign. 
      bytChr = 0 
      If booAcceptMinus = True And booNeg = False Then 
      If Len(Trim(strNum)) = 0 Or lngPos = lngLen Then 
       bytChr = cbytNeg 
       ' One minus sign only. 
       booNeg = True 
      End If 
      End If 
     Case Else 
      ' Ignore any other character. 
      bytChr = 0 
     End Select 
     If bytChr > 0 Then 
     ' Append accepted character by inserting it in result string. 
     lngOffset = lngOffset + 1 
     Mid(strNum, lngOffset) = Chr(bytChr) 
     End If 
    Next 
    End If 

    ' Trim and return result string. 
    TrimNumString = Left(strNum, lngOffset) 

End Function 


Public Function TrimTxtString(_ 
    ByVal strTxtString As String) _ 
    As String 

' Removes any numeric character from strTxtString. 
' 
' 2003-12-19. Cactus Data ApS, CPH. 

    Dim lngPos  As Long 
    Dim lngLen  As Long 
    Dim lngOffset As Long 
    Dim bytChr  As Byte 
    Dim strNum  As String 

    strTxtString = Trim(strTxtString) 
    lngLen = Len(strTxtString) 
    If lngLen > 0 Then 
    ' Create empty result string of maximum possible length. 
    strNum = Space(lngLen) 

    For lngPos = 1 To lngLen 
     bytChr = Asc(Mid(strTxtString, lngPos, 1)) 
     Select Case bytChr 
     Case 48 To 57 
      ' Digit. 
      bytChr = 0 
     Case Else 
      ' Accept any other character. 
     End Select 
     If bytChr > 0 Then 
     ' Append accepted character by inserting it in result string. 
     lngOffset = lngOffset + 1 
     Mid(strNum, lngOffset) = Chr(bytChr) 
     End If 
    Next 
    End If 

    ' Trim and return result string. 
    TrimTxtString = Left(strNum, lngOffset) 

End Function 

然后调整你的SQL。排序新的领域。我在这里使用一个包含全部z和额外z的值来排除z的真实条目。

SELECT RadButtonNo 
from tbl 
order by IIF(RadButtonNo like "*#*", "zzzzzzzzz" & RadButtonNo, RadButtonNo) 
+0

我希望这可以在MS访问的SQL脚本中完成,我们可以做 –

+0

,所以我只添加MS访问标签 –

+0

是的,只要继续。 – Gustav