2016-11-23 93 views
0

我的任務是對訪問數據庫進行一系列更改。其中之一是將數據庫中的格式從英國改爲澳大利亞。如何更改數據庫中每個字段的每種格式?

數據庫中的「成本」字段具有貨幣類型,但「格式」爲「£#,## 0.00; - £#,## 0.00」。將格式更改爲「貨幣」使其顯示爲$,但這些字段無處不在此數據庫的不同表中。

是否有一箇中心點來改變它,或者如果沒有,是否有一些我可以寫的VBA來做到這一點?我試圖通過VBA腳本來完成,在每個表上使用For Each迭代器,但我不知道如何通過該方法更改「格式」字段。

+0

這不是表,但在形式和顯示值報告的控制,必須改變。因此迭代Forms,Controls,Property Format並在找到£時進行調整。 – Gustav

+0

這個應用程序在英國或澳大利亞運行嗎?在某些情況下,您可能可以更改區域設置以獲得您想要的結果,但是如果您在英國,我懷疑您想將它們設置爲澳大利亞,否則意外的結果可能會與其他產品一起出現。你可以改變口罩和格式,但這可能是殘酷的。我們部署到70多個國家,但由於所有用戶都是'美國'員工,我們要求他們堅持英語(美國)。 –

+0

@ WayneG.Dunn在「貨幣」類型的每個字段上,格式字段都設置爲「£#,## 0.00; - £#,## 0.00」,我的機器設置爲澳大利亞地區,但它並沒有幫助:( –

回答

0

您需要遍歷TableDefs並調用SetProperty來查看您希望更改格式的每個字段。

+0

謝謝你的約翰。在Access中通過VBA的方法,但我沒有得到很多,你有任何示例代碼可以幫助嗎? –

+0

@LewisCianci - 這是什麼意思 - 你沒有得到很遠?你可以顯示你的代碼嘗試和什麼發生了什麼或沒有發生,應該怎麼辦?DId它改變了表格中的字段格式嗎?您是否需要更改表格字段 – dbmitch

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

的如果你有很多領域,涉及的步驟可能會很困難s需要改變格式。以下是所需的步驟,以及一些示例代碼。

  1. 在設計視圖打開
  2. 打開表單/報表的「詳細信息部分」
  3. 在標籤屬性,要更改格式,用逗號分隔的列表,每個控件的名稱屬性。
  4. 添加表單/報告「打開」事件並插入下面顯示的代碼。
  5. 保存更改
  6. 運行報表/表單

    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 
    
+0

感謝這看起來像迄今爲止最有希望的迴應。我可以問一個問題 - 你在使用內置的a使用VBA編輯器來做這個東西?我習慣了Visual Studio IDE和美妙的智能感知,而Access中的intellisense看起來有些雜亂無章。 –

+0

是的,我只是使用Access(Office 2010)內置的VBA編輯器。您應該能夠複製代碼,粘貼到模塊中並運行它。 –

相關問題