如何更改数据库中每个字段的每个格式?

时间:2016-11-23 02:52:23

标签: ms-access access-vba ms-access-2010

我的任务是对访问数据库进行一系列更改。其中之一是将数据库中的格式从英国更改为澳大利亚。

数据库中的“cost”字段具有Currency类型,但“Format”为“£#,## 0.00; - £#,## 0.00”。将格式更改为“Currency”会使其显示为$,但这些字段在此数据库的不同表中无处不在。

是否有改变它的中心位置,或者如果没有,是否有一些我可以写的VBA来做到这一点?我试图通过VBA脚本,在每个表上使用For Each迭代器,但我不明白如何通过该方法更改“格式”字段。

2 个答案:

答案 0 :(得分:2)

首先,请阅读以下内容,看看是否能解决您的货币格式问题:http://www.everythingaccess.com/tutorials.asp?ID=Using-the-Currency-field-data-type---without-the-hassle

如果没有,请继续阅读......

经过进一步测试后,似乎正在改变格式'表中字段的属性对现有表单/报告没有任何帮助。因此,您有两种方法可以解决格式问题:(方法1)是将所有表单/报表的设计视图更改为使用显式格式;或(方法2)使用一些VBA代码在运行时设置format属性(打开对象时)。如果要更改许多字段,则选项(1)将更容易。阅读选项说明(方法2)然后你就可以决定。

以下VBA代码可以循环遍历所有报表和表单。我目前将其设置为仅处理一个表单和一个报表,我建议您从小开始,然后在对结果满意时处理所有对象。

看看我用过的每个地方' ###'因为它解释了你可以/应该改变的东西。

方法1:

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

方法2

我在此网站找到了一种在运行时更改格式的方法:http://donnedwards.openaccess.co.za/2009/03/microsoft-access-and-ten-year-old.html

如果您有许多字段需要更改格式,则所涉及的步骤可能会非常困难。以下是所需的步骤,后面是一些示例代码。

  1. 在设计视图中打开表单/报告
  2. 打开&#39;明细部分&#39;
  3. 的属性
  4. 在TAG属性中,列出要更改格式的每个控件名称,并以逗号分隔。
  5. 添加表格/报告&#39; On Open&#39;事件并插入下面显示的代码。
  6. 保存更改
  7. 运行报告/表单

    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
    

答案 1 :(得分:0)

您需要遍历TableDefs并为要更改格式的每个字段调用SetProperty