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

clear all controls on a subform

imrosie
222 100+
Hello,

Does anyone have a simple cmd to clear all controls on Subform without using the SetFocus? Two of the controls are expressions, so they can't be set to Null. Then after clear return control to the main form?

thanks
Rosie
Sep 20 '07 #1
6 5264
Jim Doherty
897 Expert 512MB
Hello,

Does anyone have a simple cmd to clear all controls on Subform without using the SetFocus? Two of the controls are expressions, so they can't be set to Null. Then after clear return control to the main form?

thanks
Rosie
Hi Rosie,

Call the following sub function from a button mounted on the main form. As you examine the sub function and calling sub it you will see that it loops through controls that it is working with (IN THE SUBFORM)

It sets to null all those fields where your textbox naming convention first three characters is 'txt' and skips over those controls it can't set to NULL without reporting an error hence the On Error Resume Next statement

(You could also achieve this using 'TypeOf' for txtbox control identification if you wished to but I am assuming you may wish to be more specific in the controls you wish to work with)

This example merely targets only those controls you specifically want to point to, hence the 'txt' prefix.

When you click the button in the main form the actual focus remains on the main forms command button.

If you wanted then to advance to a specific control after the clear click event you could include a command to setfocus or Goto a specific control of your choosing

Expand|Select|Wrap|Line Numbers
  1.  
  2. Sub BlankTXTControls(f As Form)
  3. ' Copies Null to all controls on the form
  4. ' Assumes control names are prefixed by 'txt'
  5. On Error GoTo Err_BlankTXTControls
  6. Dim i As Integer, C As Control
  7. For i = 0 To f.Count - 1
  8.     Set C = f(i)
  9.     If Left$(C.Name, 3) = "txt" Then
  10.      C = Null
  11.     End If
  12. Next i
  13. Exit_BlankTXTControls:
  14.     Exit Sub 
  15. Err_BlankTXTControls:
  16.     MsgBox Err.Description
  17.     Resume Exit_BlankTXTControls
  18. End Sub
  19.  
You then use the following code in the on click event of the cmdclear button which as you can see passes the name of the subform (in this case the subform is named Submain) and makes the CALL to the sub function doing the work.

Expand|Select|Wrap|Line Numbers
  1.  Private Sub cmdClear_Click()
  2. On Error Resume Next 
  3. Call BlankTXTControls(Me!Submain.Form)
  4.  
  5. 'and here if you wanted to then advance to another control
  6. 'you would enter the setfocus command or the DoCmd GotoControl
  7. 'of your choosing
  8.  
  9. End Sub
  10.  
Regards

Jim
Sep 21 '07 #2
imrosie
222 100+
Hi Rosie,

Call the following sub function from a button mounted on the main form. As you examine the sub function and calling sub it you will see that it loops through controls that it is working with (IN THE SUBFORM)

It sets to null all those fields where your textbox naming convention first three characters is 'txt' and skips over those controls it can't set to NULL without reporting an error hence the On Error Resume Next statement

(You could also achieve this using 'TypeOf' for txtbox control identification if you wished to but I am assuming you may wish to be more specific in the controls you wish to work with)

This example merely targets only those controls you specifically want to point to, hence the 'txt' prefix.

When you click the button in the main form the actual focus remains on the main forms command button.

If you wanted then to advance to a specific control after the clear click event you could include a command to setfocus or Goto a specific control of your choosing

Expand|Select|Wrap|Line Numbers
  1.  
  2. Sub BlankTXTControls(f As Form)
  3. ' Copies Null to all controls on the form
  4. ' Assumes control names are prefixed by 'txt'
  5. On Error GoTo Err_BlankTXTControls
  6. Dim i As Integer, C As Control
  7. For i = 0 To f.Count - 1
  8.     Set C = f(i)
  9.     If Left$(C.Name, 3) = "txt" Then
  10.      C = Null
  11.     End If
  12. Next i
  13. Exit_BlankTXTControls:
  14.     Exit Sub 
  15. Err_BlankTXTControls:
  16.     MsgBox Err.Description
  17.     Resume Exit_BlankTXTControls
  18. End Sub
  19.  
You then use the following code in the on click event of the cmdclear button which as you can see passes the name of the subform (in this case the subform is named Submain) and makes the CALL to the sub function doing the work.

Expand|Select|Wrap|Line Numbers
  1.  Private Sub cmdClear_Click()
  2. On Error Resume Next 
  3. Call BlankTXTControls(Me!Submain.Form)
  4.  
  5. 'and here if you wanted to then advance to another control
  6. 'you would enter the setfocus command or the DoCmd GotoControl
  7. 'of your choosing
  8.  
  9. End Sub
  10.  
Regards

Jim
Jim,,,this is great...I'm going off to try this now. I'll report back for sake of others...I remember you (plate of spaghetti??)...I'm sure this will be good code. take care
Rosie
Sep 21 '07 #3
imrosie
222 100+
Jim,,,this is great...I'm going off to try this now. I'll report back for sake of others...I remember you (plate of spaghetti??)...I'm sure this will be good code. take care
Rosie
Hi Jim,,,

I put the code in as follows:
Expand|Select|Wrap|Line Numbers
  1. Sub BlankTXTControls(f As Form)
  2. On Error GoTo Err_BlankTXTControls
  3. Dim i As Integer, C As Control
  4. For i = 0 To f.Count - 1
  5.     Set C = f(i)
  6.     If Left$(C.Name, 3) = "txt" Then
  7.      C = Null
  8.     End If
  9. Next i
  10. Exit_BlankTXTControls:
  11.     Exit Sub
  12. Err_BlankTXTControls:
  13.     MsgBox Err.Description
  14.     Resume Exit_BlankTXTControls
  15. End Sub
  16.  
