access 2003 文本显示前导零 [英] access 2003 text display leading zero
问题描述
我们目前使用的是 Access 2003.我有一个车辆维护日志,其中有一个用于输入车辆编号的文本字段.该字段需要是文本,因为当车辆退役时,其中会包含一个 R.他们希望该字段是带有前导零的四位数字.因此,车辆 22 将显示在表中并报告为 0022,当退役时它将是 R0022.我试图将字段的格式更改为0000"和0000"#,但这些都不会显示前导零.
We are currently using Access 2003. I have a vehicle maintenance log which has a text field for vehicle numbers. The field needs to be text as it will have a R in it when a vehicle is retired. They would like to have the field be a four digit number with leading zero's. So vehicle 22 would be displayed in the table and reports as 0022 and when retired it would be R0022. I have tried to change the format of the field to "0000" and "0000"# but neither of these will display the leading zero's.
推荐答案
您真的希望您的用户手动编辑该字段吗?
我不喜欢这样的解决方案,因为它容易出错(除非你检查很多东西以确保没有人输入无效数据)并且对你的用户感觉不雅.
Do you really want you users to manually edit that field?
I don't like solutions like this, because it's error-prone (unless you check a lot of things to make sure that no one enters invalid data) and feels unelegant to your users.
我只想在表格中保存以下内容:
I would just save the following in the table:
- 数字字段中的车辆编号(
22
,而不是0022
) - 指示车辆是否退役的布尔字段
这对您的用户来说更容易使用:
This is much easier for your users to work with:
- 他们只需输入新的车辆编号,而无需考虑前导零
- 要报废车辆,他们只需要设置一个复选框,而不是将正确的字母放在车辆编号前面
另外,显示所需的数字 R0022
现在只是显示/格式化表格中的数据:
Plus, showing the desired number R0022
now becomes just a matter of displaying/formatting the data from the table:
Public Function GetDisplayNo(VehicleNo As Integer, IsRetired As Boolean) As String
GetDisplayNo = IIf(IsRetired, "R", "") & Right("0000" & VehicleNo, 4)
End Function
你可以这样使用这个函数:
You can use this function like this:
GetDisplayNo(22, True)
返回 R0022
GetDisplayNo(22, False)
返回 0022
而如果您需要在报表或连续表格中显示车辆编号列表,您可以在底层查询中直接使用此功能:
And if you need to display a list of vehicle numbers in a report or a continuous form, you can directly use this function in the underlying query:
SELECT
Vehicles.VehicleNo,
Vehicles.IsRetired,
GetDisplayNo([VehicleNo],[IsRetired]) AS DisplayNumber
FROM Vehicles;
这篇关于access 2003 文本显示前导零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!