When I first looked at your post, for some reason the only thing that displayed at my end was "From Chatgpt" but not the code? I will test that now..................
Works great using your post:
Code:
Option Compare Database
Option Explicit
Public Sub MySub()
' Set variables
Dim strTableName As String
Dim strFileName As String
Dim dummy As Variant
Dim blnHasHeadings As Boolean
'Set data
strTableName = "tblMenuSheet"
strFileName = "c:\SummersetApp\WeeklyMenu.xlsx"
blnHasHeadings = True
' Import data
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12, strTableName, strFileName ', blnHasHeadings
'CurrentDb.Execute "ALTER TABLE tblMenuSheet ADD Column MenuID;"
dummy = fcnMakePrimaryKey("tblMenuSheet", "MenuID")
MsgBox "Excel file imported!", vbInformation
End Sub
Public Function fcnMakePrimaryKey(tName As String, kName As String)
On Error GoTo fcnMakePrimaryKey_Error
Dim sSQL As String
sSQL = "ALTER TABLE [" & tName & "] ADD COLUMN " & kName & " COUNTER"
CurrentDb.Execute sSQL, dbFailOnError
Application.CurrentDb.TableDefs.Refresh
sSQL = "CREATE INDEX [$" & tName & "$] ON [" & tName & "] ([" & kName & "]) With Primary"
DoEvents
'Debug.Print sSQL
CurrentDb.Execute sSQL, dbFailOnError
fcnMakePrimaryKey_EXIT:
Exit Function
fcnMakePrimaryKey_Error:
Select Case Err
Case Else
MsgBox "Errors encountered" & Err.Number & " Desc " & Err.Description
End Select
Resume fcnMakePrimaryKey_EXIT
End Function