Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBA and access

Status
Not open for further replies.

osbel

Programmer
Sep 17, 2001
26
US
I have an access database with 20+ tables each with 20+ fields. I need a way to change the field names with spaces to have "_" instead. Does anyone have a way to do this or something to help me on my way? Thanks.
 
You'll be using the tableDef and Field objects, and the instr function.

Dim dbs as database
Dim tdef as tabledef
Dim x as integer
Dim y as integer
Set dbs = currentdb()
For x = 0 to dbs.tabledefs.count-1
set tdef = dbs.tabledefs(x)
For y = 0 to tdef.fields.count
With tdef
if instr("_",.fields(y).name) then
.fields(i).name = 'code here to strip & rename field using another loop, mid, left etc
Next y
Set tdef = nothing
Next tdef
set dbs = nothing

Tyrone Lumley
augerinn@gte.net
 
Thanks for the help. When I go to run the code it says that the database is a user-defined type and is not defined. How do I remedy this?
 
thanks, this is the code i ended up with. Works great

Public Sub ChangeNames()
Dim x As Integer
Dim y As Integer
Dim space As Integer
Dim count As Integer
Set dbs = CurrentDb()
Dim test As String
For x = 0 To dbs.tabledefs.count - 1
If InStr(dbs.tabledefs(x).Name, "MS") = 0 Then
Set tdef = dbs.tabledefs(x)
For y = 0 To tdef.Fields.count - 1
tdef.Fields(y).Name = Replace(tdef.Fields(y).Name, " ", "_")
Next y
End If
Next x
Set tdef = Nothing
Set dbs = Nothing
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top