2

I have some combo boxes that require user input. In Access 2000 I am linking to our ERP database (FoxPro) via ODBC. So, I cannot modify the table structure at all. For whatever reason, a lot of the fields have leading zeros. I have Limit To List set as Yes on the combo boxes.

Sample of the combo boxes:

enter image description here

I would like it if the user did not have to enter the leading zeros. However, if the user where to enter 47009, they would get the error:

The text you entered isn't an item in the list.

The fields are text and my row source looks like:

SELECT recid, recnum FROM receiver ORDER BY recnum; 

and I am binding to the first column.

Is there a way I can force the user to what is in the list and not require them to enter the leading zeros?

4

1 回答 1

3

对于组合框的行源,您可以使用从recnum字段字符串值中丢弃前导零的查询。使用自定义 VBA 函数中的正则表达式可以轻松去除这些零。这是下面包含的功能的即时窗口演示StripLeadingZeros()

? StripLeadingZeros("000000AV1-00011")
AV1-00011
? StripLeadingZeros("000000TI1-00035")
TI1-00035

因此,您可以在这样的查询中使用该函数...

SELECT recid, StripLeadingZeros(recnum) FROM receiver ORDER BY 2; 

将该查询作为组合框的行源,用户将看不到前导零。如果他们确实需要查看但不键入前导零,您可以将recnum其自身再次添加为第三列。

如果您的字符串值包含数字,则任务会更简单。您可以使用该Val()函数将这些字符串转换为数字。您仍然可以使用StripLeadingZeros(),但Val()应该更快。

SELECT recid, Val(recnum) FROM receiver ORDER BY 2; 
Public Function StripLeadingZeros(ByVal pstrInput As String) As String
    Static re As Object
    If re Is Nothing Then
        Set re = CreateObject("VBScript.RegExp")
        re.pattern = "^0*"
    End If
    StripLeadingZeros = re.Replace(pstrInput, vbNullString)
End Function
于 2013-08-26T19:43:22.343 回答