Help needed for DBEngine.Workspace(0)

sandrapoh

New member
Local time
Today, 09:55
Joined
Sep 4, 2013
Messages
8
Dear all Gurus,

I have written the attached program. Every time, the 1st time I ran the program, it is ok. But the 2nd time, I run the program, I always hit problem shown in the sheet "Error Hit" in the attached.

The statement that hit the problem is below in the [Public Function CreateTables].

Set daoWS = DBEngine.Workspaces(0)

I have the other functions or subroutine in the actual program files that needs to reference to "Microsoft Office 12.0 Access database engine Object Library".

I know I need to add "Microsoft DAO 3.6 Object Library". But when I tried to add this library together with "Microsoft Office 12.0 Access database engine Object Library", I will hit error in the 3rd picture in the sheet "VBA Reference".

Please advise what should I do next.

Regards
Sandra Poh
 

Attachments

First, recommend putting all this code into a new Excel Module - rather than a workbook code module. That is just me.

The CreateTables is calling a public Function Create Tables
Since it is a function and needs to return something, suggest it checks for an error and returns a Pass / Fail (boolean)
' Now, pass in the Workbook name "source" this will allow "Summary" to be in parens as a function requires.

Dim AResult As BooleanThe
AResult = CreateTables(ThisWorkbook.Worksheets("Source"), "Summary")


Public Function CreateTables(shtLayOut As String, strTableName As String) As Boolean ' just test for pass fail with out error

Also added Option Explicit to the top of the Module (or the Require Variable Declaration option for code)
the shtlayout.cells(...) is not defined.
Exiting code with out error trapping to destroy object variables can leave an orphan object. This can give phantom results when running the 1st, 2nd time in a debug situation.
Before running your code, go to Task Manager and End Process for all Access and Excel applications.
Then open your Excel with the code. Keep task manager open as you step through the code. If a 2nd Excel or 2nd Access application opens, this means that a 2nd object was instantiated.
Be sure to use the Immediate window at different points to verify your variables.


Digest this information and get back with more questions. Be sure to upload the working code afterwards. Perhaps a few tips can be offered.
 
Made some minor changes, hard coded some variables not designed. Removed the object code not yet declared.
This will get through the basic concept of Creating a new DB, creating and appending a generic table.

Code:
Option Explicit
Public accAP As Access.Application, accDB As Database, accDF As TableDefs
Public daoDE As DAO.DBEngine, daoWS As DAO.Workspace, daoDB As DAO.Database, daoRS As DAO.Recordset, daoTB As DAO.TableDef, daoFD As DAO.Field
Public daoID As DAO.Index, daoIF As DAO.Field

Public shtFMT As Worksheet, lngFMTRow As Long, lngFMTCol As Long, strFMT As String
Public shtMAP As Worksheet, lngMAPRow As Long, lngMAPCol As Long, strMAP As String
Public shtSRC As Worksheet
Public lngSRCRow As Long, lngSRCRowF As Long, lngSRCRowT As Long, lngSRCCol As Long, strSRCCol As String, lngSRCColT As Long
Public wkbNew As Workbook, shtNew As Worksheet, lngNewFile As Long

Public lngLayOutRow As Long, strFieldName As String, varFieldLength As Variant
Public strPath As String, strActiveCell As String
Public strVerN As String, strVersion As String, strValueType As String
Public strMTHS As String, strMTHF As String, strMTHT As String, lngMTHS As Long
Public lngREC1 As Long, lngREC2 As Long
Public strCom1 As String, strCom2 As String, strCom3 As String, strCom4 As String
Public strCom5 As String, strCom6 As String, strCom7 As String, strCom8 As String
Public strTmp1 As String, strTmp2 As String, strTmp3 As String, strTmp4 As String
Public strTmp5 As String, strTmp6 As String, strTmp7 As String, strTmp8 As String
Public strApro As String, strComF As String
Public strFST1 As String, strFST2 As String, strFST3 As String
Public strLST1 As String, strLST2 As String, strLST3 As String
Public varAMT  As Variant
Public Sub CreateDB()
    
    Application.ScreenUpdating = False
    strPath = ThisWorkbook.Path & "\"
    If Dir(strPath & "UploadNew.accdb", vbNormal) <> "" Then
        Kill (strPath & "UploadNew.accdb")
    End If

    Set accAP = Nothing
    Set accDB = Nothing
    Set accAP = New Access.Application
    Set accDB = accAP.DBEngine.CreateDatabase(strPath & "UploadNew.accdb", dbLangGeneral)
    accAP.Visible = True
        Debug.Print ThisWorkbook.Path & "\" & "uploadNew.accdb"
    accDB.Close
    accAP.Quit
    Set accAP = Nothing
    Set accDB = Nothing
    Dim AResult As Boolean
    AResult = CreateTables("Source", "Summary")
    Application.ScreenUpdating = True

