Can I upgrade an Access MDE database to Access 2007 ACCDE format using VBA only? - ms-access-2007

I would like to provide the facility for the user to upgrade their own back-end databases rather than sending the file to me to manually convert using Access.
Is this possible? Having read around the subject I don't have much hope but would like expert confirmation it is impossible or hopefully a solution.

If you are really limited to just MDE/ACCDE format, this appears to be impossible.
One imperfect way you could do it, if you only use tables and queries, is to create a new database in the desired format, and import the tables and queries from the old database. This is because using the MDE/ACCDE format, you cannot import or export forms, modules, and macros. To try this produces the error:
Error 29045: Description: "You can't import, export, create, modify, or rename any forms, reports, pages or modules in an ACCDE, MDE or ADE database."
If using just tables and queries you could go ahead as follows based on the example given in http://support.microsoft.com/kb/298174
(In my scenario, I am not able to use the new database as the current database so I must import into the current database temporarily and then export into the new database, otherwise just directly import into the new database.)
Dim strNewDB As String
Dim strOldDB As String
Dim dbOld as Database
Dim dbNew as Database
Dim tbl As Variant
Dim qry As QueryDef
Set dbNew = DBEngine.CreateDatabase(strNewDB, dbLangGeneral & ";pwd=xxxxx"), dbEncrypt)
Set dbOld = DBEngine.OpenDatabase(strOldDB, , , ";pwd=xxxxx"))
'import/export tables
For Each tbl In dbOld.TableDefs
Debug.Print tbl.name
If left(tbl.name, 4) <> "MSys" Then
DoCmd.TransferDatabase acImport, "Microsoft Access", dbOld.name, acTable, _
tbl.name, "_temp_" & tbl.name
DoCmd.TransferDatabase acExport, "Microsoft Access", dbNew.name, acTable, _
"_temp_" & tbl.name, tbl.name
DoCmd.DeleteObject acTable, "_temp_" & tbl.name
End If
Next tbl
'import/export queries
For Each qry In dbOld.QueryDefs
Debug.Print qry.name
DoCmd.TransferDatabase acImport, "Microsoft Access", dbOld.name, acQuery, _
qry.name, "_temp_" & qry.name
DoCmd.TransferDatabase acExport, "Microsoft Access", dbNew.name, acQuery, _
"_temp_" & qry.name, qry.name
DoCmd.DeleteObject acQuery, "_temp_" & qry.name
Next qry
As mentioned by Robert Harvey in this comment, when using MDB/ACCDB you could just use Application.ConvertAccessProject.

Related

VBA /ADODB recordset is not cleared - PC dependent

As usual, a bit of a strange problem with one of my Excel VBA macro's.
I use Excel as a database, with one worksheet storing a large amount of data.
I search the date using ADODB.
Everything run's fine on most PC's, but today, one of my colleagues encountered a problem which I cannot understand.
The issue is only seen on his computer. He sent me the file, I failed to reproduce the issue on my PC. Additionally, other colleagues using the same source code do not have any issues.
The issue:
One of the fields (column) in the database is the "case status". When we change the status of a case from 'Open' to 'Closed' and then query all records with status 'Open', the changed case still shows up in the resulting record set. When I verify the status in the worksheet which is used as the query source, I can see that the status is set to 'Closed' but ADODB still find it as 'Open'.
As I mentioned, this happens only on 1 PC, so it cannot be the code. Could this be a particular library and if so, which one. I compared the used libraries via a teamviewer session on his and my PC and they are all identical.
I am lost.
This is the code I use
Public Sub OpenDB()
If cnn.state = adStateOpen Then cnn.Close
cnn.ConnectionString = "Driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};DBQ=" & _
ActiveWorkbook.Path & Application.PathSeparator & ActiveWorkbook.Name
cnn.Open
End Sub
Public Sub closeRS()
If rs.state = adStateOpen Then rs.Close
rs.CursorLocation = adUseClient
End Sub
...
If PRODUCTname <> "" Or PIRstate <> "" Or cmbPIRnr.Text <> "" Then
'now extract data
closeRS
OpenDB
rs.Open strSQL, cnn, adOpenKeyset, adLockOptimistic
If rs.RecordCount > 0 Then
'Now putting the data on the sheet
ActiveCell.CopyFromRecordset rs
Else
MsgBox "I was not able to find any matching records.", vbExclamation + vbOKOnly
Exit Sub
End If
...

