473,387 Members | 1,548 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,387 software developers and data experts.

Simplifying code in a Function Module.

Hello All,
I was wondering if it is possible to do this; I have a form that has
number of text boxes that when a button is clicked it turns into combo
boxes, simply by toggling the visibility to the box. For example 3
textboxes, txtA, txtB, and txtC, and 3 combo boxes, cmbA, cmbB, and
cmbC. Now when the form is open all three of the text boxes are
visible, and the combo boxes are invisible, until a button is pressed,
and the text boxes become invisible, and the combo boxes become
visible.

As it stand the cold for the button goes like this:

Private Sub Toggle_Click()

If Forms!Form1.txtA.visible = True Then
Forms!Form1.txtA.visible = False
Forms!Form1.cmbA.visible = True
Else
Forms!Form1.txtA.visible = True
Forms!Form1.cmbA.visible = False
End If
If Forms!Form1.txtB.visible = True Then
Forms!Form1.txtB.visible = False
Forms!Form1.cmbB.visible = True
Else
Forms!Form1.txtB.visible = True
Forms!Form1.cmbB.visible = False
End If
If Forms!Form1.txtC.visible = True Then
Forms!Form1.txtC.visible = False
Forms!Form1.cmbC.visible = True
Else
Forms!Form1.txtC.visible = True
Forms!Form1.cmbC.visible = False
End If

End Sub

Now my question is, if there is a way to simplify this code using a
Function Module, and passing the variable names as the parameters.
Nov 12 '05 #1
4 2688
I wouldn't find placing this in a module to be a lot easier unless you have a lot of these
buttons so that the code is being repeated a number of times. Basically, whatever is
easiest to type in and maintain. If you need to make changes, it is sometimes easier to
change things if it is all in one location. This assumes that the changes will always be
applied to all the connecting objects.

What you may find simpler (a little less typing) is replacing the Form call with Me if the
code is running on the same form it is referring to. If not, you could shorten up your
typing by creating a variable to hold part of the name.

Example:
Me.txtA.Visible
or
Dim frm As Form
Set frm = Forms!Form1
frm.txtA.Visible
......
Set frm = Nothing

Another thing that may shorten this is:

With Me
.txtA.Visible = Not .txtA.Visible
.cmbA.Visible = Not .txtA.Visible
.txtB.Visible = Not .txtB.Visible
.cmbB.Visible = Not .txtB.Visible
.txtC.Visible = Not .txtC.Visible
.cmbC.Visible = Not .cmbC.Visible
End With

If you went with the frm variable, you would replace Me in the With statement with frm.

If you had many more of these, I would be tempted to replace A, B, and C with 1, 2, and 3
and so on. You could then run a loop to toggle them.

Example:
With Me
For i = 1 To 3
.Controls("txt" & i).Visible = Not .Controls("txt" & i).Visible
.Controls("cmb" & i).Visible = Not .Controls("txt" & i).Visible
Next i
End With

--
Wayne Morgan
Microsoft Access MVP
"Bobbak" <bo****@ottawa.com> wrote in message
news:64**************************@posting.google.c om...
Hello All,
I was wondering if it is possible to do this; I have a form that has
number of text boxes that when a button is clicked it turns into combo
boxes, simply by toggling the visibility to the box. For example 3
textboxes, txtA, txtB, and txtC, and 3 combo boxes, cmbA, cmbB, and
cmbC. Now when the form is open all three of the text boxes are
visible, and the combo boxes are invisible, until a button is pressed,
and the text boxes become invisible, and the combo boxes become
visible.

As it stand the cold for the button goes like this:

Private Sub Toggle_Click()

If Forms!Form1.txtA.visible = True Then
Forms!Form1.txtA.visible = False
Forms!Form1.cmbA.visible = True
Else
Forms!Form1.txtA.visible = True
Forms!Form1.cmbA.visible = False
End If
If Forms!Form1.txtB.visible = True Then
Forms!Form1.txtB.visible = False
Forms!Form1.cmbB.visible = True
Else
Forms!Form1.txtB.visible = True
Forms!Form1.cmbB.visible = False
End If
If Forms!Form1.txtC.visible = True Then
Forms!Form1.txtC.visible = False
Forms!Form1.cmbC.visible = True
Else
Forms!Form1.txtC.visible = True
Forms!Form1.cmbC.visible = False
End If

