Results 1 to 9 of 9
  1. #1
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 10 Access 2013 32bit
    Join Date
    Feb 2011
    Posts
    2,097

    Need code to create table from Excel file

    I see Ranman256 post https://www.accessforums.net/showthread.php?t=54561 dealing with importing parts of an Excel file, but I just need the applicable code to essentially do what I would do in design mode starting with getting the Excel file option with "External Data" > "Import & Link" > "New Data Source" > "From File" > "Excel". Anyone have a simple code example where everything about the created table defaults from what is found in the Excel file?

  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,556
    I have started asking ChatGPT instead of googling recently.

    Here is what another user was using.
    https://www.accessforums.net/showthread.php?t=90726
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  3. #3
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 10 Access 2013 32bit
    Join Date
    Feb 2011
    Posts
    2,097
    Searching on your suggestion, and finding this code example, the code gets the job done.
    Code:
    Option Compare Database
    Option Explicit
    Public Function MyFunc()
    
    
     ' Set variables
     Dim strTableName As String
     Dim strFileName As String
     Dim blnHasHeadings As Boolean
    
    
     'Set data
     strTableName = "tblMenuSheet"
     strFileName = "c:\SummersetApp\WeeklyMenu.xlsx"
     blnHasHeadings = True
     ' Import data
     DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12, strTableName, strFileName       ', blnHasHeadings
     MsgBox "Excel file imported!", vbInformation
    
    
    End Function
    The only thing missing that I need in the new table is an added "ID" autonumber field.

  4. #4
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,556
    Well as you already appear to have a table, why not just add one?
    I never use a Function unless I wish to return a value.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  5. #5
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 10 Access 2013 32bit
    Join Date
    Feb 2011
    Posts
    2,097
    I never use a Function unless I wish to return a value.
    I was only trying to get a code segment to do the job. Final implementation will likely just be inline during the larger scope of functionality.

    I added a statement to add a column:
    Code:
     CurrentDb.Execute "ALTER TABLE tblMenuSheet ADD Column MenuID;"
    I get 3292 error: "Syntax error in field definition". I've done this sort of things in the past without errors, so I have no idea what the problem is? BTW, whats the data type expression for autonumber?

    In the larger view of this new app, a new Excel file gets created each week. The app will NOT append, but rather delete the previous week's table and create anew. The add column therefore has to be repeated in code each week.

  6. #6
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,556
    No idea.
    Why the need to do it in code and not manually?
    Was the table created by the transfer?

    From chatgpt
    Code:
    Sub AddAutoNumberField()
        Dim db As Database
        Dim tdf As TableDef
        Dim fld As Field
        
        ' Set the current database
        Set db = CurrentDb
        
        ' Reference the table you want to add the AutoNumber field to
        Set tdf = db.TableDefs("YourTableName") ' Replace with your table's name
        
        ' Add the AutoNumber field to the table
        Set fld = tdf.CreateField("AutoID", dbLong) ' AutoID is the name of the new field
        fld.Attributes = dbAutoIncrField ' This sets the field to AutoNumber
        
        ' Append the new field to the table
        tdf.Fields.Append fld
        
        ' Save and close
        db.TableDefs.Refresh
        MsgBox "AutoNumber field created successfully!", vbInformation
    End Sub
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  7. #7
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 10 Access 2013 32bit
    Join Date
    Feb 2011
    Posts
    2,097
    Yes. The end user doesn't have any knowledge or access to the tabledefs in design mode. The entire process of establishing the needed table has to be entirely automatic. And yes, the import works perfectly, I just need an autonumber field added to the table.

  8. #8
    davegri's Avatar
    davegri is offline Excess Access
    Windows 11 Access 2021
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,740
    I've had success with this. An added autonumber is not automatically made the primary key. You need more code to do that.
    Code:
    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 "Error " & Err.Number & " (" & Err.Description & ")", vbExclamation, "Error in Function fcnMakePrimaryKey"
        End Select
        Resume fcnMakePrimaryKey_EXIT
    End Function
    Last edited by davegri; 03-23-2025 at 05:12 PM. Reason: Changed msgbox in error handler

  9. #9
    GraeagleBill's Avatar
    GraeagleBill is offline Experienced Old Geezer
    Windows 10 Access 2013 32bit
    Join Date
    Feb 2011
    Posts
    2,097
    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

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 3
    Last Post: 08-07-2019, 02:57 PM
  2. Replies: 6
    Last Post: 12-18-2017, 07:12 PM
  3. Replies: 34
    Last Post: 08-09-2017, 09:57 AM
  4. Replies: 5
    Last Post: 05-25-2016, 12:43 PM
  5. Create Excel File Through File Browser
    By kdbailey in forum Access
    Replies: 6
    Last Post: 04-21-2016, 10:56 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums