Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsFormsForms ProgrammingQueriesModules / DAO / VBAReports / PrintingMacrosDatabase DesignSecurityConversionImporting / LinkingSQL Server / ADPMultiuser / NetworkingReplicationSetup / ConfigurationDeveloper ToolkitsActiveX ControlsNew UsersGeneral 1General 2
Access DirectoryToolsTutorialsUser Groups
Related Topics
SQL ServerOther DB ProductsMS OfficeMore Topics ...

MS Access Forum / Forms / February 2008

Tip: Looking for answers? Try searching our database.

Retyping reference to formfield in module

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Maarten - 13 Feb 2008 08:56 GMT
I've got this annoying thing where the reference to formfields are not
recognised in the VBA module if I refer to them by their name only. I
need to include the reference to the form. Is there a workaround,
would save me a lot of typing?
Example:
I've got a form (frm_AAA) with two text fields str_Greet and Result.
Apart from the coding in the object area of the form I created a
separate module to keep things clear. In this module there a sub which
calculates the color of the field Result (Calculation)

The code I in the object area:
Private Sub str_Greet_AfterUpdate()
    Calculation
End Sub

And the module needs to be coded as follow:
Sub Calculation()
    If str_Greet = Hello Then frm_AAA.Resultaat.BackColor = RBG(0,255,0)
    Else If str_Greet = Cheers Then frm_AAA.Resultaat.BackColor =
RGB(255,0,0)
    Else: MsgBox("Sorry, I can't hear you")
    End If
End Sub

While I rather type:
Sub Calculation()
    If str_Greet = Hello Then Resultaat.BackColor = RBG(0,255,0)
    Else If str_Greet = Cheers Then Resultaat.BackColor = RGB(255,0,0)
    Else: MsgBox("DSorry, I can't hear you")
    End If
End Sub

Althoug the difference is minimal for this code, it definately makes
sense on 500+ lines of code.

Any help welcome, thanks.
Maarten
Jon Lewis - 13 Feb 2008 13:14 GMT
You could try passing the Form as a parameter to your Sub like this: (not
tested)

Sub Calculation(oForm As Form)
If str_Greet = "Hello" Then oForm.Resultaat.BackColor = RBG(0,255,0)
Else If str_Greet = "Cheers" Then oForm.Resultaat.BackColor =
RGB(255,0,0)
Else: MsgBox("Sorry, I can't hear you")
End If
End Sub

Then when you call the sub:
(Assuming that Resultaat is on the same form)

Private Sub str_Greet_AfterUpdate()
Calculation (Me)
End Sub

Also how does Sub Calculation() know what str_Greet  is?  Are you storing
this in a global variable? You might want to pass this value in the same way
too, e.g.:

Sub Calculation(strGreet As String, oForm As Form)
If strGreet = "Hello" Then oForm.Resultaat.BackColor = RBG(0,255,0)
Else If strGreet = "Cheers" Then oForm.Resultaat.BackColor =
RGB(255,0,0)
Else: MsgBox("Sorry, I can't hear you")
End If
End Sub

Private Sub str_Greet_AfterUpdate()
Calculation (str_Greet, Me)
End Sub

> I've got this annoying thing where the reference to formfields are not
> recognised in the VBA module if I refer to them by their name only. I
[quoted text clipped - 33 lines]
> Any help welcome, thanks.
> Maarten
Maarten - 14 Feb 2008 10:21 GMT
Hi Jon, thanks for your help.

sorry for the errors in my start post, i wrote it directly into the
newsgroup window, without checking it in VBA.
Where I said 'Resultaat' it should have been 'Result' (Same word,
different language)
str_Greet is also a text field in the form. So the original
programming should have been 'frm_AAA.str_Greet' in stead of just
'str_Greet'
Thanks for your careful reading!

Unfortunately I can't get this to work. I tried the first option (with
only oForm as Form). It returns a type mismatch on "Calculation (Me)"
I gave it a try on using different types (Forms, Variant, ..., either
if it made sense to me or not)  and replaced Me in "Calculation (Me)"
by frm_AAA, Form_frm_AAA and Me!.

Any clue?
Maarten - 14 Feb 2008 10:26 GMT
Hi Jon, thanks for your help.

