I recently took over a database in which the previous dba had set a number of table fields to act as lookup displaying combo boxes. This is a horrible thing to do, IMHO, and since users should never directly have access to tables, and should be working through forms, there is absolutely no reason for this in the first place. Now not wanting to have to go through hundreds of tables and countless fields resetting this property manually, I decided to write a simple function to perform this cleanup for me. Hopefully this might help someone else out with this issue.
'---------------------------------------------------------------------------------------
' Procedure : RemoveAllTableLookupCboLst
' Author : Daniel Pineault, CARDA Consultants Inc.
' Website : http://www.cardaconsultants.com
' Purpose : Loops through all the tables in a database and removes/resets the Lookup
' Display Control property to reset it to a simple textbox rather than a
' combo box or listbox. Never needed as users should never access tables
' directly!
' Copyright : The following is release as Attribution-ShareAlike 4.0 International
' (CC BY-SA 4.0) - https://creativecommons.org/licenses/by-sa/4.0/
' Req'd Refs: None required
'
' Usage:
' ~~~~~~
' Call RemoveAllTableLookupCboLst
'
' Revision History:
' Rev Date(yyyy/mm/dd) Description
' **************************************************************************************
' 1 2013-05-17 Initial Release
' 2 2019-02-23 Updated Header
' Updated Error Handler
'---------------------------------------------------------------------------------------
Function RemoveAllTableLookupCboLst() As String
Dim db As DAO.Database
Dim td As DAO.TableDefs
Dim t As DAO.TableDef
Dim fld As Field
On Error GoTo Error_Handler
Set db = CurrentDb()
Set td = db.TableDefs
On Error Resume Next
For Each t In td 'Loop through each table
If Left(t.Name, 4) <> "MSys" Then 'Don't mess with system tables!
For Each fld In t.Fields 'loop through each field
If fld.Type = dbText Then 'Only modify Text fields
fld.Properties("DisplayControl") = acTextBox
End If
Next fld
End If
Next t
Error_Handler_Exit:
On Error Resume Next
If Not td Is Nothing Then Set td = Nothing
If Not db Is Nothing Then Set db = Nothing
Exit Function
Error_Handler:
MsgBox "The following error has occurred" & vbCrLf & vbCrLf & _
"Error Number: " & Err.Number & vbCrLf & _
"Error Source: RemoveAllTableLookupCboLst" & vbCrLf & _
"Error Description: " & Err.Description & _
Switch(Erl = 0, "", Erl <> 0, vbCrLf & "Line No: " & Erl) _
, vbOKOnly + vbCritical, "An Error has Occurred!"
Resume Error_Handler_Exit
End Function
This code will “destroy” attachment fields. Theirs ControlDisplay ist acAttachment. And even problems could come with BoolFields, because they have standard acCheckbox, even no RowSource is set.
Thank you for your comment. The function was originally developed against several mdb databases (so no attachment fields, etc…). I have since amended my code to only modify text fields.
you are fast 🙂
I work for only a few select clients and your code has saved me hours! I had the same problem, a database that SIX other developers worked on, all putting lookup comboboxes in the tables. This has saved me a great deal of time, thank you!
This looks like exactly what I need. So I copied the code and now I get a syntax error on the following line:
If Left(t.Name, 4) <> “MSys” Then
Also the error message has a syntax error.
It a formatting issue with the website. I’ve updated the post, so try it now, it should work for you.