End Sub


Public Function CreateTables(shtLayOut As String, strTableName As String) As Boolean ' just test for pass fail with out error

    Application.EnableLargeOperationAlert = True
    Application.DisplayAlerts = False
    Application.ScreenUpdating = False
    strPath = ThisWorkbook.Path & "\"
    
    Set daoWS = Nothing
    Set daoDB = Nothing
    Set daoTB = Nothing
    
    Set daoWS = DBEngine.Workspaces(0)
    Set daoDB = daoWS.OpenDatabase(strPath & "UploadNew.accdb") ' strDatabase is the full path to the accdb or mdb
    Set daoTB = daoDB.CreateTableDef(strTableName) ' you will need to declare daoTB As DAO.TableDef at the top of your procedure

    lngLayOutRow = 2
    'Do While Len(shtLayOut.Cells(lngLayOutRow, 2))
        
        'strFieldName = shtLayOut.Cells(lngLayOutRow, 6)
        'varFieldLength = shtLayOut.Cells(lngLayOutRow, 3)
        
        'If shtLayOut.Cells(lngLayOutRow, 2) = "Text" Then
            Set daoFD = daoTB.CreateField("MyTest", dbText, 10) ' fix variable strfieldname - put in a hardcoded value for now
            daoFD.Required = False
            daoFD.AllowZeroLength = True
        'Else
            'If shtLayOut.Cells(lngLayOutRow, 2) = "Number" And shtLayOut.Cells(lngLayOutRow, 3) = "Double" Then
                'Set daoFD = daoTB.CreateField(strFieldName, dbDouble)
                'daoFD.Required = False
                'daoFD.DefaultValue = 0
            'Else
                'If shtLayOut.Cells(lngLayOutRow, 2) = "Number" And shtLayOut.Cells(lngLayOutRow, 3) = "Long Integer" Then
                    'Set daoFD = daoTB.CreateField(strFieldName, dbLong)
                    'daoFD.Required = False
                    'daoFD.DefaultValue = 0
                'End If
            'End If
        'End If
        
        
        ' now a long integer but most others are the same
        'strFieldName = "Long_Integer_Field"
        'Set daoFD = daoTB.CreateField(strFieldName, dbLong)
        ' or you can use dbInteger, dbSingle, dbDate, dbCurrency, dbBoolean, dbBinary dbMemo or, dbLongBinary (did I miss any?)

        ' after each field you need to append it to the table def
        daoTB.Fields.Append daoFD
    
        ' text is a little different, you need to specify the length
        'Set daoFD = daoTB.CreateField(strFieldName, dbText, intFieldLength)
        'daoTB.Fields.Append daoFD
        
        ' once you've added all the fields then we need to append the table to the tables collection
        
        lngLayOutRow = lngLayOutRow + 1
    'Loop
    daoDB.TableDefs.Append daoTB

    daoDB.Close
    daoWS.Close
'There is a table and all the fields now for the indexes - here are some samples
' PrimaryKey"
 'Set daoID = daoTB.CreateIndex("PrimaryKey")
 'Set daoIF = daoID.CreateField(strFieldName)
 'daoID.Primary = True
 'daoID.Fields.Append daoIF
 'daoTB.Indexes.Append daoID
' Unique"
 'Set daoID = daoTB.CreateIndex(strIndexName)
 'Set daoIF = daoID.CreateField(strFieldName)
 'daoID.Unique = True
 'daoID.Fields.Append daoIF
 'daoTB.Indexes.Append daoID
' NonUnique"
 'Set daoID = daoTB.CreateIndex(strIndexName)
 'Set daoIF = daoID.CreateField(strFieldName)
 'daoID.Fields.Append daoIF
 'daoTB.Indexes.Append daoID
Application.ScreenUpdating = True

End Function
 
Dear RX,
In my Excel VBA program, sometimes, I need to use "Microsoft DAO 3.6 Object Library".
Sometimes I need to use "Microsoft Office 12.0 Access Database Engine Object Library".

But both cannot exist at the same time, how to define in Excel VBA.
I tried to write "Public Function DAOToACE" and "Public Function ACEToDAO".
When my program needs to refer to "Microsoft DAO 3.6 Object Library", it will remove the
library ""Microsoft Office 12.0 Access Database Engine Object Library" and add in
"Microsoft DAO 3.6 Object Library".

But it will cause my program error that the variable "accDB As Database" as User-Defined Type not valid.

Please review my program and advise.

Regards
Sandra
 

Attachments

Users who are viewing this thread

Back
Top Bottom