Peter Chan
asked on
Issue to code
Hi,
Further to code below, how to clear one table within the Access DB opened?
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
ASKER
Hi,
Please help as last line is not being supported in VBA.
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
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
...
Or perhaps DoCmd.RunSQL...
With appAccess
.DoCmd.SetWarnings False
.DoCmd.RunSQL "DELETE FROM [YourTableName]"
.DoCmd.SetWarnings True
End With
...
ASKER
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:
Choose meaningfull names for variables.
Your are not good.
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
Side note:Choose meaningfull names for variables.
Your are not good.
ASKER
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.
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.
ASKER
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;")
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
If something similar doesn't work for you, it is because the records cannot be deleted (i.e. referential integrity)
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;")
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
ASKER
How to make it work with the select statement?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
RunSQL will run any action query.
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
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.
You can bing/google for a bunch of examples.
ASKER
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
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.
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
As a query
SELECT * FROM [C:\Test\Test.accdb].tblTest;
or in codeSet rs = CurrentDb.OpenRecordset("SELECT * FROM [C:\Test\Test.accdb].tblTest;")
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
Open in new window