2016-05-15 51 views
0

我有一個表中的Access與一些組合框字段(組合框作爲行源)。每個組合框有2列(第一個是整數類型,第二個是文本類型)。第二欄是用戶可見的內容。第一個是綁定列。如何通過VBA將數據保存到組合框避免「數據類型轉換錯誤」?

如果我打開表格並鍵入與列表相關的文本,它不會導致任何錯誤。我想要做的是通過VBA填充組合框,但使用文本而不是整數。

例如,在第一個組合框中,用戶可以看到2個選項:「C」和「V」。綁定列中的「C」是1,「V」是2.我需要將字符發送到組合框並保存。當我嘗試輸入文本時,會發生「數據類型轉換錯誤」。在這種情況下我能做什麼?

在此先感謝。

代碼:

Public Sub SalvarAreaRangeNoBD(registro As DAO.Recordset, areaRange As Range) 
    Dim totalLin, l As Integer 
    Dim totalCol, c As Integer 
    Dim deletar As Boolean 

    totalLin = areaRange.Rows.Count 
    totalCol = areaRange.Columns.Count 
    deletar = True 

    Call mdlUtil.LimparRegistro(registro) 
    registro.AddNew 

    For l = 1 To totalLin 
     For c = 1 To totalCol 
      If ((areaRange.Cells(l, c) & "") = "") Then 
       registro.Fields(c - 1).Value = Empty 
      Else 
       registro.Fields(c - 1).Value = areaRange.Cells(l, c) 

       If (deletar <> False) Then 
        deletar = False 
       End If 
      End If 
     Next c 

     If (deletar) Then 
      registro.CancelUpdate 
     Else 
      registro.Update 
     End If 

     registro.AddNew 
     deletar = True 
    Next l 
End Sub 
+0

你能不能給我們您所使用的代碼? – INOPIAE

+0

@INOPIAE我嘗試在單獨的表格中編寫相同的文本,並遇到同樣的問題。訪問不允許寫入文本,而只允許綁定列中的等效整數。例如。而不是「C」,寫1.發生的是,這些數據來自Excel電子表格,我無法搜索相應的整數。 – MyThorRJ

+0

你在哪裏使用組合框?在表單上,​​在工作表上?如果你談論綁定列,你如何填充組合框? – INOPIAE

回答

0

打開表到你試圖寫入數據,並期待在現場與ComboBox查找。什麼是數據類型?

我幾乎可以保證它是一個NumberLong Integer

你遇到的問題是,組合框使它看起來就像你存儲的字符串值,但它真的存儲在ComboBox的第1列的整數值,但顯示人類可讀的字符串值在第2列。

你需要做的是查找整數值。如果ComboBox的記錄源綁定到另一個表,那就太容易了。如果它是value list,那有點煩人,但仍然完全可行。

下面是一個例子:

Public Sub SalvarAreaRangeNoBD(registro As DAO.Recordset, areaRange As Range) 
    Dim totalLin, l As Integer 
    Dim totalCol, c As Integer 
    Dim cellValue as Variant 

    'New code. Be sure to add a reference to Microsoft Scripting Runtime: 
    Dim simpleLookup as Scripting.Dictionary 

    set simpleLookup=getLookup("LookupTableName","DisplayFieldName","NumberFieldName") 

    totalLin = areaRange.Rows.Count 
    totalCol = areaRange.Columns.Count 

    Call mdlUtil.LimparRegistro(registro) 
    registro.AddNew 

    For l = 1 To totalLin 
    For c = 1 To totalCol 
     cellValue=areaRange.Cells(l, c) 
     'If ((cellValue & "") = "") Then 
     ' 'Do nothing 
     'Else 
     If simpleLookup.exists (cellValue) then 
      'Only add the row to the recordset if you're actually going to use it. 
      registro.AddNew 
      registro.Fields(c - 1).Value = simpleLookup(cellValue) 
      registro.Update 
     Else 
      'Do nothing 
     End If 
     'End If 
    Next c 
    Next l 
End Sub 

Public Function getLookup(tableName as String, displayField as String, recordField as String) as Scripting.Dictionary 
    dim rs as DAO.Recordset 
    Set getLookup = new Scripting.Dictionary 
    Set rs = CurrentDB.OpenRecordset(tableName) 
    With rs 
    Do Until .eof 
     getLookup.Add .fields(displayField).value, .fields(recordField) 
     .MoveNext 
    Loop 
    End With 
End Function 
+0

hi @C。白色。我看了一下你的代碼,我發現它不會解決問題,不幸的是。請查看原因並告訴我是否同意: 1 - 它必須是通用代碼(可重複使用)。 2 - 我的表可能(通常有)多一個組合框字段。 3 - 我的表格包含非鏈接字段以及組合框字段(例如價格,日期等)。 4 - 我需要將所有單元格保存到表格中,即使是重複數據的單元格也是如此。 「如果simpleLookup.exists(cellValue),那麼」不允許我這樣做。 我感謝你的承諾。 – MyThorRJ

+0

我的目標是將Excel電子表格中的範圍複製到Access表格中。儘管組合框字段與文本類型的範圍字段不同,但該範圍具有相同的列,並且數據類型相同。我的代碼對此很好。僅在我身邊的三個事實是Access不允許將文本數據直接粘貼到組合框字段中。 – MyThorRJ

+0

我試圖避免我最終採用的解決方案:刪除每個源表中的整數類型字段,並將文本類型字段設置爲主鍵。所以我可以將Excel單元格中的文本粘貼到表格字段中,因爲它們具有相同的數據類型。表中的組合框字段將保留,但不再是整數數據類型。代碼沒有改變,它的功能就像一個魅力! – MyThorRJ

相關問題