2017-05-25 70 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”)非常有趣。我不知道你可以调用一个函数,同时将它设置为一个对象。 –

相关问题