任何幫助將不勝感激。我試圖讓這個代碼工作。我需要獲取臨時表的字段名稱,並檢查它們是否存在使用另一個永久表。我的問題是,逮捕變量,我在哪裏實際放在字段名稱來檢查。 ?如果我不打算添加字段名稱,代碼會爲我執行嗎?我該如何調用函數才能做到這一點。該函數接受和參數稱爲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
哪裏代碼?而且這兩個查詢似乎都使用了同一張表?兩種方法之間似乎存在「額外」的代碼 - 是真的還是錯誤的? –
我編輯它,並將函數中的表格更改爲mytable2。第二部分是功能檢查哦,我看到它我會刪除。 TY! –