Referencing a control on a form from function in module 
Author Message
 Referencing a control on a form from function in module

hi all,

i've set up a table which is used to show which records of
selected tables have been added or updated at what time.
the function which manages this resides in a module. the problem
is that when i try to reference a control on an open form
which calls the function
runtime error 438: object doesn't support property or method

the following code is the object of the problem and is referenced
in the afterupdate event of selected forms. for the purpose of keeping
this message as short as possible i've excluded the declarations.

Function RetUpdateInf()
strCurrentName = Application.CurrentObjectName
Set frm = Forms(strCurrentName)
strTblName = frm.RecordSource
lngNum = frm.CurrentRecord
Set dbs = CurrentDb
Set tdf = dbs.TableDefs(strTblName)
For Each idx In tdf.Indexes   'i only want the values of the prim key
If idx.Primary Then
For Each fld In idx.Fields
varPrimName = fld.Name  'works because each table has prims <=1
Next fld
End If
Next idx
If varPrimName = "" Then ' if there is no prim key
strPrimkeyVal = frm.Controls(0).Value & ", " & frm.Controls(1).Value
'strPrimkeyVal = frm.Controls(0).Value  'works because control has
'focus, i think?
'strPrimkeyVal = frm.Controls(1).Value  ' doesn't work --> error occurs
Else
strPrimkeyVal = frm.Controls(varPrimName).Value
End If
intLookup = DCount("[PrimKeyValue]", "tblUpdateInfo", _
"[PrimKeyValue] " & "= '" & strPrimkeyVal & "'")
If intLookup > 0 Then
strVal = "Updated"
Else
strVal = "Created"
End If
Set rst = dbs.OpenRecordset("tblUpdateInfo")
With rst
.AddNew
!Tablename = strTblName
!UpdateProperty = strVal
!RecordsetPosition = lngNum
!PrimkeyValue = strPrimkeyVal
!DateUpdated = Now
.Update
End With
rst.Close
End Function

Q: can a control be referenced from a function in a module?
or i am i just doing something stupid?

tia for any help

frank
-----



Sat, 15 Jul 2000 03:00:00 GMT  
 Referencing a control on a form from function in module

Hi,

I suppose the first control is an edit box and that the second control is a
label for that box, and a label don't have a value but a caption property,
so the error on Controls(1).Value since it is probably Controls(1).Caption.

Anyhow, you may see it is not always wise to use indexes, and it may be
preferable to use a string with the name of the control:

    frm(strWithTheNameOfTheControl)

and that will return the default property of that control.

Hope it may help,
Vanderghast, Access MVP.

Quote:

>hi all,

>i've set up a table which is used to show which records of
>selected tables have been added or updated at what time.
>the function which manages this resides in a module. the problem
>is that when i try to reference a control on an open form
>which calls the function
>runtime error 438: object doesn't support property or method

>the following code is the object of the problem and is referenced
>in the afterupdate event of selected forms. for the purpose of keeping
>this message as short as possible i've excluded the declarations.

>Function RetUpdateInf()
>strCurrentName = Application.CurrentObjectName
>Set frm = Forms(strCurrentName)
>strTblName = frm.RecordSource
>lngNum = frm.CurrentRecord
>Set dbs = CurrentDb
>Set tdf = dbs.TableDefs(strTblName)
>For Each idx In tdf.Indexes   'i only want the values of the prim key
>If idx.Primary Then
>For Each fld In idx.Fields
>varPrimName = fld.Name  'works because each table has prims <=1
>Next fld
>End If
>Next idx
>If varPrimName = "" Then ' if there is no prim key
>strPrimkeyVal = frm.Controls(0).Value & ", " & frm.Controls(1).Value
>'strPrimkeyVal = frm.Controls(0).Value  'works because control has
>'focus, i think?
>'strPrimkeyVal = frm.Controls(1).Value  ' doesn't work --> error occurs
>Else
>strPrimkeyVal = frm.Controls(varPrimName).Value
>End If
>intLookup = DCount("[PrimKeyValue]", "tblUpdateInfo", _
>"[PrimKeyValue] " & "= '" & strPrimkeyVal & "'")
>If intLookup > 0 Then
>strVal = "Updated"
>Else
>strVal = "Created"
>End If
>Set rst = dbs.OpenRecordset("tblUpdateInfo")
>With rst
>.AddNew
>!Tablename = strTblName
>!UpdateProperty = strVal
>!RecordsetPosition = lngNum
>!PrimkeyValue = strPrimkeyVal
>!DateUpdated = Now
>.Update
>End With
>rst.Close
>End Function

