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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

When should I Dim frm as Form 3

Status
Not open for further replies.

NeilT123

Technical User
Jan 6, 2005
302
GB
The Aceman1 recently gave me some code to help with an issue that I was having which started as follows:

Code:
Private Function FilterMyForm()
   Dim frm As Form, strFilter As String
   
   Set frm = Me.frmIfSoilAnalResults.Form


Being self taught and more by trial and error rather than formal teaching in MS Access I am not sure of the significance of the Dim frm as Form.

Can anyone point me in the direction of any information / tutorials on why and when I should Dim frm as Form and then setting the form.

Failing that, is anyone prepared to give me some guidance? Should I always Dim frm as Form? What happens when I have a form and nested and unnested subforms, do I have to dim them all? Does each Dim override the previous Dim? What is the benefit of Dimming? Sorry that there are so many questions but I am keen to find out more.
 
hi,

In this case, it might just be pointless. I'd do something like this...
Code:
Private Function FilterMyForm()
   Dim strFilter As String
   
   With Me.frmIfSoilAnalResults.Form 
      'do stuff to form here

   End With
End Function


Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
If you reference an object more than once you may want to assign it to a variable to save a lot of typing. Especially if you want to pass it to another function. I find it is much easier to read and a lot easier to error check

So without seeing the code, not sure if anything is saved. But imagine your code calls a couple of functions.
Code:
Public sub someSub()
  
   call someProcedure(me.frmIfSoilAnalResults.form)
   me.frmIfSoilAnalResults.form.someProperty = someValue
   call someProcedure2(me.frmIfSoilAnalResults.form)
   me.frmIfSoilAnalResults.form.someOtherProperty = someValue2
....
or
Code:
Public Sub someSub()
  dim frm as form
  set frm = me.frmIfSoilAnalResults.form
  call someProcedure(frm)
  frm.someProperty = somevalue
  call someProcedure2(frm)
  frm.someotherProperty = somevalue2
end sub
 
How are ya NeilT123 . . .

There is a thing called readability when it comes to writing code. I'm in the habit of making my code easily readable for myself. Depending on how its written, code can be read or deciphered! Its when you have to decipher that code becomes a pain. Proper syntax, indenting, and use of low character count words, all work together (if done properly), to bring code to a readable level. I typically find my errors quite quickly.

Your case was a matter of readability. You initially had:
Me.frmIfSoilAnalResults.Requery
Me.frmIfSoilAnalResults.Form.Recordset.MoveFirst

which became:
frm.Requery
frm.Recordset.MoveFirst


Surely one is more easily readable than the other ...... see what I mean! So readable or not the choice is yours. Imagine 4 or 5 more of those nested on the same page ...

Your Thoughts? . . .

See Ya! . . . . . .

Be sure to see faq219-2884 [blue]Worthy Reading![/blue] [thumbsup2]
Also faq181-2886 [blue]Worthy Reading![/blue] [thumbsup2]
 
Thank you for the responses. This had been bugging me all weekend and google didn't help but now it now makes sense. I now know I can use Dim where I feel it appropriate to help with reading and checking code, particularly on some of my more complex forms but it is not essential.

Thanks again
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top