I was able to use vba to change the name of a field, but was dependent on that field being in a specific col. I found some code which I thought would allow me to change the name of the field regardless of its position, however I receive an error message [tt]The database engine could not lock table 'netprospex' because it is already in use by another person or process.[/tt]
What I would like is the code to find out if there is a field in the table which is imported from an outside source and then if the field is called Title or Executive Title would be renamed to ETitle.
I originally had this by itself, [tt]db.TableDefs(Me.cboInputTable).Fields(1).Name = "ETitle"[/tt] which worked, but of course was informed that the field could be in the second col, or any other col such as the 8th. I was hoping that with the loop, it could find the field and then change it, so I don't need to know specifically which col.
Alternatively, could I write an if statement in the vba to create an sql statement that would give the appropriate name, [tt]SELECT * FROM Table WHERE (if fieldname is Title or FieldName is Executive Title) = "President"[/tt]
Becoming
[tt]SELECT * FROM Table WHERE Title = "President" -->If the fieldname is Title
SELECT * FROM Table WHERE [Executive Title] = "President" -->If the fieldname is Executive Title[/tt]
What I would like is the code to find out if there is a field in the table which is imported from an outside source and then if the field is called Title or Executive Title would be renamed to ETitle.
Code:
Dim db As DAO.Database
Dim qd As DAO.QueryDef
Dim rs As DAO.Recordset
Dim fld As DAO.Field
Set db = CurrentDb
Set rs = db.OpenRecordset(Me.cboInputTable, dbOpenTable).Clone
For Each fld In rs.Fields
'Debug.Print fld.Name
'Debug.Print fld.OrdinalPosition
If InStr(fld.Name, "Title") <> 0 Then
db.TableDefs(Me.cboInputTable).Fields(fld.OrdinalPosition).Name = "ETitle"
End If
Next fld
Set fld = Nothing
rs.Close
Set rs = Nothing
I originally had this by itself, [tt]db.TableDefs(Me.cboInputTable).Fields(1).Name = "ETitle"[/tt] which worked, but of course was informed that the field could be in the second col, or any other col such as the 8th. I was hoping that with the loop, it could find the field and then change it, so I don't need to know specifically which col.
Alternatively, could I write an if statement in the vba to create an sql statement that would give the appropriate name, [tt]SELECT * FROM Table WHERE (if fieldname is Title or FieldName is Executive Title) = "President"[/tt]
Becoming
[tt]SELECT * FROM Table WHERE Title = "President" -->If the fieldname is Title
SELECT * FROM Table WHERE [Executive Title] = "President" -->If the fieldname is Executive Title[/tt]