>Q: can a control be referenced from a function in a module?
>or i am i just doing something stupid?

>tia for any help

>frank
>-----



Sat, 15 Jul 2000 03:00:00 GMT  
 Referencing a control on a form from function in module

hi michel,
thanx for the reply,

i'd totally ignored the fact that a label is of course a control.
in other words i was doing something stupid!

thanks for guiding me in the right direction
as always

see ya

Quote:

> Hi,

> I suppose the first control is an edit box and that the second control is a
> label for that box, and a label don't have a value but a caption property,
> so the error on Controls(1).Value since it is probably Controls(1).Caption.

> Anyhow, you may see it is not always wise to use indexes, and it may be
> preferable to use a string with the name of the control:

>     frm(strWithTheNameOfTheControl)

> and that will return the default property of that control.

> Hope it may help,
> Vanderghast, Access MVP.


> >hi all,

> >i've set up a table which is used to show which records of
> >selected tables have been added or updated at what time.
> >the function which manages this resides in a module. the problem
> >is that when i try to reference a control on an open form
> >which calls the function
> >runtime error 438: object doesn't support property or method

> >the following code is the object of the problem and is referenced
> >in the afterupdate event of selected forms. for the purpose of keeping
> >this message as short as possible i've excluded the declarations.

> >Function RetUpdateInf()
> >strCurrentName = Application.CurrentObjectName
> >Set frm = Forms(strCurrentName)
> >strTblName = frm.RecordSource
> >lngNum = frm.CurrentRecord
> >Set dbs = CurrentDb
> >Set tdf = dbs.TableDefs(strTblName)
> >For Each idx In tdf.Indexes   'i only want the values of the prim key
> >If idx.Primary Then
> >For Each fld In idx.Fields
> >varPrimName = fld.Name  'works because each table has prims <=1
> >Next fld
> >End If
> >Next idx
> >If varPrimName = "" Then ' if there is no prim key
> >strPrimkeyVal = frm.Controls(0).Value & ", " & frm.Controls(1).Value
> >'strPrimkeyVal = frm.Controls(0).Value  'works because control has
> >'focus, i think?
> >'strPrimkeyVal = frm.Controls(1).Value  ' doesn't work --> error occurs
> >Else
> >strPrimkeyVal = frm.Controls(varPrimName).Value
> >End If
> >intLookup = DCount("[PrimKeyValue]", "tblUpdateInfo", _
> >"[PrimKeyValue] " & "= '" & strPrimkeyVal & "'")
> >If intLookup > 0 Then
> >strVal = "Updated"
> >Else
> >strVal = "Created"
> >End If
> >Set rst = dbs.OpenRecordset("tblUpdateInfo")
> >With rst
> >.AddNew
> >!Tablename = strTblName
> >!UpdateProperty = strVal
> >!RecordsetPosition = lngNum
> >!PrimkeyValue = strPrimkeyVal
> >!DateUpdated = Now
> >.Update
> >End With
> >rst.Close
> >End Function

> >Q: can a control be referenced from a function in a module?
> >or i am i just doing something stupid?

> >tia for any help

> >frank
> >-----



Sat, 15 Jul 2000 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Referencing the Form from Which a Module Function Is Called

2. Referencing Form Controls from a Module

3. referencing a form's control from a module

4. Referencing Access Queries that use Functions stored in a module from VB

5. Please help me reference an API function declaration from inside a module

6. Referencing objects on forms from Standard Modules?

7. Referencing Form Objects - Treeview in Modules

8. Referencing forms in module level code

9. Circular Reference Between Modules when 2 Class Modules Listen to Each others Events

10. Circular Reference Between Modules when 2 Class Modules Listen to Each others Events

11. passing forms and controls so I can set focus back to a form from a module

12. passing forms and controls so I can set focus back to a form from a module

 

 
Powered by phpBB® Forum Software