I am creating a simple database. I have a functionality where i have simply created a Insert Into query an saved it in the database. what i want to do is,
call that query in VBA code.
I have done following coding :
Private Sub Update_ISO_Review_Register()
Dim dbs As DAO.Database
Set dbs = CurrentDb
dbs.Execute "Update_ISO_Review_Register_ApplicationData"
dbs.Close
Set dbs = Nothing
End Sub
The code works and it executes the query. Issue is that after that it locks the database and it gives the following error.
You do not have exclusive access to the database at this time. if you
proceed to make changes, you may not be able to save them later.
The SQL for the Update_ISO_Review_Register_ApplicationData query is:
INSERT INTO ISO_REVIEW_REGISTER ( SLTF_Ref, Brand, Application_No )
SELECT DISTINCT b.matter_No AS SLTF_Ref
, b.Brand
, b.CREDIT_APPLICATION_ID AS Application_No
FROM WBC_HFM_FileReveiw_Table AS a INNER JOIN WBC_HFM_Application_Table AS b ON
a.CREDIT_APPLICATION_ID = b.CREDIT_APPLICATION_ID;
Any help ?
Would it work if you only do this:
Private Sub Update_ISO_Review_Register()
CurrentDb.Execute "Update_ISO_Review_Register_ApplicationData"
End Sub
You can nicely live without these:
Dim dbs As DAO.Database
Set dbs = CurrentDb
dbs.Close
Set dbs = Nothing
especially set set dbs = Nothing, which is a local variable and "dies" after End Sub.
Related
I have a strange problem: I do an insert into command using as usual example:
Dim dbs As DAO.Database
Dim sql As String
Set dbs = CurrentDb
sql = "INSERT INTO dbo_tblScheMod (Stato, Collezione, Modello, Articolo) VALUES ('x','x','x','x');"
dbs.Execute sql
I get error:
Could not find output table dbo_tblScheMod.(Error 3192)
The table dbo_tblScheMod exists and visible in the Access environment in the Tables section.
Do you have any ideas?
I want to use Excel VBA to query an MS Access database, searching and adding to a table.
By searching this site I found this code:
Dim dbs As DAO.Database
Dim dbs As Object
Set dbs = OpenDatabase("FILEPATH\database1.accdb")
dbs.Execute "INSERT INTO Table1(ID, Field1, Field2, Field3) VALUES('1','Hello','Helo','Hi')"
which doesn't work because it says that the sub or function was not defined for the function OpenDatabase()
Has anyone got a better method of this
The reason I want to do this when I know there are better methods out there is because the database and spreadsheet already exist (run by my employer) and I just want to link them.
Try this (with a correct FILEPATH, of course):
Dim appAccess As Object: Set appAccess = CreateObject("Access.Application")
appAccess.OpenCurrentDatabase ("FILEPATH\database1.accdb")
appAccess.CurrentProject.Connection.Execute "INSERT INTO Table1(ID, Field1, Field2, Field3) VALUES('1','Hello','Helo','Hi')"
Set appAccess = Nothing
I have a table containing business priorities and the name of the query that is used to generate the results for each.
Is it possible to loop through this table and where it finds a query in the database with the same name, run it?
The closet thing I have found is below but I don't want to input my full query codes into a table.
http://www.experts-exchange.com/Database/MS_Access/Q_28284655.html
This will execute every query name that is in your table. It fills a recordset with the query names and then loops through and executes each one.
Dim sql As String
Dim rst As DAO.Recordset
Dim myQuery As String
sql = "Select myQuery from myTable"
Set rst = CurrentDb.OpenRecordset(sql)
Do While Not rst.BOF And rst.EOF
myQuery = rst(0)
DoCmd.OpenQuery myQuery, acViewNormal
rst.MoveNext
Loop
rst.Close
Set rst = Nothing
Let me know if you need to check to see if the query exists before you execute it. If so, I can add the code to search the querydefs collection for a query with the same name and only execute it if a match is found.
i am trying to append some records from a certain table to a new table ,
when i try it i am unable to append to the values
to the new table i.e "Table1" in this case what might be the reason for the data not getting appended to the table?
From what I can tell from your code you are just appending records from one table in your database into another table in the same database. You don't need to use a Recordset object to accomplish this; an INSERT INTO query will accomplish what you want.
Dim strSQL as String
Dim db as DAO.Database
Set db = CurrentDb
strSQL = "INSERT INTO Table1 ( UPI, COMPANY ) " & _
"SELECT INPUT_TBL.ID, INPUT_TBL.COMPANY FROM INPUT_TBL"
db.Execute strSQL, dbFailOnError
Set db = Nothing
Note that I used CurrentDb for the db object, under the assumption that the current database is the same one at C:\Users\test.accdb. If that's not the case, then you will need to do one of two things:
If you can add both of these tables as linked tables to the database you are running this code in, you can just use the same code as above. Instructions on how to add tables from an external database to your current database are available here: http://office.microsoft.com/en-us/access-help/import-or-link-to-data-in-another-access-database-HA001227658.aspx#BM3 and many other places on the internet.
If you can't add linked tables to your database for some reason, you can add IN clauses to the previous SQL string, which would become:
Dim strSQL as String
Dim db as DAO.Database
Dim dbname as String
Set db = CurrentDb
dbname = "C:\Users\test.accdb"
strSQL = "INSERT INTO Table1 ( UPI, COMPANY ) IN """ & dbname & """ " & _
"SELECT INPUT_TBL.ID, INPUT_TBL.COMPANY FROM INPUT_TBL IN """ & dbname & """"
db.Execute strSQL, dbFailOnError
Set db = Nothing
This code would allow you select the data from Table1 in the external database and add it to INPUT_TBL in the same external database.
How to check table is there or not?
Using VB 6.0
cmd.CommandText = "drop table t1"
cmd.Execute
Above code is working fine, but if table is not exist then it showing “table does not exit”
How to check table exist or table not exist?
Need VB CODE help?
If you just want to drop the table without throwing an error message, you can use the following SQL if you're using MySQL.
DROP TABLE t1 IF EXISTS
Other databases have a similar feature, but the syntax is different. To do the same in MSSQL:
IF EXISTS(SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 't1') DROP TABLE t1;
Although that looks very ugly.. there must be a better syntax to get the same result.
For a Jet MDB (and perhaps generically for many OLEDB Providers) you can use an approach like:
Private Sub Main()
Dim cnDB As ADODB.Connection
Set cnDB = New ADODB.Connection
cnDB.Open "Provider=Microsoft.Jet.OLEDB.4.0;" _
& "Jet OLEDB:Engine Type=5;Data Source='sample.mdb'"
'Check presence of table --------------
Dim rsSchema As ADODB.Recordset
Set rsSchema = _
cnDB.OpenSchema(adSchemaColumns, _
Array(Empty, Empty, "t1", Empty))
If rsSchema.BOF And rsSchema.EOF Then
MsgBox "Table does not exist"
Else
MsgBox "Table exists"
End If
rsSchema.Close
Set rsSchema = Nothing
'--------------------------------------
cnDB.Close
End Sub
You'd be better off checking for existence of the table concerned, rather than trying to drop it.
The SQL syntax is dependent on the database server/engine you're using, but for Sql Server you could use something like:
Sql Server 2000:
SELECT 1 as Exists FROM sysobjects WHERE name = 't1'
Sql Server 2005/2008:
SELECT 1 as Exists FROM sys.objects WHERE name = 't1'
You can then use VB like:
Dim rs as Recordset
Dim iExists as Integer
rs = cmd.Execute
On Error Goto DoesNotExist
rs.MoveFirst
iExists = CInt(rs!Exists)
DoesNotExist:
If iExists = 1 Then
' Put code here for if the table exists
Else
' Put code here for if the table does not exist
End If
Note: This code needs tidying up and "productionising" (i.e. I haven't actually tested that it works as I don't have VB6 on this machine)