Is it possible to modify the structure of an Access encrypted backend? - ms-access

I have a split database. Experimenting with the frontend, I was able to add fields to a table in the linked backend using VBA until I encrypted the backend with a password.
Is it possible to still add fields to tables in the backend using VBA in the frontend WITHOUT decrypting the backend manually?
Thanks for any reply.

It should be. Try these notes:
Sub AlterDB()
Dim db As DAO.Database
Dim sDB As String
Dim tdf As TableDef
Dim fld As Field
''Encrypted
sDB = "Z:\Docs\Test.enc"
''http://msdn.microsoft.com/en-us/library/office/ff193474.aspx
''Password is case sensitive
Set db = OpenDatabase(sDB, False, False, "MS Access;PWD=pW")
''Option with tabledef
''The table is currently closed
Set tdf = db.TableDefs("table1")
Set fld = tdf.CreateField("NewField", dbText, 20)
tdf.Fields.Append fld
''Option with DDL
ssql = "ALTER TABLE table1 ADD COLUMN AnotherNew Int"
db.Execute ssql, dbFailOnError
End Sub
Sub ListFields()
sDB = "Z:\Docs\Test.enc"
Set db = OpenDatabase(sDB, False, False, "MS Access;PWD=FB")
Set tdf = db.TableDefs("table1")
For Each f In tdf.Fields
Debug.Print f.Name
Next
End Sub

Related

How to set rich text property when creating memo field in DAO?

I'm creating a table with a memo field in DAO.
However, I don't know how to set/create the rich text property for the field.
The following code gives a run time error:
Sub CreateTable()
Dim db As DAO.Database
Dim tdf As DAO.TableDef
Dim fld As DAO.Field2
Set db = CurrentDb
' Create table
Set tdf = db.CreateTableDef
tdf.Name = "myTable"
Set fld = tdf.CreateField("memo_field", dbMemo)
fld.Properties("TextFormat").Value = acTextFormatHTMLRichText '<- getting error here
tdf.Fields.Append fld
db.TableDefs.Append tdf
db.TableDefs.Refresh
Application.RefreshDatabaseWindow
Set fld = Nothing
Set tdf = Nothing
Set db = Nothing
End Sub
Somebody in a forum suggested something like this,
fld.Properties.Append fld.CreateProperty("TextFormat", dbByte, acTextFormatHTMLRichText)
but I was not able to implement this.
I guess I just go the syntax wrong. What would be the correct way to implement this?
Thanks for your help!
You may have trouble appending a property to a field that is not yet appended to a TableDef.
Try:
Set fld = tdf.CreateField("memo_field", dbMemo)
tdf.Fields.Append fld
tdf.Fields(fld.Name).Properties.Append fld.CreateProperty("TextFormat", dbByte, acTextFormatHTMLRichText)

Transferring using ADO 5 worksheets into 5 tables

I have been struggling to transfer data from multiple excel worksheets into multiple access tables. So how this goes is this way. I have 5 worksheets and each of this worksheet is to be transferred from Excel into a specific Access table. How do I do this using VBA?
I cant seem to put the file in so I hope you guys understand!
Thanks in advance for helping me!!
You can use ADO. First, set a reference to the ADO library in the VBE: Tools, References. Look for Microsoft ActiveX Date Objects Library 6.1 (or 6.0) and tick the box next to it.
Then you can use the code below to post data from a sheet to a table in the Access database (use this in a loop if you want to do multiple sheets):
Dim i As Long, j As Long
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim arr() As Variant
'Load the data from a sheet into an array
arr = Sheets(1).Range("A2:B10").Value
'Connect to Access database
Set cn = New ADODB.Connection
With cn
.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Documents\Database1.accdb"
.Open
End With
'Post data to table
Set rs = New ADODB.Recordset
With rs
.Source = "Table1"
.ActiveConnection = cn
.CursorType = adOpenStatic
.CursorLocation = adUseServer
.LockType = adLockOptimistic
.Open
For i = 1 To UBound(arr, 1)
.AddNew
For j = 1 To UBound(arr, 2)
.Fields(j).Value = arr(i, j) 'This assumes you have an autonumber ID field. (Field indexes in recordsets are 0 based.)
Next
.Update
Next
.Close
End With
'Clean up
Set rs = Nothing
cn.Close
Set cn = Nothing
EDIT:
If you want to check if a record already exists in the table, use the recordset FILTER property. Say you have an "ID" in column 1 of your spreadsheet and an "ID" field in your database table, then do:
rs.Filter = "ID='" & arr(1,j) & "'"
If rs.RecordCount > 0 then
'Record(s) already exist
...

