473,669 Members | 2,449 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.txt A.visible = True Then
Forms!Form1.txt A.visible = False
Forms!Form1.cmb A.visible = True
Else
Forms!Form1.txt A.visible = True
Forms!Form1.cmb A.visible = False
End If
If Forms!Form1.txt B.visible = True Then
Forms!Form1.txt B.visible = False
Forms!Form1.cmb B.visible = True
Else
Forms!Form1.txt B.visible = True
Forms!Form1.cmb B.visible = False
End If
If Forms!Form1.txt C.visible = True Then
Forms!Form1.txt C.visible = False
Forms!Form1.cmb C.visible = True
Else
Forms!Form1.txt C.visible = True
Forms!Form1.cmb C.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 2701
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.Visibl e
......
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.goo gle.com...
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.txt A.visible = True Then
Forms!Form1.txt A.visible = False
Forms!Form1.cmb A.visible = True
Else
Forms!Form1.txt A.visible = True
Forms!Form1.cmb A.visible = False
End If
If Forms!Form1.txt B.visible = True Then
Forms!Form1.txt B.visible = False
Forms!Form1.cmb B.visible = True
Else
Forms!Form1.txt B.visible = True
Forms!Form1.cmb B.visible = False
End If
If Forms!Form1.txt C.visible = True Then
Forms!Form1.txt C.visible = False
Forms!Form1.cmb C.visible = True
Else
Forms!Form1.txt C.visible = True
Forms!Form1.cmb C.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.txt A.visible = True Then
Forms!Form1.txt A.visible = False
Forms!Form1.cmb A.visible = True
Else
Forms!Form1.txt A.visible = True
Forms!Form1.cmb A.visible = False
End If
If Forms!Form1.txt B.visible = True Then
Forms!Form1.txt B.visible = False
Forms!Form1.cmb B.visible = True
Else
Forms!Form1.txt B.visible = True
Forms!Form1.cmb B.visible = False
End If
If Forms!Form1.txt C.visible = True Then
Forms!Form1.txt C.visible = False
Forms!Form1.cmb C.visible = True
Else
Forms!Form1.txt C.visible = True
Forms!Form1.cmb C.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.goo gle.com...
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.txt A.visible = True Then
Forms!Form1.txt A.visible = False
Forms!Form1.cmb A.visible = True
Else
Forms!Form1.txt A.visible = True
Forms!Form1.cmb A.visible = False
End If
If Forms!Form1.txt B.visible = True Then
Forms!Form1.txt B.visible = False
Forms!Form1.cmb B.visible = True
Else
Forms!Form1.txt B.visible = True
Forms!Form1.cmb B.visible = False
End If
If Forms!Form1.txt C.visible = True Then
Forms!Form1.txt C.visible = False
Forms!Form1.cmb C.visible = True
Else
Forms!Form1.txt C.visible = True
Forms!Form1.cmb C.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.txt A.visible = True Then
Forms!Form1.txt A.visible = False
Forms!Form1.cmb A.visible = True
Else
Forms!Form1.txt A.visible = True
Forms!Form1.cmb A.visible = False
End If

If Forms!Form1.txt B.visible = True Then
Forms!Form1.txt B.visible = False
Forms!Form1.cmb B.visible = True
Else
Forms!Form1.txt B.visible = True
Forms!Form1.cmb B.visible = False
End If

If Forms!Form1.txt C.visible = True Then
Forms!Form1.txt C.visible = False
Forms!Form1.cmb C.visible = True
Else
Forms!Form1.txt C.visible = True
Forms!Form1.cmb C.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
3778
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 produce two messages having the same message digest. That conjecture is false, as demonstrated by Wang, Feng, Lai and Yu in 2004 . Just recently, Wang, Yu, and Lin showed a short- cut solution for finding collisions in SHA-1 . Their result
9
1506
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 foo.Bar import Bar from foo.Blah import Blah from foo.Zzz import Zzz What I'd like to do would be to replace it all by a single line:
5
3018
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 be a better way that I'm just not bright enough to think of. Any suggestions? (It's the first 3 lines of each of the two functions below. When I have it fully written, there will be about 10 similar functions, each repeating those three lines.)
4
2823
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 Basic code that will examine numeric value for a particular field in a query, and assign a new numeric vaule to that field. There are over 21 possible values and I am told that IIF statement will only handle 9 of the possibilities and that I need...
8
1939
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
3081
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 problem is that a lot of the subs within the modules are quite large so haveing the module name and procedure name points me in the right direction but I would like to be able to record which line of code has produced the error. Is this possible?
3
5831
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 I can check on the possible effects of a change. But the 97 modules collection is open modules only. Access 97 doesn't have an allmodules collection - 2000+ does - so I can move the app up for this purpose. But now I find that a module in...
9
6651
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: http://img381.imageshack.us/img381/738/image001fi8.gif I thought the function is a little heavy to compute (for hundreds of values) realtime in Actionscript. I won't need a precise computation of the a-weighting filter.
0
8465
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8383
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8894
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8803
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8658
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7407
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4206
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4384
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2792
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.