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.
Related
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.
I have a recordset and I want to copy this in new table in sql using VB Script. new table should be created as per recordset only. Someone has examples?
I know how to create new table but how to copy entire recordset to this table. I have something like following code and my recordset is retrieved in oRs variable.
Set oRs = CreateObject("ADODB.Recordset")
Set oCom = CreateObject("ADODB.Command")
oCom.CommandText = "my command text"
Set oRs = oCom.Execute
Kbv.
I'm writing some Excel VBA for a user to be able to insert records into a SQL Server table, using ADODB. This is working fine:
Dim conn As New ADODB.connection
conn.Open "<my connection string>"
Dim records As New ADODB.Recordset
records.CursorLocation = adUseClient
records.Open "SELECT TOP 1 * FROM [MyTable]", conn, adOpenStatic, adLockBatchOptimistic, adCmdText
' (Add some stuff here.)
records.UpdateBatch
Something's nagging me, though: is it possible to get the recordset pointed at the right table without doing a SELECT up front? This could get to be a pretty big table, so SELECT * FROM [MyTable] is out. I'm limiting that with a TOP 1, but I'm only doing a write, so it feels like I shouldn't have to do that!
The documentation makes it sound like you can just put a table name in the Source argument:
Use the optional Source argument to specify a data source using one of the following: a Command object variable, an SQL statement, a stored procedure, a table name, [...]
In practice, this doesn't work for me, and all of the following just error out with the message below, like it thinks I'm calling a stored procedure:
records.Open "MyTable", conn, ' [...]
records.Open "[MyTable]", conn, ' [...]
records.Open "[dbo].[MyTable]", conn, ' [...]
The request for procedure 'MyTable' failed because 'MyTable' is a table object.
Is there some syntax I'm missing here, or do I just have to go with the SELECT?
I can successfully create a pivot table in excel vba 2010 with the following code where the data come from an MS access database,
Dim con As New ADODB.Connection
Dim rs As New ADODB.Recordset
con.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=H:\SQL.accdb"
rs.Open "SELECT * FROM Table1", con, adOpenStatic, adLockReadOnly
Set piv = ActiveWorkbook.PivotCaches.Create(xlExternal)
Set piv.Recordset = rs
piv.CreatePivotTable TableDestination:=Range("A1"), TableName:="P2"
con.Close
Set rs = Nothing
Set con = Nothing
But i when i change the SQL code of the recordset then the pivot can't be refreshed. In fact the pivot table has to be deleted and created over. Is there a way i can create a pivot table this way and refresh the data without deleting the pivot table.
The above answer was not quite what i was look for as different users would query from the database and will be using parameters as well, which could cause problems when multiple users query at the same time. But i did found a way to around my problem.
The code i placed above in my question, I can use to initially create my pivot and pull all the data through. I then build a separate Sub that can refresh the pivot table that looks as follows,
Dim con As New ADODB.Connection
Dim rs As New ADODB.Recordset
con.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=H:\SQL.accdb"
rs.Open "SELECT * FROM Table1", con, adOpenStatic, adLockReadOnly
With Sheets(1).PivotTables("P2").PivotCache
Set .Recordset = rs
.Refresh
End With
Set rs = Nothing
con.Close
Set con = Nothing
A quick-fix, depending on how you usually update the SQL code, is as follows.
In ACCESS , create a query, using SQL mode as
SELECT * FROM Table1 and save it as Query1
In EXCEL, modify your code to SELECT * FROM Query1
That way, Excel will treat Query1 as a Table for all practical purposes and you may make Query1 in Access as complicated as you'd like, Excel will still pull up all its contents, keeping your code reusable.
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.