Avatar of Sandra Smith
Sandra Smith
Flag for United States of America asked on

Create an ACCESS table from Excel

I seem to be going in circles.  I get some to work and then it doesn't.  The below partially works - however, the code between the asterisk lines does not.  I need to create the table in the databased from Excel, but it does not recognie my appACCESS.

Sandra

Public Sub OpenKeyMetricsForm()
'Opens Key Metrics form for data entry into the database
On Error Resume Next
Dim strPath As String
Dim appAccess As Access.Application
Dim tdfTable As DAO.TableDef
Dim dteDate As Date
Dim gstrUserId As String
Dim intDeptID As Long

intDeptID = ThisWorkbook.Worksheets("MainMenu").Range("DeptID")
gstrUserId = VBUserName()
dteDate = BOM(Date) - 1

strPath = ThisWorkbook.Worksheets("MainMenu").Range("B1")

    Set appAccess = CreateObject("Access.Application")
   ' Open the Employees table in the Northwind database
   appAccess.OpenCurrentDatabase strPath
   
'*********************************************************************************************************************
 'Create table in database

Set tdfTable = appAccess.CreateTableDef("tblTEMPKMQuantities")
With tdfTable
    .Fields.Append .CreateField("KMID", dbText)
    .Fields.Append .CreateField("Quantity", dbLong)
    .Fields.Append .CreateField("Description", dbText)
    .Fields.Append .CreateField("KMDate", dbDate)
    .Fields.Append .CreateField("UserID", dbText)
    CurrentDb.TableDefs.Append tdfTable
End With

'Insert KM information
DoCmd.RunSQL "INSERT INTO tblTEMPKMQuantities ( KMID, Description, KMDate, UserID)" & _
    "SELECT tblKM.KMID, tblKM.Description, #" & dteDate & "#, '" & gstrUserId & "' " & _
    "FROM tblKM WHERE tblKM.Active = -1 AND tblKM.DeptID = " & DeptID
'*********************************************************************************************************************
   
   ' Open KM form
    ' appAccess.DoCmd.OpenForm "frmKeyMetricsQtys", acViewNormal
     appAccess.DoCmd.Close acForm, "frmTimeSheet"
     appAccess.Visible = True
     appAccess.DoCmd.OpenForm "frmKeyMetricsQty", acViewNormal

End Sub
Microsoft DevelopmentMicrosoft ApplicationsMicrosoft Excel

Avatar of undefined
Last Comment
Sandra Smith

8/22/2022 - Mon
ukerandi

You need to open Microsoft access 12.0 object library in Refrences(tools menue)
ASKER CERTIFIED SOLUTION
ukerandi

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ukerandi

ukerandi

'Create table in database
Set Db = CurrentDb
Set tdfTable = Db.CreateTableDef("tblTEMPKMQuantities")
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
ukerandi

OR

Dim ws as Workspace
Dim db as Database

Set ws=DBEngine.Workspaces(0)
set db=ws.OpenDatabase({databasepath and name})


Set tdfTable = Db.CreateTableDef("tblTEMPKMQuantities")
Sandra Smith

ASKER
Totally overlooked the reference.  I have not worked in Excel for quite some time so did not think about the library reference.  Thank you for the same and suggestion.  there will be more questions as i work on this project as I have forgotten much of my Excel VBA relative to working with ACCESS.  

Sandra