任何帮助将不胜感激。我试图让这段代码工作。我需要获取临时表的字段名称,并检查它们是否存在使用另一个永久表。我的问题是,抓住变量,我在哪里实际放入字段名称进行检查。 ?如果我不打算添加字段名称,那么代码会为我做吗?我如何调用该函数才能执行此操作。函数接受和称为strfield的参数,令我困惑的是get names变量是arrStrings。他们不应该匹配吗?
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)
你应该可以这样做:
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
不确定你的最终目标是什么,但这应该会给你一个正确的方向。如果你真的想知道两个表中是否存在每个单独的字段,那么像以前一样的数组是一个好主意。
答案 1 :(得分: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