Variable table/field name in statement

S

Stu

The following statement works fine for me with the table name TABLE1 and
field name FIELD1 hardcoded:
iType = CurrentDb.TableDefs!TABLE1.Fields!FIELD1.Properties!Type

Is there a way to make the table name and field name variable, something
like the following? What would go in for the question marks?

sTableName = "MyTable"
sFieldName = "MyField"
itype = CurrentDb.TableDefs!???????.Fields!??????.Properties!Type
 
S

Stu

The statement CurrentDb.TableDefs(sTableName).Fields(sFieldName) gives me
an "invalid operation" error. (#3219)
 
D

Douglas J. Steele

Exactly how did you use that expression? I was simply trying to show you the
syntax.

Going back to your original equation, it would be

itype = CurrentDb.TableDefs(sTableName).Fields(sFieldName).Properties!Type
 
S

Stu

Sub test1()
sTableName = "tblAudit"
sFieldName = "audType"
sdesc = CurrentDb.TableDefs(sTableName).Fields(sFieldName)
MsgBox sdesc
End Sub
 
D

Douglas J. Steele

CurrentDb.TableDefs(sTableName).Fields(sFieldName) is a reference to the
Field object. The only way to use it by itself would be:

Dim fldCurr As Field

Set fldCurr = CurrentDb.TableDefs(sTableName).Fields(sFieldName)

If you want a property of that object, you need to refer to the property.
Assuming you're trying to get the Description property, your code needs to
be:

Sub test1()
Dim sTableName As String
Dim sFieldName As String
Dim sdesc As String

sTableName = "tblAudit"
sFieldName = "audType"
sdesc =
CurrentDb.TableDefs(sTableName).Fields(sFieldName).Properties("Description")
MsgBox sdesc
End Sub

Note that Description is an odd property: it doesn't actually exist unless a
description has been assigned to the field. If you hadn't actually assigned
a description to the field, you'll get an error 3270 ("Property not found.")
A common way to handle that, then, would be

Sub test1()
On Error GoTo ErrHandler

Dim sTableName As String
Dim sFieldName As String
Dim sdesc As String

sTableName = "tblAudit"
sFieldName = "audType"
sdesc =
CurrentDb.TableDefs(sTableName).Fields(sFieldName).Properties("Description")

ExitHere:
MsgBox sdesc
Exit Sub

ErrHandler:
Select Case Err.Number
Case 3270
sdesc = "***No Description Given***"
Case Else
MsgBox "Error " & Err.Number & ": " & Err.Description
End Select
Resume ExitHere

End Sub

I'm assuming from your code that you haven't told Access to require that all
variables be declared. To me, that's a huge mistake. Go into the VB Editor
and select Tools | Options from the menu. On the Editor tab, make sure that
the "Require Variable Declaration" check box is selected. That will ensure
that all new modules will have a line "Option Explicit" near the top.
(Unfortunately, you'll have to go into all existing modules and add that
line yourself). While it may seem a pain having to declare each variable, it
can save you hours in the long run trying to track down why your code isn't
doing what it's supposed to when you accidentally mistyped one of the
variable names!
 
S

Stu

OOPS, bad cut 'n paste. It works fine. Thanks

Stu said:
Sub test1()
sTableName = "tblAudit"
sFieldName = "audType"
sdesc = CurrentDb.TableDefs(sTableName).Fields(sFieldName)
MsgBox sdesc
End Sub
 
S

Stu

Got it. Case closed. Thanks

Douglas J. Steele said:
CurrentDb.TableDefs(sTableName).Fields(sFieldName) is a reference to the
Field object. The only way to use it by itself would be:

Dim fldCurr As Field

Set fldCurr = CurrentDb.TableDefs(sTableName).Fields(sFieldName)

If you want a property of that object, you need to refer to the property.
Assuming you're trying to get the Description property, your code needs to
be:

Sub test1()
Dim sTableName As String
Dim sFieldName As String
Dim sdesc As String

sTableName = "tblAudit"
sFieldName = "audType"
sdesc =
CurrentDb.TableDefs(sTableName).Fields(sFieldName).Properties("Description")
MsgBox sdesc
End Sub

Note that Description is an odd property: it doesn't actually exist unless a
description has been assigned to the field. If you hadn't actually assigned
a description to the field, you'll get an error 3270 ("Property not found.")
A common way to handle that, then, would be

Sub test1()
On Error GoTo ErrHandler

Dim sTableName As String
Dim sFieldName As String
Dim sdesc As String

sTableName = "tblAudit"
sFieldName = "audType"
sdesc =
CurrentDb.TableDefs(sTableName).Fields(sFieldName).Properties("Description")

ExitHere:
MsgBox sdesc
Exit Sub

ErrHandler:
Select Case Err.Number
Case 3270
sdesc = "***No Description Given***"
Case Else
MsgBox "Error " & Err.Number & ": " & Err.Description
End Select
Resume ExitHere

End Sub

I'm assuming from your code that you haven't told Access to require that all
variables be declared. To me, that's a huge mistake. Go into the VB Editor
and select Tools | Options from the menu. On the Editor tab, make sure that
the "Require Variable Declaration" check box is selected. That will ensure
that all new modules will have a line "Option Explicit" near the top.
(Unfortunately, you'll have to go into all existing modules and add that
line yourself). While it may seem a pain having to declare each variable, it
can save you hours in the long run trying to track down why your code isn't
doing what it's supposed to when you accidentally mistyped one of the
variable names!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top