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

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)

Related

.CreateField Method using a string var as the Name

This is what I have so far with some extraneous code left out and some anonymous database and variable names:
Dim dB as DAO.Database
Dim tdf as DAO.TableDef
Dim newField As DAO.Field
Dim newFieldNameVar As String
newFieldNameVar = "Variable Name"
Set dB = CurrentDb
Set tdf = dB.TableDefs("PreExistingTable")
Set newField = tdf.CreateField(newFieldNameVar)
tdf.Fields.Append newField
Set newField = Nothing
Set tdf = Nothing
Set dB = Nothing
Where the aforementioned code exists in my function is not in a loop but the newFieldNameVar is a product of a loop, and so changes regularly based on question thread irrelevant conditions.
At the point the .Fields.Append line executes I get this run-time error: Invalid field data type. '3259' I am fairly new to programming in any form or fashion, but imagine that I might be using the wrong syntax for the variable used as the Name element of the .CreateField Method. Any ideas? Am I able to use a string variable as the Name in the .CreateField Method?
Thanks for the help in advance!
While Type is an optional parameter of the .CreateField method, each field does need a type before you can append it to the table, else Access will throw this error.
It's also a good practice to specify field size.
Specify your field type, e.g.:
Dim dB as DAO.Database
Dim tdf as DAO.TableDef
Dim newField As DAO.Field
Dim newFieldNameVar As String
newFieldNameVar = "Variable Name"
Set dB = CurrentDb
Set tdf = dB.TableDefs("PreExistingTable")
Set newField = tdf.CreateField(newFieldNameVar, dbText, 255) '255 character long text field
tdf.Fields.Append newField
Set newField = Nothing
Set tdf = Nothing
Set dB = Nothing

Need to hide a column in a table on button click

I have a button that calls a macro which opens up a table. I'd like to hide a column in that table when it opens. The macro opens the table tChart1_Reportable and I'd like to hide the ID column.
I figure this has to be done in VBA with the RunCode action selected in the macro.
I haven't had luck finding code that works. Found this online but it doesnt work in the current form:
Public Sub SetColumnHidden()
Dim dbs As DAO.Database
Dim fld As DAO.Field
Dim prp As DAO.Property
Const conErrPropertyNotFound = 3270
' Turn off error trapping.
On Error Resume Next
Set dbs = CurrentDb
' Set field property.
Set fld = dbs.TableDefs!tChart1_Reportable.Fields!ID
fld.Properties("ColumnHidden") = True
' Error may have occurred when value was set.
If Err.Number <> 0 Then
If Err.Number <> conErrPropertyNotFound Then
On Error GoTo 0
MsgBox "Couldn't set property 'ColumnHidden' " &; _
"on field '" &; fld.Name &; "'", vbCritical
Else
On Error GoTo 0
Set prp = fld.CreateProperty("ColumnHidden", dbLong, True)
fld.Properties.Append prp
End If
End If
Set prp = Nothing
Set fld = Nothing
Set dbs = Nothing
End Sub
Although you really should think about using a query to accomplish this goal instead, the problem with this code is definitely that the Table itself does not refresh the hidden property on execution.
That being said, this would likely suffice as a solution to this problem:
Public Sub SetColumnHidden()
Dim dbs As DAO.Database
Dim fld As DAO.Field
Dim prp As DAO.Property
Set dbs = CurrentDb
'Set field property.
Set fld = dbs.TableDefs!tChart1_Reportable.Fields!ID
fld.Properties("ColumnHidden") = True
DoCmd.Close acTable, "tChart1_Reportable"
DoCmd.OpenTable "tChart1_Reportable"
Set prp = Nothing
Set fld = Nothing
Set dbs = Nothing
End Sub
To work this through a query, create a new query with Query Design, open it in SQL view. Your query should like something like this:
SELECT [Field1],
[Field2],
[Field3]
FROM tChart1_Reportable
Basically, add every field you want to view (by the field's name), removing the "ID" field from the list. This will accomplish what you're attempting to do. As far as why the code is not working for you, all I can say is it should be.

How can I create button to search database column names in MS Access?

I am trying to create a button in MS Access 2013 that allows a user to search a database for a column containing input text. I was able to find the search code in this post ( Find a table when you know the name of a column? ) but I can't figure out how to bind it to a button. Also when I run the search function from main it doesn't give any output. The only way I can get the search function to work is to hard code the search string into the function.
Sub Main()
Dim inStr As String
ListTablesWithColumnNamesContaining (InputBox("Name contains", "Search"))
End Sub
Public Sub ListTablesWithColumnNamesContaining(ByVal pText As String)
Dim db As DAO.Database
Dim tdf As DAO.TableDef
Dim fld As DAO.Field
Set db = CurrentDb
For Each tdf In db.TableDefs
For Each fld In tdf.Fields
If InStr(1, fld.Name, "pText", vbTextCompare) > 0 Then
Debug.Print tdf.Name & ":", fld.Name
End If
Next fld
Next tdf
Set fld = Nothing
Set tdf = Nothing
Set db = Nothing
End Sub
You should create a form, put a button inside it choosing button from the Design menu tab.
Then you need to double click the button, and you will open the properties window. Inside this window in the event tab, you need to double click the "on click" row and then press the ".." button on the side.
Now you should see the VBA Editor with the cursor inside this function:
Private function button1_onClick (....)
End Function
You can copy and paste your code here inside and obtain this:
Private function button1_onClick (....)
Dim db As DAO.Database
Dim tdf As DAO.TableDef
Dim fld As DAO.Field
Dim pText As String
pText = InputBox("Name contains", "Search")
Set db = CurrentDb
For Each tdf In db.TableDefs
For Each fld In tdf.Fields
If InStr(1, fld.Name, pText, vbTextCompare) > 0 Then
Msgbox tdf.Name & ":" & fld.Name
End If
Next fld
Next tdf
Set fld = Nothing
Set tdf = Nothing
Set db = Nothing
End Function
Note that, as HansUp commented, you need to remove the quotes surrounding pText. I've also put the InputBox command directly inside this function and substituted the debug.print with a Message Box.

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

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

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.