I have a database that has been working well for a couple years until this morning. When I attempt to copy the contents of a remote table into a local backend, I am presented with an err: "Error 3622 - You must use the dbSeeChanges option..."
The remote table is on a server and does have an AutoNumber attribute. The backend table is a simple readonly/static snapshot that does not care about the auto numbering datatype and is defined simply as Number - I just need the table (snapshot) to be local for performance concerns.
I added the dbSeeChanges variable without success - complains about "Too few Parameters" on the db.execute line (below).
Here are some details from my db:
Dim db As Database
dim strSQL as string
Set db = CurrentDb()
strSQL = "INSERT INTO Item " & _
"SELECT dbo_Item.* " & _
"FROM dbo_Item " & _
"WHERE dbo_Item.MASTER_INVID= " & TempVars!my_InvID
db.Execute strSQL, dbFailOnError + dbSeeChanges
What am I missing? Any suggestions to avoid/correct.
Another way to do this is to make a copy of the linked table then covert that to a local table:
localTableName = "Item"
DoCmd.CopyObject , localTableName , acTable, "dbo_Item"
DoCmd.SelectObject acTable, localTableName , True
RunCommand acCmdConvertLinkedTableToLocal
Related
I am told (and agree) that it is better to replace the SQL in an existing querydef, rather than delete and re-define the querydef each time the query needs to change. But my code only seems to work the second way. Here is the code I have that works:
Dim db As Database
Set db = CurrentDb
Dim QD As QueryDef
Dim mySql As String
mySql = ""
mySql = "TRANSFORM COUNT(tblDocs.Document) AS CountOfDocument " & _
"SELECT tblDocs.[Contractor Dept], " & _
"COUNT(tblDocs.Document) AS [Total Of Document] " & _
"FROM tblDocs " & _
"GROUP BY tblDocs.[Contractor Dept] " & _
"PIVOT tblDocs.[Engineering Status Code]"
On Error Resume Next
db.QueryDefs.Delete "qryX" 'Remove temporary query if exists
Set QD = db.CreateQueryDef("qryX", mySql) 'create temporary query
DoCmd.RunSQL "SELECT * INTO tblDocsCrossTabX FROM qryX;"
Here is the code I can't get to work
Dim db As Database
Set db = CurrentDb
Dim QD As QueryDef
Dim mySql As String
mySql = " "
Set QD = db.CreateQueryDef(("qryX"), mySql)
mySql = "TRANSFORM COUNT(tblDocs.Document) AS CountOfDocument " & _
"SELECT tblDocs.[Contractor Dept], " & _
"COUNT(tblDocs.Document) AS [Total Of Document] " & _
"FROM tblDocs " & _
"GROUP BY tblDocs.[Contractor Dept] " & _
"PIVOT tblDocs.[Engineering Status Code]"
QD.SQL = mySQL 'overwrite query SQL
DoCmd.RunSQL "SELECT * INTO tblDocsCrossTabX FROM qryX;"
Oddly, the second version doesn't throw any errors at me, but it doesn't make the crosstab table at all.
Edit: Maybe I wasn't clear enough. The problem is that the second set of code Does. Not. Execute. The. SQL. If it executed the code, I would be happy to re-write and use the same temp query over and over, but it does. not. execute. the. SQL..
Please respond with how to make the code in the second block actually execute the indicated SQL statement and build the desired table.
I know I have to remake a query if I delete it. Duh.
I know I "should" be able to re-use the same query if I can get the Set statement to properly overwrite the previous sql with the desired sql.
I know you all want to provide an answer, but please make it an answer to the question I am asking.
You can recycle the object if you don't delete it:
' On Error Resume Next
Set QD = db.QueryDefs("qryX")
QD.SQL = mySQL
DoCmd.RunSQL "SELECT * INTO tblDocsCrossTabX FROM qryX;"
Solved.
In the first block of code, when I delete and recreate the query, the string mySql contains a valid SQL statement, so Access is able to assign that SQL to the querydef when I completely recreate it with SET. Specifically,
mySql = "Transform..."
Comes Before
Set QD = db.CreateQueryDef("qryX", mySql)
In the second set of code, the string mySql contains only a space when the Set command is used, as " " is not a valid SQL Statement, the Set command won't even get executed, and the QueryDef QD never even gets created.
Specifically, the error in the second code occurs because
mySql = " " 'NOT a valid SQL Statement
comes before the attempt to create the queryDef
Set QD = db.CreateQueryDef(("qryX"), mySql) 'QD never gets created because mySql is not valid SQL
preventing the assignment of a valid SQL statement later, and so no table gets created.
If you can use a subquery, you can use a tempory QueryDef instead of bothering with checking forQueryDefalready existing or not, just combine the SELECT and the INSERT-Query to use a temporaryQueryDef
Unfortunally you can't useTransformin a subquery so this code leads toRuntime-Error 3129 Invalid SQL statement.
Sql = "SELECT * INTO tblDocsCrossTabX FROM (TRANSFORM COUNT(tblDocs.Document) AS CountOfDocument " & _
"SELECT tblDocs.[Contractor Dept], " & _
"COUNT(tblDocs.Document) AS [Total Of Document] " & _
"FROM tblDocs " & _
"GROUP BY tblDocs.[Contractor Dept] " & _
"PIVOT tblDocs.[Engineering Status Code])"
With CurrentDb.CreateQueryDef(vbNullString) 'or db.CreateQueryDef("") creates a not named and therefore temporary QueryDef
.SQL = Sql
.Execute dbFailOnError
End With
Sample of successful code.
Dim db As Database
Set db = CurrentDb
Dim QueryString As String
Dim QDDocsCross As QueryDef
Set QDDocsCross = db.CreateQueryDef("DocsCross")
QueryString = "TRANSFORM COUNT(Docs.Document) AS CountOfDocument " & _
"SELECT Docs.[Contractor Dept], " & _
"COUNT(Docs.Document) AS [Total Of Document] " & _
"FROM Docs " & _
"GROUP BY Docs.[Contractor Dept] " & _
"PIVOT Docs.[Engineering Status Code]"
QDDocsCross.SQL = QueryString
I want to apply an SQL query to one Access table, from which it is retrieving data from a table in another Access file. I've looked around on this subject and can't seem to get solutions to work.
Based on this source http://support.microsoft.com/kb/113701, I came up with the following, but still have no luck.
sSQL = "UPDATE TableInCurrentDB
SET [Field1InCurrentDB]= DAvg('Field1InExternalDB','[;database=C:\VB\ExternalDB.accdb].[TableInExternalDB]','Field2InExternalDB= & Year(Now()) & ')
WHERE [Field2InCurrentDB]='1';"
DoCmd.RunSQL sSQL
I know that the error lies somewhere in the reference to the external DB, because the code works fine if the tables are in the same database. However, it's tough to tell exactly what's wrong because the error I get is 'Unknown'.
How can I modify this statement to update an Access table from another Access database's table?
You prefer not to use a link to the table in the external database, but that choice is a complication when you want to use DAvg. However, since you're doing this with VBA code, you can ditch DAvg and do what you need in 2 steps:
First retrieve the average from the external table.
Use that step #1 average in your UPDATE.
For step #1, test this as a new query in the Access query designer ...
SELECT Avg(Field1InExternalDB)
FROM TableInExternalDB IN 'C:\VB\ExternalDB.accdb'
WHERE Field2InExternalDB=Year(Date());
Assuming that query returns the correct value, adapt your VBA code to retrieve the same value.
Dim db As DAO.database
Dim strSelect As String
Dim varAvg As Variant
strSelect = "SELECT Avg(Field1InExternalDB)" & vbCrLf & _
"FROM TableInExternalDB IN 'C:\VB\ExternalDB.accdb'" & vbCrLf & _
"WHERE Field2InExternalDB=Year(Date());"
'Debug.Print strSelect
Set db = CurrentDb
varAvg = db.OpenRecordset(strSelect)(0)
Debug.Print Nz(varAvg, 0) ' see note
Note that query will return Null when no rows include Field2InExternalDB values which match the current year. That is why varAvg is declared as Variant. Later Nz(varAvg, 0) will give you zero instead of Null.
Then you can use a parameter query for your UPDATE and supply Nz(varAvg, 0) as the parameter value.
Dim qdf As DAO.QueryDef
Dim strUpdate As String
strUpdate = "UPDATE TableInCurrentDB" & vbCrLf & _
"SET [Field1InCurrentDB]=[pAvg]" & vbCrLf & _
"WHERE [Field2InCurrentDB]='1';"
'Debug.Print strUpdate
Set qdf = db.CreateQueryDef(vbNullString, strUpdate)
qdf.Parameters("pAvg") = Nz(varAvg, 0)
qdf.Execute dbFailOnError
Set qdf = Nothing
Set db = Nothing
Could you not do this as a single step? Incorporate the output of the first SQL as the input to the "set" in the second?
In other words,bypass the first query and just do the second using this as the "strUpdate" string:
strUpdate = "UPDATE TableInCurrentDB" & vbCrLf & _
"SET [Field1InCurrentDB]=" & vbCrLf & _
" (SELECT Val(Nz(Avg(Field1InExternalDB),0))" & vbCrLf & _
" FROM TableInExternalDB IN 'C:\VB\ExternalDB.accdb'" & vbCrLf & _
" WHERE Field2InExternalDB=Year(Date()) )" & vbCrLf & _
"WHERE [Field2InCurrentDB]='1';"
I have two databases, a "Master" source (held locally) and a "Copy" database (to be distributed). Neither will be able to see each other once distributed (either locally or across a network), so we can't perform queries across the databases after distribution. I need to remove some content from the Copy database before it is distributed, so I decided to create a VBA script to produce the Copy databases for distribution.
There are lookups in the tables, so I have decided to hold a template database (which was copied from the master source, and then stripped the tables out of them), then am dropping the tables and have recreated them in an appropriate order.
I'm now needing to remove some of the data, and I'm struggling.
DeviceTable:
AutoNumber(ID)
Text(DeviceName)
Integer(ClusterID)
Text(Distribution)
ClusterTable:
AutoNumber(ID)
Text(ClusterName)
VirtualSystemTable:
AutoNumber(ID)
Text(VirtualSystemName)
Integer(ClusterID)
Integer(DeviceID)
InterfaceTable:
AutoNumber(ID)
Integer(VirtualSystemID)
Integer(ClusterID)
Integer(DeviceID)
Text(Description)
I need to remove entries from DeviceTable, ClusterTable, VirtualSystemTable and InterfaceTable for anything which is not marked as Distribution: "Public"
Normally I would do (in psudocode):
arrDEV = SQL("SELECT ID, ClusterID FROM DeviceTable WHERE Distribution<>"Public"")
Then, for each response, I would
arrVSYS = SQL("SELECT ID FROM VirtualSystemTable WHERE DeviceID=$arrDEV.ID OR ClusterID=$arrDEV.ClusterID")
SQL("DELETE FROM InterfaceTable WHERE DeviceID=$arrDEV.ID OR ClusterID=$arrDEV.ClusterID OR VirtualSystemID=$arrVSYS.ID")
SQL("DELETE FROM VirtualSystemTable WHERE DeviceID=$arrDEV.ID OR ClusterID=$arrDEV.ClusterID")
SQL("DELETE FROM ClusterTable WHERE ID=$arrDEV.ClusterID")
My issue is that I can't work out how to perform these queries across the database link. I amusing ADODB. I normally code in PHP, so this is a bit of a struggle for me!
You have the right idea but VBA is quite a bit different from PHP. You can't use variables inside strings with first exiting the string, concatenating the variable, and then starting the string again.
Where you would typically use an array called arrDev or arrVSys, we use a DAO Recordset or ADO Recordset object inside MS Access.
Updated answer to reflect the information you gave:
Dim db As DAO.Database
Dim sSQL as String
Dim arrDEV As DAO.Recordset, Dim arrVSYS as DAO.Recordset
Set db = OpenDatabase("C:\SomeDatabase.accdb")
sSQL = "SELECT ID, ClusterID FROM DeviceTable WHERE Distribution <> 'Public'"
Set arrDEV = db.Open(sSQL)
If Not (arrDEV.EOF and arrDEV.BOF) Then
arrDEV.movefirst
Do Until arrDEV.eof = True
sSQL = "SELECT ID FROM VirtualSystemTable WHERE DeviceID = " & arrDEV("ID") & _
" OR ClusterID = " & arrDEV("ClusterID")
Set arrVSYS = CurrentDb.Open(sSQl)
sSQL = "DELETE FROM InterfaceTable WHERE DeviceID = " & arrDEV("ID") & _
" OR ClusterID = " & arrDEV("ClusterID") & " OR VirtualSystemID = " & arrVSYS("ID")
CurrentDb.Execute sSQl, dbFailOnError
sSQL = "DELETE FROM VirtualSystemTable WHERE DeviceID = " & arrDEV("ID") & " OR ClusterID = " & arrDEV("ClusterID")
CurrentDb.Execute sSQl, dbFailOnError
sSQL = "DELETE FROM ClusterTable WHERE ID = " & arrDEV("ClusterID")
CurrentDb.Execute sSQl, dbFailOnError
arrVSYS.Close
arrDEV.MoveNext
Loop
End If
'Cleanup
Set arrVSYS = Nothing
arrDEV.Close
Set arrDEV = Nothing
db.close
Set db = Nothing
The code is untested and could have some errors. The only thing I didn't plan for was having more than one record in arrVSYS. If that recordset will have multiple records then you'll need yet another loop.
As you can see, you don't need to use ADO to access another Access database. But if the "outside" database is something other than Access then yes, you would need to use ADO.
Normally, I would just loop through using Recordset.AddNew, but this is painfully slow when dealing with large recordsets -- is there a better way to do it? I was hoping there would be a way to simply write an insert statement to go from Oracle to Access, perhaps by using two ADO connects, but the only examples I can find are in VB.net (using OleDbCommand), which is sadly not an option. Is this simply a limitation of VBScript?
Thanks!
Your question tags include access-vba, so I'll suggest the DoCmd.TransferDatabase Method.
This example, near the bottom of the page, is to create an ODBC link to a SQL Server table. It assumes a DSN (data source name) named DataSource1. The link name in Access will be Authors, and the server source table is named dboAuthors.
DoCmd.TransferDatabase acLink, "ODBC Database", _
"ODBC;DSN=DataSource1;UID=User2;PWD=www;LANGUAGE=us_english;" _
& "DATABASE=pubs", acTable, "Authors", "dboAuthors"
You can adapt that for Oracle with an appropriate DSN. And if you want to import rather than link, substitute acImport for acLink.
And actually, you might not even need code for this. With a working DSN, you can import (or link) via the Access user interface.
Your title mentioned VBScript. Tell us if you actually must use VBScript instead of VBA.
After reading your comments, I think you would be better of with a different approach.
Create a database with an ODBC link to the Oracle table.
Then export subsets of that table data to your individual database files.
SELECT master_link.* INTO local_table IN 'c:\somefolder\db1.mdb'
FROM master_link
WHERE location = 'location1';
Then adjust the SQL for each target db file and data selection criterion. It should be easy to drive that from a simple VBA procedure.
Here are a few notes for VBScript using SQL Server, rather than Oracle, however, it should be possible to use something very similar.
Const CreatePath = "Z:\docs\"
Const Provider = "Microsoft.ACE.OLEDB.12.0;"
''The easiest way to get this line is from the connect property of
''a linked table
Const ServerInLine = "[ODBC;DRIVER=SQL Server;SERVER=server;Trusted_Connection=Yes;DATABASE=db]"
''Change this to the Oracle connection
Const ServerConnect = "Provider=sqloledb;Data Source=server;Initial Catalog=db;Integrated Security=SSPI;"
Dim cnSS ''As ADODB.Connection
Dim cnMSA ''As ADODB.Connection
Dim cat ''As New ADOX.Catalog
Set cat = CreateObject("ADOX.Catalog")
Set cnSS = CreateObject("ADODB.Connection")
Set cnMSA = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
cnSS.Open ServerConnect
''Only the locations that exist. Assuming a location table
''with a description, Location
sSQL = "SELECT DISTINCT a.LocationID, b.Location FROM ATable a "
sSQL = sSQL & "INNER JOIN Location b ON a.LocationID = b.ID"
rs.Open sSQL, cnSS
Do While Not rs.EOF
DBName = CreatePath & Trim(rs.Fields("Location")) & ".accdb"
scn = "Provider=" & Provider & ";Data Source=" & DBName
cat.Create scn
cnMSA.Open scn
sSQL = "SELECT * INTO " & Replace(Trim(rs.Fields("Location")), " ", "")
sSQL = sSQL & " FROM " & ServerInLine & ".ATable"
sSQL = sSQL & " WHERE LocationID=" & rs.Fields("LocationID")
cnMSA.Execute sSQL, recs
sMsg = sMsg & vbCrLf & DBName & " created " & recs
rs.MoveNext
cnMSA.Close
Loop
MsgBox sMsg
I have linked the sql server tables to ms access so that I can use ms access as the front end.I was able to access the tables from access, until I run into an error ODBC call failed when I tried to open one of the tables. There was no problem with the other tables. Actually I have changed a column name in sql server after creating a link. Is this the problem? I am really worried about this as I was about to use access as a front-end for my future purposes.
When you link to a remote table, Access stores metadata about that table. When you later change the table structure, the metadata doesn't get updated to capture the change.
Delete the link. Then recreate the link. That way the metadata will be consistent with the current version of the table.
Yes changing the column name after linking the table is most likely causing your failure. Is it is now trying to pull data from a column that no longer exists. You will need to relink the table. You can programatically link tables in access. We do that in may of our access applications and drive the tables that need to be linked from a local access table.
Public Sub LinkODBCTables()
Dim objRS As DAO.Recordset
Dim objTblDef As DAO.TableDef
Dim strTableName As String
Dim strAliasName As String
Dim strDSN As String
Dim lngTblCount As Long
Set objRS = CurrentDb.OpenRecordset( _
" select TableName," & _
" AliasName," & _
" DSN," & _
" DatabaseName," & _
" Development_DSN," & _
" UniqueIndexCol" & _
" from tblODBCLinkedTables " & _
" order by TableName", dbOpenSnapshot)
While Not objRS.EOF
' Check to see if we already have this linked tableDef
' We don't care if it is not actually in there
strTableName = objRS.Fields("TableName")
If Not IsNull(objRS.Fields("AliasName")) Then
strAliasName = objRS.Fields("AliasName")
Else
strAliasName = strTableName
End If
If DEV_MODE Then
strDSN = objRS.Fields("Development_DSN")
Else
strDSN = objRS.Fields("DSN")
End If
On Error Resume Next
CurrentDb.TableDefs.Delete strAliasName
If Err.Number <> 0 And _
Err.Number <> 3265 Then ' item not found in collection
Dim objError As Error
MsgBox "Unable to delete table " & strAliasName
MsgBox Err.Description
For Each objError In DBEngine.Errors
MsgBox objError.Description
Next
End If
On Error GoTo 0
Set objTblDef = CurrentDb.CreateTableDef(strAliasName)
objTblDef.Connect = g_strSQLServerConn & _
"DSN=" & strDSN & _
";DATABASE=" & objRS.Fields("DatabaseName") & _
";UID=" & g_strSQLServerUid & _
";PWD=" & g_strSQLServerPwd
objTblDef.SourceTableName = strTableName
On Error Resume Next
CurrentDb.TableDefs.Append objTblDef
If Err.Number <> 0 Then
Dim objErr As DAO.Error
For Each objErr In DBEngine.Errors
MsgBox objErr.Description
Next
End If
On Error GoTo 0
' Attempt to create a uniqe index of the link for updates
' if specified
If Not IsNull(objRS.Fields("UniqueIndexCol")) Then
' Execute DDL to create the new index
CurrentDb.Execute " Create Unique Index uk_" & strAliasName & _
" on " & strAliasName & "(" & objRS.Fields("UniqueIndexCol") & ")"
End If
objRS.MoveNext
Wend
objRS.Close
End Sub
We are using a single SQLServer login for our access applications so the g_strSQLServerUID and g_strSQLServerPwd are globals that contain that info. You may need to tweek that for your own needs or integrated security. We are setting up two DSNs one for production and the other for development. The DEV_MODE global controls wich set of DSNs are linked. You can call this code from a startup macro or startup form. It will deleted the old link and create a new link so you always have the most up to date schema.