Trevor G
Well-known Member
- Joined
- Jul 17, 2008
- Messages
- 6,740
- Office Version
- 365
- 2021
- 2019
- 2016
- Platform
- Windows
I import data into a Temp Table and there are additional spaces. What I have below is code to loop through all the tables in the database and trim all text fields, what I need it to do is just do this on the TempImport table.
What would I need to adjust this to assign the code to the TempImport only?
What would I need to adjust this to assign the code to the TempImport only?
Sub TrimAllTextFieldsAllTables()
'The following code will trim all text fields in all tables
Dim db As DAO.Database
Dim tbls As DAO.TableDefs
Dim tbl As DAO.TableDef
Dim thisTable As DAO.TableDef
Dim SQLString As String
Dim flds As DAO.Fields
Dim fld As DAO.Field
Set db = CurrentDb
Set tbls = db.TableDefs
' loop through each appropriate table (i.e. no linked or system tables)
For Each tbl In tbls
If tbl.Attributes = 0 Then
Set thisTable = tbl
' grab all fields
Set flds = thisTable.Fields
SQLString = "UPDATE [" & tbl.Name & "] SET "
' if field is text, create SQL string to trim it
For Each fld In flds
If fld.Type = dbText Then
SQLString = SQLString & "[" & fld.Name & "] = Trim([" & fld.Name & "]),"
End If
Next fld
SQLString = Left(SQLString, Len(SQLString) - 1) & ";"
Debug.Print SQLString
' execute update statement on table
db.Execute SQLString, dbFailOnError
End If
Next tbl
End Sub