Connect to SQL Server Database using MS Access VBA

I am trying to mass import a lot of tables from a SQL Server Database into MS Access. I have been able to use this code to import a single table, but I want to find a way to import many (not all) tables.
DoCmd.TransferDatabase acLink, "ODBC Database", _
"ODBC;Driver={SQL Server};Server=[serverName];Database=[dbName]" _
& ";Trusted_Connection=Yes", acTable, "tableName", "tableName"
I'm thinking if I can connect to the database, I can iterate through all the tables that match my criteria and loop through the code above, replacing tableName with each table. Here is what I have tried so far:
Dim c As ADODB.connection
Dim r As ADODB.Recordset
Dim connStr As String
connStr = "DRIVER={SQL Server};Server=[serverName];Database=[dbName];Trusted_Connection=yes;"
c.Open connStr
This is the error I get.
"Object variable or With block variable not set"
Anyone have any suggestions on how to approach this?
Also on a side note, I have been trying to transfer tables using DSN to connect, but I am not sure how. This is what I have.
DoCmd.TransferDatabase acLink, "ODBC Database", _
"ODBC;DSN=[DSN File];LANGUAGE=us_english;" _
& "DATABASE=[dbName]", acTable, "tableName", "tableName"
[DSN File] is a DSN file that I have saved in the same folder as my MS Access file. I think I need to add more info to specify the DSN, since I'm not sure how the code will know where to look for the DSN file. This is the error I am getting. Thanks for any help in advance.
[Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (#0)
Edit: Everything works when I connect without using the DSN. But I'm curious, how would I do it using a DSN file? Thanks

VBA/SQL ACCESS: Copy query result to a table in other access database

i want to copy my query results to a other Access database with
VBA-code in a module or directly with the SQL-query.
maybe my code should look like this?:
Sub export()
DoCmd.CopyObject , "myquery", acQuery, "C:/...mytable//"
MsgBox ("Export finish!" & Date & " , " & Time)
DoCmd.SetWarnings True
End Sub
Thanks for each tip!
The below should be your code to copy a query from one access database to another...
DoCmd.TransferDatabase acExport, "Microsoft Access", "C:\DestTemp\Dest.mdb", acQuery, "qryInfo", "qryInfo".
"C:\DestTemp\Dest.mdb" -> your target database
qryInfo -> Query name in the source and then the target database accordingly...
NOTE: Both the database must be opened during this task....

How to copy data from non-linked to linked table in Access 2010 using VBA

So what I'm trying to do is to either copy data from an Access local table to a linked table on a SQL Server. I'd like this to work via a macro so it can be triggered upon opening a report. Or I could just bypass the linked table and insert directly to the remote server.
What I'm having a difficult time finding is the proper syntax to accomplish this. It should be very easy but apparently it's not.
Public Sub CopyPT()
Dim strSQL As String
Dim strConnect As String
Dim strDatabase As String
Dim strTableName As String
ODBC_String = "ODBC;DRIVER={SQL Server};SERVER=aaa;DATABASE=bbb;UID=ccc;PWD=ddd;" _
& "LANGUAGE=us_english;TRUSTED_CONNECTION=No"
' Id also like to truncate the Destination table here too
strSQL = "SELECT * INTO dbo_DESTINATION_TABLE IN [" & ODBC_String & "]" _
& " FROM [ACCESS_LOCAL_TABLE];"
DoCmd.RunSQL strSQL
MsgBox "Success!"
End Sub
Either I need to get the proper format for the IN statement, which doesn't appear to have ODBC option, or I need another method to copy to the linked table. Such a simple copy and paste line by line macro - which I also can't find anywhere since most sane people would just write a query.
Thanks!

Schema changes not updated in MS Access front end linked to the database

I created a new table in SQL server using SQL Server Management Studio but the MS Access front end linked to the database was not updated.
I tried reopening Access but still the new tables cannot be found. Yet when I check the SQL Server database they are there.
My tables in Access are linked to the database so I assumed any table or changes made in the SQL server database would be reflected in the Access front end. When I run a query in Access looking for the tables nothing is found. Another bit of information is when I right click and press view dependencies it says unable to view dependencies because
"unable to cast object of type 'System.DBNull' to type
'System.string'"
Something is maybe wrong with the way i save the query but I am not sure.
Your assumption:
I assumed any table or changes made in the SQL server database would
be reflected in the Access front end
...is not correct. Access does not automatically re-link when the SQL Server's schema changes, and it really can't. You're expecting Access assumes the data models between SQL Server and Access are the same. Even if your table and column names are exactly the same there are still differences to deal with since the data types have some differences. So, even in the best-case scenario Access does not have enough info to automatically re-link.
When you modify the SQL Server db you have to re-link from Access. Here's an article with some code that will allow you to do that quickly but note that you still have to launch it manually. And beware, as mentioned above, linking isn't that straightforward. If you use an automated method for linking the process will have to make some decisions, some of which make take you by surprise.
I have found the management of linked tables in access to be administratively tedious. In order to make my life simpler I have used the functions below that can be called to update the linked tables in access. This will take care of updating the structure of any changed table in SQL. Adding values to the SetTableNames function will bring in new tables
Private mstrTableNames(100) As String
Private const gcSQLDB as string = "MySQLServer"
Private const gcUserID as string = "SQLUserName"
Private const gcUserPassword as string = "SQLPassword"
Private const gcLiveDSN as string = "DSN"
Private const gcEmpty as string = ""
Public Function LinkLiveTables() As Boolean
Dim tdfLinked As TableDef
Dim strConnect As String
Dim intLoop As Integer
'Remove all non system tables from the application:
' !!!NB Add other exclusions so as to not delete tables that are not linked!!!
For Each tdfLinked In CurrentDb.TableDefs
If Left(tdfLinked.Name, 2) <> "MS" Then
If Left(tdfLinked.Name, 7) <> "tblTemp" Then
CurrentDb.TableDefs.Delete tdfLinked.Name
End If
End If
Next
'Create a linked table that points to SQL Server
strConnect = "ODBC;DATABASE=" & gcSQLDB & ";UID=" & gcUserID & _
";PWD=" & gcUserPassword & ";DSN=" & gcLiveDSN
SetTablesNames
For intLoop = 1 To 100
If mstrTableNames(intLoop) = gcEmpty Then GoTo ProcExit
Set tdfLinked = CurrentDb.CreateTableDef(mstrTableNames(intLoop))
With tdfLinked
.Connect = strConnect
.SourceTableName = "dbo." & mstrTableNames(intLoop)
End With
CurrentDb.TableDefs.Append tdfLinked
Next
ProcExit:
MsgBox "Connection to the LIVE tables was successful.", vbInformation
Exit Function
ProcError:
MsgBox "Link to LIVE tables Failed." & vbCrLf & vbCrLf & _
"Error Number : " & Err.number & vbCrLf & _
"Error Description : " & Err.Description, vbCritical
End Function
Private Sub SetTablesNames()
mstrTableNames(1) = "tblMoistureHist"
mstrTableNames(2) = "tblRawMaterials"
' ... add the additional table that you need as mstrTableNames(n) = "tablename"
End Sub