End Sub

Now my question is, if there is a way to simplify this code using a
Function Module, and passing the variable names as the parameters.

Nov 12 '05 #2
Bobbak wrote:
Hello All,
I was wondering if it is possible to do this; I have a form that has
number of text boxes that when a button is clicked it turns into combo
boxes, simply by toggling the visibility to the box. For example 3
textboxes, txtA, txtB, and txtC, and 3 combo boxes, cmbA, cmbB, and
cmbC. Now when the form is open all three of the text boxes are
visible, and the combo boxes are invisible, until a button is pressed,
and the text boxes become invisible, and the combo boxes become
visible.

As it stand the cold for the button goes like this:

Private Sub Toggle_Click()

If Forms!Form1.txtA.visible = True Then
Forms!Form1.txtA.visible = False
Forms!Form1.cmbA.visible = True
Else
Forms!Form1.txtA.visible = True
Forms!Form1.cmbA.visible = False
End If
If Forms!Form1.txtB.visible = True Then
Forms!Form1.txtB.visible = False
Forms!Form1.cmbB.visible = True
Else
Forms!Form1.txtB.visible = True
Forms!Form1.cmbB.visible = False
End If
If Forms!Form1.txtC.visible = True Then
Forms!Form1.txtC.visible = False
Forms!Form1.cmbC.visible = True
Else
Forms!Form1.txtC.visible = True
Forms!Form1.cmbC.visible = False
End If

End Sub

Now my question is, if there is a way to simplify this code using a
Function Module, and passing the variable names as the parameters.


Sure you could use a sub procedure to do this, but it may
not buy you much if you'd just rewrite the code. I believe
this will do the same as the code you posted:

Me.txtA.Visible = Not Me.txtA.Visible
Me.cmbA.visible = Not Me.txtA.Visible
Me.txtB.Visible = Not Me.txtB.Visible
Me.cmbB.visible = Not Me.txtB.Visible
Me.txtC.Visible = Not Me.txtC.Visible
Me.cmbC.visible = Not Me.txtC.Visible

--
Marsh
MVP [MS Access]
Nov 12 '05 #3
You can certainly simplify the code, though I can't see any advantages to
moving it to a standard code module. In the form module you can use the Me
keyword to refer to the current form. Something like:

Dim blnComboVisible as Boolean

With Me
blnComboVisible = .txtA.Visible
.txtA.Visible = Not blnComboVisible
.txtB.Visible = Not blnComboVisible
.txtC.Visible = Not blnComboVisible
.cmbA.Visible = blnComboVisible
.cmbB.Visible = blnComboVisible
.cmbC.Visible = blnComboVisible
End with

If you renamed your controls txt1, txt2 etc then you could shorten the code
a bit more:

Dim blnComboVisible as Boolean
Dim i as Integer

blnComboVisible = Me.txtA.Visible
For i = 1 to 3
Me("txt" & i).Visible = Not blnComboVisible
Me("cmb" & i).Visible = blnComboVisible
Next
"Bobbak" <bo****@ottawa.com> wrote in message
news:64**************************@posting.google.c om...
Hello All,
I was wondering if it is possible to do this; I have a form that has
number of text boxes that when a button is clicked it turns into combo
boxes, simply by toggling the visibility to the box. For example 3
textboxes, txtA, txtB, and txtC, and 3 combo boxes, cmbA, cmbB, and
cmbC. Now when the form is open all three of the text boxes are
visible, and the combo boxes are invisible, until a button is pressed,
and the text boxes become invisible, and the combo boxes become
visible.

As it stand the cold for the button goes like this:

Private Sub Toggle_Click()