Expand|Select|Wrap|Line Numbers
  1. Private Sub clearsubform_Click()
  2. On Error Resume Next
  3. Call BlankTXTControls(Me![Order Details Subform].Form)
  4.  
The name of the Me, main form is "Add an Order and Details"...
There is one combo box too on the subform. So far nothing happens...it just sits there with the controls still populated.
I"m trying make sure I've got the formatting right for the naming of the Me![subform].Form

thanks
Sep 21 '07 #4
Jim Doherty
897 Expert 512MB
Hi Jim,,,

I put the code in as follows:
Expand|Select|Wrap|Line Numbers
  1. Sub BlankTXTControls(f As Form)
  2. On Error GoTo Err_BlankTXTControls
  3. Dim i As Integer, C As Control
  4. For i = 0 To f.Count - 1
  5. Set C = f(i)
  6. If Left$(C.Name, 3) = "txt" Then
  7. C = Null
  8. End If
  9. Next i
  10. Exit_BlankTXTControls:
  11. Exit Sub
  12. Err_BlankTXTControls:
  13. MsgBox Err.Description
  14. Resume Exit_BlankTXTControls
  15. End Sub
  16.  
Expand|Select|Wrap|Line Numbers
  1. Private Sub clearsubform_Click()
  2. On Error Resume Next
  3. Call BlankTXTControls(Me![Order Details Subform].Form)
  4.  
The name of the Me, main form is "Add an Order and Details"...
There is one combo box too on the subform. So far nothing happens...it just sits there with the controls still populated.
I"m trying make sure I've got the formatting right for the naming of the Me![subform].Form

thanks
Rosie,

Remember this code targets only those controls where the name of any control to be cleared is prefixed with the characters 'txt' it will not work otherwise

If you wish to clear the combo on the subform then provided the name of the combo begins with the characters txt...... it will clear that also (that is why I did not go for the TYPEOF identification element of controls because I assumed you might have have a miraid of different control type ie combos textboxes and so on)

The code does work I have tested in this end :)

Regards

Jim
Sep 21 '07 #5
imrosie
222 100+
Rosie,

Remember this code targets only those controls where the name of any control to be cleared is prefixed with the characters 'txt' it will not work otherwise

If you wish to clear the combo on the subform then provided the name of the combo begins with the characters txt...... it will clear that also (that is why I did not go for the TYPEOF identification element of controls because I assumed you might have have a miraid of different control type ie combos textboxes and so on)

The code does work I have tested in this end :)

Regards

Jim
Thanks Jim, I realize you tested it...so I assumed it must have worked for you.
Yes I was aware of the 'txt'...

The code doesn't clear the txt's so could it be that I've improperly formatted the string for the subform (which has spaces inthe name, Order Details Subform)?

In this part, which is the only area I really changed. thanks again.
Expand|Select|Wrap|Line Numbers
  1. Call BlankTXTControls(Me![Order Details Subform].Form)
Rosie
Sep 21 '07 #6
Jim Doherty
897 Expert 512MB
Thanks Jim, I realize you tested it...so I assumed it must have worked for you.
Yes I was aware of the 'txt'...

The code doesn't clear the txt's so could it be that I've improperly formatted the string for the subform (which has spaces inthe name, Order Details Subform)?

In this part, which is the only area I really changed. thanks again.
Expand|Select|Wrap|Line Numbers
  1. Call BlankTXTControls(Me![Order Details Subform].Form)
Rosie
Rosie

You have correctly wrapped the subform name in square brackets because you have spaces so that is not where your problem lies it must be something else.if you get stuck PM me with your email address and I'll send you this small db here and you can compare what I have with what you should have if you get me otherwise we could be ping ponging on here for something that is relatively straight forward

Jim
Sep 21 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Joseph J. Egan | last post by:
I am extending an existing Access/VBA app and need to update a subform displayed in continuous view within a containing form. The existing form and subform have worked fine to date with the...
0
by: Deano | last post by:
If I create 2 or more records in my subform and I then delete the main record, then all the controls on my subform disappear. Yet if there is only one record in the subform then it gets deleted...
4
by: Deano | last post by:
If I create 2 or more records in my subform and I then delete the main record, then all the controls on my subform disappear. Yet if there is only one record in the subform then it gets deleted...
3
by: cefrancke | last post by:
Is there a way to list (through VBA code) all the controls on an open form including subforms, tab controls and all other containers as well. My specific issue is to find all controls with a...
0
by: Lauren Quantrell | last post by:
In terms of resources, wondering if it makes a difference in adopting one of the three options below for resizing controls on a form and controls on a subform at the same time. option a.) Put...
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...
1
by: planetthoughtful | last post by:
Hi All, I have a mainform with a subform in which I show some task summary data. On the mainform I have a number of unbound controls that reflect values relevant to each task in the subform....
4
imrosie
by: imrosie | last post by:
Hello all, I'm a YAN (yet another newbie) in need of help. I've got an image database all setup and working fine now, thanks to the help you've given me. In the main form a user can search for an...
5
aas4mis
by: aas4mis | last post by:
I haven't had much luck with specific controls, their properties and loops in the past. I thought I would share this tidbit of code, feel free to modify/modularize in any way to suit your needs. This...
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
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
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,...

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.