sorry for the errors in my start post, i wrote it directly into the
newsgroup window, without checking it in VBA.
Where I said 'Resultaat' it should have been 'Result' (Same word,
different language)
str_Greet is also a text field in the form. So the original
programming should have been 'frm_AAA.str_Greet' in stead of just
'str_Greet'
Thanks for your careful reading!

Unfortunately I can't get this to work. I tried the first option (with
only oForm as Form). It returns a type mismatch on "Calculation (Me)"
I gave it a try on using different types (Forms, Variant, ..., either
if it made sense to me or not)  and replaced Me in "Calculation (Me)"
by frm_AAA, Form_frm_AAA and Me!.

Any clue?
Maarten - 14 Feb 2008 10:27 GMT
Hi Jon, thanks for your help.

sorry for the errors in my start post, i wrote it directly into the
newsgroup window, without checking it in VBA.
Where I said 'Resultaat' it should have been 'Result' (Same word,
different language)
str_Greet is also a text field in the form. So the original
programming should have been 'frm_AAA.str_Greet' in stead of just
'str_Greet'
Thanks for your careful reading!

Unfortunately I can't get this to work. I tried the first option (with
only oForm as Form). It returns a type mismatch on "Calculation (Me)"
I gave it a try on using different types (Forms, Variant, ..., either
if it made sense to me or not)  and replaced Me in "Calculation (Me)"
by frm_AAA, Form_frm_AAA and Me!.

Any clue?
Maarten - 14 Feb 2008 10:31 GMT
Hi Jon, thanks for your help.

sorry for the errors in my start post, i wrote it directly into the
newsgroup window, without checking it in VBA.
Where I said 'Resultaat' it should have been 'Result' (Same word,
different language)
str_Greet is also a text field in the form. So the original
programming should have been 'frm_AAA.str_Greet' in stead of just
'str_Greet'
Thanks for your careful reading!

Unfortunately I can't get this to work. I tried the first option (with
only oForm as Form). It returns a type mismatch on "Calculation (Me)"
I gave it a try on using different types (Forms, Variant, ..., either
if it made sense to me or not)  and replaced Me in "Calculation (Me)"
by frm_AAA, Form_frm_AAA and Me!.

Any clue?
Maarten - 14 Feb 2008 10:39 GMT
Hi Jon, thanks for your help.

sorry for the errors in my start post, i wrote it directly into the
newsgroup window, without checking it in VBA.
Where I said 'Resultaat' it should have been 'Result' (Same word,
different language)
str_Greet is also a text field in the form. So the original
programming should have been 'frm_AAA.str_Greet' in stead of just
'str_Greet'
Thanks for your careful reading!

Unfortunately I can't get this to work. I tried the first option (with
only oForm as Form). It returns a type mismatch on "Calculation (Me)"
I gave it a try on using different types (Forms, Variant, ..., either
if it made sense to me or not)  and replaced Me in "Calculation (Me)"
by frm_AAA, Form_frm_AAA and Me!.

Any clue?
Maarten - 14 Feb 2008 10:52 GMT
Hi Jon, thanks for your help.

sorry for the errors in my start post, i wrote it directly into the
newsgroup window, without checking it in VBA.
Where I said 'Resultaat' it should have been 'Result' (Same word,
different language)
str_Greet is also a text field in the form. So the original
programming should have been 'frm_AAA.str_Greet' in stead of just
'str_Greet'
Thanks for your careful reading!

Unfortunately I can't get this to work. I tried the first option (with
only oForm as Form). It returns a type mismatch on "Calculation (Me)"
I gave it a try on using different types (Forms, Variant, ..., either
if it made sense to me or not)  and replaced Me in "Calculation (Me)"
by frm_AAA, Form_frm_AAA and Me!.

Any clue?
Jon Lewis - 14 Feb 2008 11:38 GMT
Sorry - my mistake!  You don't need brackets.

Calculation Me
or
Calculation str_Greet, Me

HTH
Jon

> Hi Jon, thanks for your help.
>
[quoted text clipped - 14 lines]
>
> Any clue?
Maarten - 14 Feb 2008 13:12 GMT
Yes it does, it works :) Thanks a bunch! that'll save me quite some
typing.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.