VBA to list all field names in a table
VBA to list all field names in a table
am 10.11.2007 03:02:55 von google
Hi
I'm using automation with winword to search and replace text in a word
document.
The word document is a letter with text such as <> and
Dear <>, Which of course I would like to replace with
database data.
Rather than duplicate the search and replace code for each field one-
by-one, it would be great to search the document for Each fieldname in
the table, making for neater code and great housekeeping.
Does anyone know how to list all the fieldnames in a given table or
form??
ie.
For each Table.Fieldname
debug.print Fieldname
Next
Anyone got any ideas??
Regards
Graeme.
Re: VBA to list all field names in a table
am 10.11.2007 03:06:17 von Bob Quintal
google@webedi.co.uk wrote in
news:1194660175.364709.31050@d55g2000hsg.googlegroups.com:
> Hi
>
> I'm using automation with winword to search and replace text in a
> word document.
>
> The word document is a letter with text such as <>
> and Dear <>, Which of course I would like to replace
> with database data.
>
> Rather than duplicate the search and replace code for each field
> one- by-one, it would be great to search the document for Each
> fieldname in the table, making for neater code and great
> housekeeping.
>
> Does anyone know how to list all the fieldnames in a given table
> or form??
> ie.
>
> For each Table.Fieldname
> debug.print Fieldname
> Next
>
> Anyone got any ideas??
>
> Regards
> Graeme.
>
Public Sub showtdf()
Dim db As Database
Dim tdf As tabledef
Dim x As Integer
Set db = CurrentDb
For Each tdf In db.TableDefs
Debug.Print tdf.Name,
Debug.Print tdf.Fields.Count
For x = 0 To tdf.Fields.Count - 1
Debug.Print tdf.Fields(x).Name,
Next x
Next tdf
End Sub
--
Bob Quintal
PA is y I've altered my email address.
--
Posted via a free Usenet account from http://www.teranews.com
Re: VBA to list all field names in a table
am 10.11.2007 06:32:18 von Tony Toews
Bob Quintal wrote:
>Public Sub showtdf()
>Dim db As Database
>Dim tdf As tabledef
>Dim x As Integer
>Set db = CurrentDb
>For Each tdf In db.TableDefs
> Debug.Print tdf.Name,
> Debug.Print tdf.Fields.Count
> For x = 0 To tdf.Fields.Count - 1
> Debug.Print tdf.Fields(x).Name,
> Next x
>Next tdf
>End Sub
Very minor nit. I don't have the code right in front of me but I'm sure you can go
through the fields collection on a table object in the same way you went through the
tableDefs object.
Also the original poster want only one table's fields. And you can get right to that
table without using a tabledefs collection. Although more likely would be a
querydef.
But very minor nit picking.
Tony
--
Tony Toews, Microsoft Access MVP
Please respond only in the newsgroups so that others can
read the entire thread of messages.
Microsoft Access Links, Hints, Tips & Accounting Systems at
http://www.granite.ab.ca/accsmstr.htm
Tony's Microsoft Access Blog - http://msmvps.com/blogs/access/
Re: VBA to list all field names in a table
am 10.11.2007 08:22:54 von lyle
On Nov 10, 12:32 am, "Tony Toews [MVP]"
wrote:
> Bob Quintal wrote:
> >Public Sub showtdf()
> >Dim db As Database
> >Dim tdf As tabledef
> >Dim x As Integer
> >Set db = CurrentDb
> >For Each tdf In db.TableDefs
> > Debug.Print tdf.Name,
> > Debug.Print tdf.Fields.Count
> > For x = 0 To tdf.Fields.Count - 1
> > Debug.Print tdf.Fields(x).Name,
> > Next x
> >Next tdf
> >End Sub
>
> Very minor nit. I don't have the code right in front of me but I'm sure you can go
> through the fields collection on a table object in the same way you went through the
> tableDefs object.
>
> Also the original poster want only one table's fields. And you can get right to that
> table without using a tabledefs collection. Although more likely would be a
> querydef.
>
> But very minor nit picking.
>
> Tony
> --
> Tony Toews, Microsoft Access MVP
> Please respond only in the newsgroups so that others can
> read the entire thread of messages.
> Microsoft Access Links, Hints, Tips & Accounting Systems athttp://www.granite.ab.ca/accsmstr.htm
> Tony's Microsoft Access Blog -http://msmvps.com/blogs/access/
There are many ways:
Dim z As Long
Dim y As Long
While Err.Number = 0
On Error Resume Next
While Err.Number = 0
On Error Resume Next
Debug.Print DBEngine(0)(0)(z)(y).Name
y = y + 1
Wend
If y > 1 Then
Err.Number = 0
y = 0
z = z + 1
End If
Wend
Re: VBA to list all field names in a table
am 10.11.2007 11:10:31 von Bob Quintal
"Tony Toews [MVP]" wrote in
news:fegaj3pob61ccf5o2cn3p17gfn4j2lucqm@4ax.com:
> Bob Quintal wrote:
>
>>Public Sub showtdf()
>>Dim db As Database
>>Dim tdf As tabledef
>>Dim x As Integer
>>Set db = CurrentDb
>>For Each tdf In db.TableDefs
>> Debug.Print tdf.Name,
>> Debug.Print tdf.Fields.Count
>> For x = 0 To tdf.Fields.Count - 1
>> Debug.Print tdf.Fields(x).Name,
>> Next x
>>Next tdf
>>End Sub
>
> Very minor nit. I don't have the code right in front of me but
> I'm sure you can go through the fields collection on a table
> object in the same way you went through the tableDefs object.
You are so right. I just copied some code I had in my library
instead of writing a new version for the original poster..
>
> Also the original poster want only one table's fields. And you
> can get right to that table without using a tabledefs collection.
> Although more likely would be a querydef.
>
Actually,, rereading the original message, I'm not sure that's what
the user wants, he's talking about automatically populating a Word
Doc from a table. He says he wants to scan the database for field
names, but I suspect he'll need to scan the .doc instead.
> But very minor nit picking.
>
> Tony
:-)
--
Bob Quintal
PA is y I've altered my email address.
--
Posted via a free Usenet account from http://www.teranews.com