Link to home
Start Free TrialLog in
Avatar of Peter Chan
Peter ChanFlag 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

Avatar of Daniel Pineault
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


Avatar of 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

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


Daniel,
Can you help to error like
User generated imagedue 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

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.
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.
Thanks.
Can you help to error
User generated imagedue 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

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.
How work with recordset, having select statement, in VBA?
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
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)
Hi,
Please help to error
User generated imagedue to line below.
    Dim rs As dao.Recordset

Open in new window


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

User generated image
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

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

How to make it work with the select statement?
ASKER CERTIFIED SOLUTION
Avatar of Gustav Brock
Gustav Brock
Flag of Denmark image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
RunSQL will run any action query.
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



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

You can bing/google for a bunch of examples.
How about that I need to duplicate existing table (within Access Database), to another table?
CREATE TABLE query.
More infos here: SQL CREATE TABLE - SQL 
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.
I think you should take some VBA and SQL lessons.
It will be much more complete than asking random questions here.
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