How can you convert an ACCDB to an MDB programmatically?

Is there a way to programmatically convert an Access 2010 ACCDB file to an Access 95/97 MDB file?
Here are some notes. I do not have an old version to play around with, so I do not know if you can import more than you can export:
Dim ws As Workspace
Dim db As Object
Dim tdf As TableDef
Dim qdf As QueryDef
Dim dbExp As Database
Dim acApp As New Access.Application
acApp.OpenCurrentDatabase "z:\docs\demo.accdb"
Set dbExp = acApp.CurrentDb
Set ws = DBEngine.Workspaces(0)
FName = "z:\docs\oldver95.mdb"
''Access 95
Set db = ws.CreateDatabase(FName, dbLangGeneral, dbVersion30)
''You can only export tables and a limited range of datatypes
For Each tdf In dbExp.TableDefs
If Left(tdf.Name, 4) <> "Msys" Then
acApp.DoCmd.TransferDatabase acExport, "Microsoft Access", _
FName, acTable, tdf.Name, tdf.Name
End If
Next
See http://msdn.microsoft.com/en-us/library/office/bb243161(v=office.12).aspx
A few notes using VBScript to demonstrate using the engine:
Dim objEngine
Dim objWS
Dim objDB
Dim db: db = "z:\docs\oldver95.mdb"
Set objEngine = CreateObject("DAO.DBEngine.36")
Set objDB = objEngine.OpenDatabase(db)
strSQL="SELECT * FROM Table1"
objDB.CreateQueryDef "Query1", strSQL

Issue using Access DoCmd.Rename on table: linked names not renamed

Summary: why might Docmd.Rename on a table result in tables that don't change name over a Link from another DB?
I'm trying to fix up an old database that needs TLC. Part of this is deleting lots of unused cruft, amongst which are some tables. The first part if a VBA procedure that calls DoCmd.Rename on these tables, renaming with DELETE_ prepended.
The "deletes" appear to work fine - but when I try to reference tables from another DB using the Linked Table manager, no renames have happened at all. If I go back and load that DB, the table names are changed.
Is it best to use TableDefs().Name to rename? Is that a better method? I'd assumed an "official" way like Rename would be better.
I'm using Access 2007 on Win7/64. The files are in MDB format.
Do you wish to rename the tables in the linked database? If so, you can use OpenDatabase to reference the linked Access database. You might try something on the lines of:
Dim dbLink As DAO.Database
Dim dbCurr As DAO.Database
Dim ws As DAO.Workspace
Dim rst As DAO.Recordset
Dim tdf As TableDef
Set ws = DBEngine.Workspaces(0)
Set dbCurr = CurrentDb
For Each tdf In dbCurr.TableDefs
sConn = tdf.Connect
sSource = tdf.SourceTableName
sName = tdf.Name
If InStr(sConn, ";DATABASE=") > 0 Then
strdb = Mid(sConn, InStr(sConn, ";DATABASE=") + 10)
If InStr(sConn, "PWD") > 0 Then
sPWD = Left(sConn, InStr(sConn, ";DATABASE="))
Else
sPWD = vbNullString
End If
If Dir(strdb) <> vbNullString Then
Set dbLink = ws.OpenDatabase(strdb, False, False, sPWD)
dbLink.TableDefs(sSource).Name = "DELETE_" & sSource
End If
End If
Next

rapidly change data type of table in Access 2007

I have a table with >100 columns imported from excel to access 2007, and I want to change all the datatype of the fields into memo, being fed up of manually clicking the datatype pull down list one by one, can I do it by VBA or SQL statement? Thanks!
I fixed it finally:
Dim db As DAO.Database
Dim tdf1 As DAO.TableDef
Dim fld As DAO.Field
Set db = CurrentDb
Set tdf = CurrentDb.OpenRecordset("ssi_10q12_v5_table")
Set tdf1 = db.CreateTableDef("ssi_10q12_v5_table_1")
Debug.Print tdf.Name,
Debug.Print tdf.Fields.Count
For x = 0 To tdf.Fields.Count - 1
Debug.Print tdf.Fields(x).Name,
Set fld = tdf1.CreateField(tdf.Fields(x).Name, dbMemo)
tdf1.Fields.Append fld
Next x
db.TableDefs.Append tdf1
Set fld = Nothing
Set tdf = Nothing
End Sub
See if this can help anyone here, thanks again.