473,386 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Using fields and or form controls in a module

I came across a lot of repetitious code in a form module that I would like
to put in a module that I can call multiple times from a form. The problem
I have run into is that when I put the code into a module, the Control Names
and the Fields do not work as the module does not recognise them. Is there
a way around this, or do I just live with the repetition of code in the
form's module?

dixie
Nov 12 '05 #1
4 1904
If the field/control names are the same, but the form is different, you can
pass a reference to the into your generic procedure.

The function in the general module would be declared like this:
Public Function MyFunc(frm As Form)
Debug.Print frm![SomeControl]
End Function

That works just like:
Debug.Print Me.SomeControl
in the module of the form.

To call the function from your form's module:
Call MyFunc(Me)

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"dixie" <di****@dogmail.com> wrote in message
news:UT****************@nnrp1.ozemail.com.au...
I came across a lot of repetitious code in a form module that I would like
to put in a module that I can call multiple times from a form. The problem I have run into is that when I put the code into a module, the Control Names and the Fields do not work as the module does not recognise them. Is there a way around this, or do I just live with the repetition of code in the
form's module?

dixie

Nov 12 '05 #2
Dixie

You could put a sub or function in the form's module, or write the
code in a module in such a way that you can pass the value of a
control to the function.
i.e.
Function myFunction(myControlValue)

The latter method is the way that I would do it, as I tend to need
these things in more than one form.

neil
"dixie" <di****@dogmail.com> wrote in message news:<UT****************@nnrp1.ozemail.com.au>...
I came across a lot of repetitious code in a form module that I would like
to put in a module that I can call multiple times from a form. The problem
I have run into is that when I put the code into a module, the Control Names
and the Fields do not work as the module does not recognise them. Is there
a way around this, or do I just live with the repetition of code in the
form's module?

dixie

Nov 12 '05 #3
"dixie" <di****@dogmail.com> wrote in message news:<UT****************@nnrp1.ozemail.com.au>...
I came across a lot of repetitious code in a form module that I would like
to put in a module that I can call multiple times from a form. The problem
I have run into is that when I put the code into a module, the Control Names
and the Fields do not work as the module does not recognise them. Is there
a way around this, or do I just live with the repetition of code in the
form's module?

dixie


dixie,

Have you tried passing your controls as arguments to the methods in
you module?

-dp

p.s. If you're still having trouble, provide some sample code and I'd
be glad to help.
Nov 12 '05 #4
dp****@app-tech.com (Dave Perry) wrote in message news:<a0**************************@posting.google. com>...
"dixie" <di****@dogmail.com> wrote in message news:<UT****************@nnrp1.ozemail.com.au>...
I came across a lot of repetitious code in a form module that I would like
to put in a module that I can call multiple times from a form. The problem
I have run into is that when I put the code into a module, the Control Names
and the Fields do not work as the module does not recognise them. Is there
a way around this, or do I just live with the repetition of code in the
form's module?

dixie


dixie,

Have you tried passing your controls as arguments to the methods in
you module?

-dp


Here are a couple of module functions I use to keep users from typing
in more characters in the control than the table field is designed to
hold:

Public Function LimitField(KeyAscii As Integer, txtBox As TextBox,
intMax As Integer) As Integer
'If the field has reached its maximum then return 0
LimitField = KeyAscii
txtBox.SetFocus 'I forgot why I put this line here
If Len(txtBox.Text) = intMax Then
If KeyAscii <> 8 Then LimitField = 0
End If
End Function

Example call:
Private Sub txtCity_KeyPress(KeyAscii As Integer)
KeyAscii = LimitField(KeyAscii, txtCity, MaxAFieldWidth(iCity))
End Sub

Public Function LimitCombo(KeyAscii As Integer, cbxCombo As ComboBox,
intMax As Integer) As Integer
'If the field has reached its maximum then return 0
LimitCombo = KeyAscii
cbxCombo.SetFocus
If Len(cbxCombo.Text) = intMax Then
If KeyAscii <> 8 Then LimitCombo = 0
End If
End Function

Example call:
Private Sub cbxContact_KeyPress(KeyAscii As Integer)
KeyAscii = LimitCombo(KeyAscii, cbxContact, 50)
End Sub

The '8' is so that they're not in a cul-de-sac if they hit the limit.
I probably got the idea for these functions from code on this NG.

James A. Fortune
Nov 12 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: John F Dutcher | last post by:
I use code like the following to retrieve fields from a form: recd = recd.append(string.ljust(form.getfirst("lname",' '),15)) recd.append(string.ljust(form.getfirst("fname",' '),15)) etc.,...
5
by: Carlos Ribeiro | last post by:
Hello all, I'm posting this to the list with the intention to form a group of people interested in this type of solution. I'm not going to spam the list with it, unless for occasional and...
1
by: orit | last post by:
I have the following xml file: <?xml version="1.0" encoding="utf-8" ?> <course id="2555" title="Developing Microsoft .NET Applications for Windows (Visual C# .NET)" length="5 days"...
5
by: Deborah V. Gardner | last post by:
I would like to use "Yes" and "No" checkboxes on a subform. The problem is that when I click the Yes checkbox on the subform, all of the checkboxes are checked. Currently, I have a field...
19
by: James Fortune | last post by:
I have a lot of respect for David Fenton and Allen Browne, but I don't understand why people who know how to write code to completely replace a front end do not write something that will automate...
4
by: Kathy | last post by:
What is the standard technique for handling the fields in the following scenario on a continuous form? Multiple Divisions. Each Division has multiple Buildings. Each Building has a Supervisor. ...
0
by: samlee | last post by:
Hi All, I'm learning how to write C# using reflection, but don't know how to code using reflection this.Controls.Add(this.label1); Could anyone help, Thank in advance. ...
3
by: google | last post by:
I'm developing an application for use within my company in Access 2003. I'm new to '03, the application I did for my former employer was in '97. The two applications have similar functionality...
21
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Uploading files from a local computer to a remote web server has many useful purposes, the most...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.