Avatar of Peter Chan
Peter Chan
Flag for Hong Kong asked on

Issue to code

Hi,
Further to code below, how to clear one table within the Access DB opened?

Dim appAccess As Object


'create new access object
Set appAccess = CreateObject("Access.Application")
'open the acces project
Call appAccess.OpenCurrentDatabase( _
"D:StuffBusinessTempNewDB.accdb")
appAccess.Visible = True



Open in new window

VBAMicrosoft Access

Avatar of undefined
Last Comment
ste5an

8/22/2022 - Mon
Daniel Pineault

Your should be able to do something along the lines of (untested aircode):
Dim sSQL As String
sSQL = "DELETE FROM [YourTableName]"
CurrentDb.Execute sSQL, dbFailOnError

Open in new window


Don't forget to add proper error handling.

Or when you say 'Access DB opened' do you mean AppAccess?  If so, what about
Dim sSQL As String
sSQL = "DELETE FROM [YourTableName]"
AppAccess.Execute sSQL, dbFailOnError

Open in new window


Peter Chan

ASKER
Hi,
Please help as last line is not being supported in VBA.

    Dim Adb As Object: Set Adb = CreateObject("Access.Application")
    Call Adb.OpenCurrentDatabase(F1)
    Adb.Visible = True
   
    s0 = "delete * from t1"
    Adb.Execute s0, dbfailonerror



Open in new window

Daniel Pineault

What about creating a deletion query in the db and then running it

...
    With appAccess
        .DoCmd.SetWarnings False
        .DoCmd.OpenQuery "YourDeletionQueryName"
        .DoCmd.SetWarnings True
    End With
...

Open in new window

Or perhaps DoCmd.RunSQL
...
    With appAccess
        .DoCmd.SetWarnings False
        .DoCmd.RunSQL "DELETE FROM [YourTableName]"
        .DoCmd.SetWarnings True
    End With
...

Open in new window


All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Peter Chan

ASKER
Daniel,
Can you help to error like
due to OpenQuery line below?
    Dim Adb As Object: Set Adb = CreateObject("Access.Application")
    Call Adb.OpenCurrentDatabase(F1)
    Adb.Visible = True
   
    s0 = "delete * from t1"
   
    With Adb
        'DoCmd.SetWarnings False
        .DoCmd.OpenQuery s0
        'DoCmd.SetWarnings True
    End With

Open in new window

Daniel Pineault

OpenQuery runs an existing query that you supply the name of, you can't run an SQL Statement for that you need to use DoCmd.RunSQL.
Fabrice Lambert

In other words:
    Dim Adb As Object
    Set Adb = CreateObject("Access.Application")
    Call Adb.OpenCurrentDatabase(F1)
    Adb.Visible = True
   
    Dim s0 As String
    s0 = "delete * from t1;"
   
    With Adb
        .DoCmd.SetWarnings False
        .DoCmd.RunSQL s0
        .DoCmd.SetWarnings True
    End With

Open in new window

Side note:
Choose meaningfull names for variables.
Your are not good.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Peter Chan

ASKER
Thanks.
Can you help to error
due to RunSQL line below?
    Dim Adb As Object: Set Adb = CreateObject("Access.Application")
    Call Adb.OpenCurrentDatabase(F1)
    Adb.Visible = False
   
    s0 = "select * from t1;"
   
    With Adb
        'DoCmd.SetWarnings False
        .DoCmd.RunSQL s0
        'DoCmd.SetWarnings True
    End With



Open in new window

Fabrice Lambert

The DoCmd.RunSQL instruction require an INSERT, UPDATE or DELETE SQL statement.
It does not run with SELECT statement.

To display the result of  a SELECT query, you need to create a query, and use the DoCmd.OpenQuery instruction.
alternately, to display the content of a table, you can use the DoCmd.OpenTable instruction.
Peter Chan

ASKER
How work with recordset, having select statement, in VBA?
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Fabrice Lambert

Dim Db As DAO.Database
Set Db = CurrentDb

Dim Rs As DAO.Recordset
Set Rs = Db.OpenRecordset("SELECT * FROM t1;")

Open in new window

Recordsets are rich objects.
Check microsoft's documentation
Gustav Brock

This works here:

Public Function DeleteRecords()

    Dim appAccess   As Object
    
    ' Create new Access object.
    Set appAccess = CreateObject("Access.Application")

    ' Open the Access project.
    Call appAccess.OpenCurrentDatabase( _
        "C:\Test\Test.accdb")

    appAccess.Visible = True
    
    ' Delete records.
    appAccess.DoCmd.RunSql "Delete * From tblTest"

End Function

Open in new window

If something similar doesn't work for you, it is because the records cannot be deleted (i.e. referential integrity)
Peter Chan

ASKER
Hi,
Please help to error
due to line below.
    Dim rs As dao.Recordset

Open in new window


⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Gustav Brock

That requires a reference to Microsoft Office 16.0 Access database engine Object library:

Ref.PNG
Peter Chan

ASKER
Thanks. Why is there "Type mismatch" error to last 2nd line below?
    Dim Adb As Object: Set Adb = CreateObject("Access.Application")
    Call Adb.OpenCurrentDatabase(F1)
    Adb.Visible = False
   
    Dim db As DAO.Database
    Dim rs As DAO.Recordset
   
    Set db = Adb
    Set rs = db.openrecordset("select * from t1;")



Open in new window

Gustav Brock

Because your Adb is an application, while db is a database.
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
Gustav Brock

It goes like this:

Public Function DeleteDao()

    Dim db  As DAO.Database
    
    Set db = DBEngine(0).OpenDatabase("C:\Test\Test.accdb")
    db.Execute "Delete * From tblTest"
    
    db.Close
    
End Function

Open in new window

Peter Chan

ASKER
How to make it work with the select statement?
ASKER CERTIFIED SOLUTION
Gustav Brock

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Gustav Brock

RunSQL will run any action query.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Peter Chan

ASKER
Thanks a lot.
How to resolve

due to RunSQL line below?
    s0 = "select * into " & ActiveSheet.Name & " from t1;"
   
    With Adb
        'DoCmd.SetWarnings False
        .docmd.RunSQL s0
        'DoCmd.SetWarnings True
    End With



Gustav Brock

From within Excel, you would typically use CopyFromRecordset for that.

You can bing/google for a bunch of examples.
Peter Chan

ASKER
How about that I need to duplicate existing table (within Access Database), to another table?
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Fabrice Lambert

CREATE TABLE query.
More infos here: SQL CREATE TABLE - SQL 
Gustav Brock

How about that I need to duplicate existing table (within Access Database), to another table?

Now, this is the fourth time you change the topic. Please stick to one question, get it solved, then open a new question.
Otherwise, it ends up in a mess, where no one can figure out who is answering what.
Fabrice Lambert

I think you should take some VBA and SQL lessons.
It will be much more complete than asking random questions here.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
ste5an

Using DBEngine is not mandatory. Simple scenarios can be handled be Jet/ACE itself.

As a query

SELECT * FROM [C:\Test\Test.accdb].tblTest;

Open in new window

or in code

Set rs = CurrentDb.OpenRecordset("SELECT * FROM [C:\Test\Test.accdb].tblTest;")

Open in new window