首先,请阅读以下内容,看看是否可以解决您的货币格式问题:http://www.everythingaccess.com/tutorials.asp?ID=Using-the-Currency-field-data-type---without-the-hassle
如果没有,请继续阅读...
经过进一步测试,更改表中字段的“格式”属性似乎对现有表单/报告没有任何帮助。因此,您有两种选择来解决格式问题:(方法 1)是将所有表单/报告的设计视图更改为使用显式格式;或(方法 2)使用一些 VBA 代码在运行时(打开对象时)设置格式属性。如果您有很多字段要更改,选项 (1) 会更容易。阅读选项(方法 2)的说明,然后再做决定。
以下 VBA 代码可以循环遍历所有报表和表单。我目前设置为只处理一个表格和一个报告,我建议你从小处着手,当对结果满意时,处理所有对象。
查看我使用“###”的每个地方,因为它说明了您可以/应该更改的内容。
方法一:
Option Compare Database
Option Explicit
' NOTE!!!! Look at all comments containing '###' for notes
Sub Fix_Reports_and_Forms()
Change_Form_Properties
Change_Report_Properties
End Sub
Function Change_Form_Properties()
Dim dbs As DAO.Database
Dim ctr As Container
Dim doc As Document
Dim frm As Form
Dim ctl As Control
Dim ObjectName As String
Dim i As Integer
Dim bChg As Boolean
Set dbs = CurrentDb
Set ctr = dbs.Containers!Forms
For Each doc In ctr.Documents
ObjectName = doc.Name
'### For testing, I suggest you change the following code to select only ONE Form. Later, remove the IF to do all.
If ObjectName = "Table1" Then ' ### If you want to process ALL forms, remove this 'IF' and matching 'End If'
DoCmd.OpenForm ObjectName, acViewDesign
DoCmd.Minimize
Set frm = Forms(doc.Name)
Debug.Print "Form: " & frm.Name & vbTab & "Ctls: " & frm.Controls.Count
bChg = False
For Each ctl In frm.Controls ' Loop thru all controls
If ctl.ControlType = acTextBox Then ' Is this a TextBox?
Debug.Print vbTab & ctl.Name & vbTab & "Type: " & ctl.ControlType & vbTab & "Format: " & ctl.Properties("Format")
If ctl.Properties("Format") <> "" Then
' Add code to test if some currency format
If ctl.Properties("Format") = "Currency" Then
ctl.Properties("Format") = "£#,##0.00;-£#,##0.00"
bChg = True
ElseIf ctl.Properties("Format") = "$#,##0.00;-$#,##0.00" Then
ctl.Properties("Format") = "£#,##0.00;-£#,##0.00"
bChg = True
Else ' ### any other formats that need to be changed?
' Add code if needed
End If
Else
' ### Here I am checking for a specific field name as ControlSource
' This is not needed if you can identify controls by above code.
If ctl.Properties("ControlSource") = "CurrFld" Then
ctl.Properties("Format") = "£#,##0.00;-£#,##0.00"
bChg = True
End If
End If
ElseIf ctl.ControlType = acComboBox Then ' ### Add code for other control types as needed
' Do something if necessary
End If
Next ctl
If bChg = True Then ' Save changes
DoCmd.Close acForm, ObjectName, acSaveYes
Else ' Do not Save
DoCmd.Close acForm, ObjectName, acSaveNo
End If
End If '### Remove if processing ALL Form Names
Next doc
End Function
Function Change_Report_Properties()
Dim dbs As DAO.Database
Dim ctr As Container
Dim doc As Document
Dim rpt As Report
Dim ctl As Control
Dim ObjectName As String
Dim i As Integer
Dim bChg As Boolean
Set dbs = CurrentDb
Set ctr = dbs.Containers!Reports
For Each doc In ctr.Documents
ObjectName = doc.Name
'### For testing, I suggest you change the following code to select only ONE Form. Later, remove the IF to do all.
If ObjectName = "Table1" Then ' ### If you want to process ALL Reports, remove this 'IF' and matching 'End If'
DoCmd.OpenReport ObjectName, acViewDesign
DoCmd.Minimize
Set rpt = Reports(doc.Name)
Debug.Print "Report: " & rpt.Name & vbTab & "Ctls: " & rpt.Controls.Count
bChg = False
For Each ctl In rpt.Controls
If ctl.ControlType = acTextBox Then
Debug.Print vbTab & ctl.Name & vbTab & "Type: " & ctl.ControlType & vbTab & "Format: " & ctl.Properties("Format")
If ctl.Properties("Format") <> "" Then
' Add code to test if some currency format
If ctl.Properties("Format") = "Currency" Then
ctl.Properties("Format") = "£#,##0.00;-£#,##0.00"
bChg = True
ElseIf ctl.Properties("Format") = "$#,##0.00;-$#,##0.00" Then
ctl.Properties("Format") = "£#,##0.00;-£#,##0.00"
bChg = True
Else ' any other formats that need to be changed?
' Add code if needed
End If
Else
'### Here I am checking for a specific field name as ControlSource
If ctl.Properties("ControlSource") = "CurrFld" Then
ctl.Properties("Format") = "£#,##0.00;-£#,##0.00"
bChg = True
End If
End If
ElseIf ctl.ControlType = acComboBox Then ' ### Add code for other control types as needed
' Do something if necessary
End If
Next ctl
If bChg = True Then ' Save changes
DoCmd.Close acReport, ObjectName, acSaveYes
Else ' Do not Save
DoCmd.Close acReport, ObjectName, acSaveNo
End If
End If '### Remove if processing ALL Report Names
Next doc
End Function
方法二
我在这个站点找到了一种在运行时更改格式的方法:http://donnedwards.openaccess.co.za/2009/03/microsoft-access-and-ten-year-old.html
如果您有许多需要更改格式的字段,所涉及的步骤可能会太难。以下是所需的步骤,然后是一些示例代码。
- 在设计视图中打开表单/报告
- 打开“详细信息部分”的属性
- 在 TAG 属性中,列出要更改格式的每个控件名称,并用逗号分隔。
- 添加表单/报告“打开时”事件并插入如下所示的代码。
- 保存更改
-
运行报告/表单
Option Compare Database
Option Explicit
Private Sub Report_Open(Cancel As Integer)
'
'// Fix up CURRENCY formatting
'
Dim strField As String, strTag As String, n As Long
strTag = Me.Detail.Tag
If Len(strTag) > 2 Then
strTag = strTag & ","
n = InStr(1, strTag, ",")
While n > 0
strField = Mid$(strTag, 1, n - 1)
strTag = Mid$(strTag, n + 1)
'Me(strField).Format = "Currency"
Me(strField).Format = "£#,##0.00;-£#,##0.00"
n = InStr(1, strTag, ",")
Wend
End If
End Sub