If Forms!Form1.txtA.visible = True Then
Forms!Form1.txtA.visible = False
Forms!Form1.cmbA.visible = True
Else
Forms!Form1.txtA.visible = True
Forms!Form1.cmbA.visible = False
End If
If Forms!Form1.txtB.visible = True Then
Forms!Form1.txtB.visible = False
Forms!Form1.cmbB.visible = True
Else
Forms!Form1.txtB.visible = True
Forms!Form1.cmbB.visible = False
End If
If Forms!Form1.txtC.visible = True Then
Forms!Form1.txtC.visible = False
Forms!Form1.cmbC.visible = True
Else
Forms!Form1.txtC.visible = True
Forms!Form1.cmbC.visible = False
End If

End Sub

Now my question is, if there is a way to simplify this code using a
Function Module, and passing the variable names as the parameters.

Nov 12 '05 #4
You can greatly simplify this if you group related controls together on
a tab control with tabs turned off. This way you can simply hide or show
all controls that are on the same tab page using one line of code
(unless of course you want to be able to show them separately). I use
this extensively to avoid switchboards and utilize the same screen real
estate of a form.

Cheers,
Pavel

Bobbak wrote:

Hello All,
I was wondering if it is possible to do this; I have a form that has
number of text boxes that when a button is clicked it turns into combo
boxes, simply by toggling the visibility to the box. For example 3
textboxes, txtA, txtB, and txtC, and 3 combo boxes, cmbA, cmbB, and
cmbC. Now when the form is open all three of the text boxes are
visible, and the combo boxes are invisible, until a button is pressed,
and the text boxes become invisible, and the combo boxes become
visible.

As it stand the cold for the button goes like this:

Private Sub Toggle_Click()

If Forms!Form1.txtA.visible = True Then
Forms!Form1.txtA.visible = False
Forms!Form1.cmbA.visible = True
Else
Forms!Form1.txtA.visible = True
Forms!Form1.cmbA.visible = False
End If

If Forms!Form1.txtB.visible = True Then
Forms!Form1.txtB.visible = False
Forms!Form1.cmbB.visible = True
Else
Forms!Form1.txtB.visible = True
Forms!Form1.cmbB.visible = False
End If

If Forms!Form1.txtC.visible = True Then
Forms!Form1.txtC.visible = False
Forms!Form1.cmbC.visible = True
Else
Forms!Form1.txtC.visible = True
Forms!Form1.cmbC.visible = False
End If

End Sub

Now my question is, if there is a way to simplify this code using a
Function Module, and passing the variable names as the parameters.

Nov 12 '05 #5

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

Similar topics

2
by: Bryan Olson | last post by:
The current Python standard library provides two cryptographic hash functions: MD5 and SHA-1 . The authors of MD5 originally stated: It is conjectured that it is computationally infeasible to...
9
by: chapolim-colorado | last post by:
I like to keep my classes each in a separate file with the same name of the class. The problem with that is that I end up with multiple imports in the beginning of each file, like this: from...
5
by: news | last post by:
Well, I wrote my first PHP class today. Yeah! But to get it to work, in each function within the class I have to repeat the database connection lines, and that just seems redundant; there has to...
4
by: Terencetrent | last post by:
I having been using Access '97/2002 for about 4 years now and have never really had the need or the time to learn visual basic. Well, I think the time has finally come. I need help with Visual...
8
by: goldfita | last post by:
I love this place. This one problem keeps coming back to bite me. Suppose I have the following situation (actually I do). func1() { codeA somefunc1() codeB }
3
by: Mark | last post by:
Hi All, I have wrote a sub to record events from within the database. A lot of these events are errors. The sub has the module name and function/sub name passed to it to record to the table. My...
3
by: peterhall | last post by:
In VBA an Access module has a find method - works perfectly to find a string inside a module. i'm working in A97 (legacy) systems (large ones) and want to write code that searches all modules so that...
9
by: serdar | last post by:
Hi, I'll use a function called A-weighting Filter for a spectrum analyzer I'm designing in Flash. Here is the equation of A-Weighting Filter: ...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.