2017-05-25 68 views
0

任何幫助將不勝感激。我試圖讓這個代碼工作。我需要獲取臨時表的字段名稱,並檢查它們是否存在使用另一個永久表。我的問題是,逮捕變量,我在哪裏實際放在字段名稱來檢查。 ?如果我不打算添加字段名稱,代碼會爲我執行嗎?我該如何調用函數才能做到這一點。該函數接受和參數稱爲strfield和什麼令我困惑的是get名稱變量是arrStrings。他們不應該匹配嗎?VBA-獲取字段名稱並檢查它們是否存在臨時表中

Sub Example() 
Dim objRecordset As ADODB.Recordset 
Dim arrStrings(1 To 100) As String 
Dim intIndex As Integer 
Dim i As Integer 

Set objRecordset = New ADODB.Recordset 
objRecordset.ActiveConnection = CurrentProject.Connection 
objRecordset.Open ("MyTable1") 
intIndex = 1 
'loop through table fields 
For i = 0 To objRecordset.Fields.Count - 1 
arrStrings(intIndex) = objRecordset.Fields.Item(i).Name 
  
intIndex = intIndex + 1 
  
Next i 
End Sub 


' this is the function that checks the exists 

Function CheckExists(ByVal strField As String) As Boolean 
Dim objRecordset As ADODB.Recordset 
Dim i As Integer 

Set objRecordset = New ADODB.Recordset 
objRecordset.ActiveConnection = CurrentProject.Connection 
objRecordset.Open ("MyTable2") 
'loop through table fields 
For i = 0 To objRecordset.Fields.Count - 1 
'check for a match 
  
If strField = objRecordset.Fields.Item(i).Name Then 
  
'exit function and return true 
  
CheckExists = True 
  
Exit Function 
  
End If 
  
Next i 
'return false 
CheckExists = False 
End Function 
+0

哪裏代碼?而且這兩個查詢似乎都使用了同一張表?兩種方法之間似乎存在「額外」的代碼 - 是真的還是錯誤的? –

+0

我編輯它,並將函數中的表格更改爲mytable2。第二部分是功能檢查哦,我看到它我會刪除。 TY! –

回答

0

你應該能夠做這樣的事情:

Option Explicit 


Sub Example() 
    Dim objRecordset As ADODB.Recordset 
    Dim intNumExist As Integer 
    Dim i As Integer 

    Set objRecordset = New ADODB.Recordset 
    objRecordset.ActiveConnection = CurrentProject.Connection 
    objRecordset.Open ("MyTempTable") 

    'loop through table fields, see if they exist in other table 
    For i = 0 To objRecordset.Fields.Count - 1 
     intNumExist = intNumExist + CheckExists(objRecordset.Fields.Item(i).Name) 
    Next i 

    Debug.Print intNumExist & "fields exist in both tables" 

End Sub 

Function CheckExists(ByVal strField As String) As Integer 
    Dim objRecordset As ADODB.Recordset 
    Dim i As Integer 

    Set objRecordset = New ADODB.Recordset 
    objRecordset.ActiveConnection = CurrentProject.Connection 
    objRecordset.Open ("MyPermTable") 
    'loop through table fields 
    For i = 0 To objRecordset.Fields.Count - 1 
     'check for a match ? 
     If strField = objRecordset.Fields.Item(i).Name Then 
      'exit function and return true (1) 
      CheckExists = True 
      Exit Function 
     End If 
    Next i 
    'return false 
    CheckExists = False 

    CheckExists = Abs(CheckExists) 
End Function 

不知道你的最終目標是什麼,但是這應該給你一個正確的方向點。如果你真的想看看每個單獨的字段是否存在於兩個表中,那麼像你之前的數組是一個好主意。

+0

謝謝!它用於驗證連接到Sharepoint的Sharepoint列表表中的數據...我需要知道用戶在完全導入數據之前是否刪除了列。數據是從Excel中使用停靠的傳輸電子表格進來的,我無法弄清楚如何在Excel和Access之間進行驗證,而無需設置詳細的表格模式。我現在就試試看,並讓你知道它是怎麼回事......謝謝,我希望它能起作用!看起來不錯。 –

+0

但我該如何打電話給你。我不擅長布爾調用,我不知道如何調用它。你能幫忙嗎? –

+0

所以我在第一部分將其稱爲。它現在設置的方式,它會一次檢查一個字段。所以你可以做這樣的事情(對不起,我猜不可以在註釋中使用代碼格式):If CheckExists(objRecordset.Fields.Item(i).Name)= 1 Then ....或者如果CheckExists(「fieldName」 )= 1然後....我把它設置爲1或0,以防你想知道7個字段中有6個匹配,等等。你可以改變函數返回一個布爾值而不是一個整數,並且得到True /假。如果你願意,我可以粘貼代碼。 – Hauffa

0

我傾向於返回字典中其他表中的所有字段,因此避免了在循環第一個記錄集時重複查詢。

未經測試:

Sub Example() 

    Dim objRecordset As ADODB.Recordset 
    Dim arrStrings(1 To 100) As String 
    Dim intIndex As Integer 
    Dim f As Field, dict As Scripting.Dictionary 

    Set objRecordset = New ADODB.Recordset 
    objRecordset.ActiveConnection = CurrentProject.Connection 
    objRecordset.Open "MyTable1" 

    'get the list of fields for the other table 
    Set dict = GetTableFields("MyOtherTable") 

    For Each f In objRecordset.Fields 
     If dict.Exists(f.Name) Then 
      'have matching field in MyOtherTable 
     Else 
      'no matching field in MyOtherTable 
     End If 
    Next f 

End Sub 

'return all the field names for the provided table name 
Function GetTableFields(ByVal sTable As String) As Scripting.Dictionary 
    Dim objRecordset As ADODB.Recordset 
    'add reference to "Microsoft Scripting Runtime" 
    Dim dict As New Scripting.Dictionary 
    Dim f As Field 

    Set objRecordset = New ADODB.Recordset 
    objRecordset.ActiveConnection = CurrentProject.Connection 
    'you don't need any records: just the fields 
    objRecordset.Open "select * from " & sTable & " where false" 
    'collect all the field names... 
    For Each f In objRecordset.Fields 
     dict.Add f.Name 
    Next f 
    Set GetTableFields = dict 'return the dictionary (note "Set"...) 
End Function 
+0

謝謝!我沒有想到這一點。我現在也會測試你的。在你的代碼,因爲它不是布爾值,我怎麼稱呼它..我很抱歉,我的弱點是調用函數。你可以解釋嗎? –

+0

該調用顯示在'Example'子項中。 –

+0

啊哈! :)設置dict = GetTableFields(「MyOtherTable」)非常有趣。我不知道你可以調用一個函數,同時將它設置爲一個對象。 –

相關問題