Modifying a linked Excel table from Access 365 (1 Viewer)

Danick

Registered User.
Local time
Today, 10:48
Joined
Sep 23, 2008
Messages
351
After getting that error a few times, decided to try and find a way to fix it. Found this code that will check the table for the fields. So now I'm running this code on open and then either letting it run if the field is there, or back to arne's code to fix the table. No more error messages.
Need Microsoft ActiveX Data Object Library Reference to get it work.

Code:
' credits to https://software-solutions-online.com/check-if-field-exists-vba-access/

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 ("MyTable1")
'loop through table fields
For i = 0 To objRecordset.Fields.Count - 1
    'check for a match
    If strField = objRecordset.Fields.Item(i).Name Then
        'exist function and return true
        CheckExists = True
        Exit Function
    End If
Next i
'return false
CheckExists = False
End Function

' It can be tested using this code
Sub test()
If CheckExists("MyField1") = True Then
    MsgBox ("Field exists")
Else
    MsgBox ("Field does not exist")
End If
End Sub
 

Users who are viewing this thread

Top Bottom