473,508 Members | 2,344 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Code Help to make a Check Box Functional

loonette
17 New Member
I have created a continuous data entry form to enter information in. I use the following code to make certain information entered in the previous record repeat in the next record.

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Current()
  2. Dim rs As Object
  3.     Set rs = Me.Recordset.Clone
  4.  
  5.     If rs.EOF Or Not Me.NewRecord Then
  6.         ' don't do anything if there's no records or it is not a new record
  7.     Else
  8.         With rs
  9.  
  10.             .MoveLast
  11.  
  12.             Me![LotNumber] = .Fields("LotNumber")
  13.             Me![Description] = .Fields("Description")
  14.             Me![SalePrice] = .Fields("SalePrice")
  15.         End With
  16.  
  17.     End If
  18.  
  19. End Sub
(Sorry, I haven't posted here in a while and I can't remember how to put the code in [code] tags)

I would like to add a checkbox to the form that when checked or unchecked would turn this code on or off. I have read through the forums and I can't find anything that is relevant to my situation. I have also played around with it myself, but I just don't know enough programming to figure it out myself. Any help will be greatly appreciated.

Thanks!
Lori
Aug 6 '11 #1
4 1848
ADezii
8,834 Recognized Expert Expert
There are other approaches as far as Carrying Over Values, but we'll stay with this one since you already have put work into it.
  1. Declare a Private, Boolean Variable in the Form's Code Module. The Value of this Variable (True/False) will indicate whether or not the Code in the Form's Current() Event will be executed, and is Set in the AfterUpdate() Event of a Check Box (chkCarry).
    Expand|Select|Wrap|Line Numbers
    1. Private blnCarryValue As Boolean
  2. Write Code in the AfterUpdate() of chkCarry that will Set the Value of the variable blnCarryValue.
    Expand|Select|Wrap|Line Numbers
    1. Private Sub chkCarry_AfterUpdate()
    2.   blnCarryValue = chkCarry
    3. End Sub
  3. Modify Code in the Form's Current() Event, and make it check the Value of blnCarryValue to see as to whether or not the Code should be executed.
    Expand|Select|Wrap|Line Numbers
    1. Private Sub Form_Current()
    2. Dim rs As DAO.Recordset
    3.  
    4. Set rs = Me.RecordsetClone
    5.  
    6. If blnCarryValue Then
    7.   If rs.EOF Or Not Me.NewRecord Then
    8.     ' don't do anything if there's no records or it is not a new record
    9.   Else
    10.     With rs
    11.       .MoveLast
    12.         Me![LotNumber] = .Fields("LotNumber")
    13.         Me![Description] = .Fields("Description")
    14.         Me![SalePrice] = .Fields("SalePrice")
    15.     End With
    16.   End If
    17. End If
    18.  
    19. rs.Close
    20. Set rs = Nothing
    21. End Sub
Aug 6 '11 #2
NeoPa
32,557 Recognized Expert Moderator MVP
This all sounds horribly more complicated than necessary Lori.

I would suggest the following idea :

Shove your code in the AfterUpdate event procedure of the form and simply set the .DefaultValue properties of the controls to the existing values. It's important to set only the .DefaultValue property, rather than the .Value as your code does, as otherwise you are dirtying the record immediately, even before the operator has had a chance to choose to do so. This is a point I find myself having to make frequently for some reason. Nevertheless it is quite a basic one.

I would think code similar to the following should do :

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_AfterUpdate()
  2.     With Me
  3.         If .NewRecord Then
  4.             .LotNumber.DefaultValue = .LotNumber
  5.             .Description.DefaultValue = .Description
  6.             .SalePrice.DefaultValue = .SalePrice
  7.         End If
  8.     End With
  9. End Sub
You can include code in the Open() event for the form too, if you deem it worth it (This depends on normal usage which we cannot tell).

If, when you run with this you still feel it necessary to add a CheckBox then let us know. It's not complicated, but may well prove redundant with the logic in the new place.
Aug 6 '11 #3
ADezii
8,834 Recognized Expert Expert
@NeoPa:
I would like to add a checkbox to the form that when checked or unchecked would turn this code on or off.
Isn't the idea to use a Flag (via a Check Box) that will indicate whether or not the Value even gets carried over for the three Controls? Should the Flag evaluate to False, wouldn't the Default Values have to be Reset to Null so as to not carry them over to a New Record?
Aug 6 '11 #4
NeoPa
32,557 Recognized Expert Moderator MVP
My guess was that the requirement, though clearly stated as you say, is simply a result of getting the main logic so wrong in the first place. As my last sentence indicates, this can be added in later if, after seeing how it should work, Lori still feels it is necessary. Sometimes (always really) it makes sense to strip away the complications first and get the basics right. Then one is in a better position to know what extras are required. Building on sand is not a good recipe for solid-standing structures.
Aug 7 '11 #5

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

Similar topics

3
1641
by: D. Shane Fowlkes | last post by:
First of all, can anyone recommended a good URL for ASP.NET related issues. I reallt like www.aspfaq.com but that's mainly for traditional ASP. I'm starting to explore .NET..which leads me to my...
3
1292
by: Rob Meade | last post by:
Hi all, I have some code which I cobbled together from some examples online, so its probably wrong, although I did have it working a little while ago, unfortunately for some reason it seems to...
4
1329
by: Rich | last post by:
Hi I have five check boxes in a windows form. They are called chkEQUDS01, chkEQUDS02, chkEQUDS03, chkEQUDS04, and chkEQUDS05. I am getting the text name for the check boxes from a datatable...
2
1288
by: Rich | last post by:
Hi I have two radio buttons in a groupbox on a Windows form and have wrote the code below to find out which button is selected. Can anyone tell me if there is an easier / better way to code...
4
1026
by: guy | last post by:
I am binding a datatable to a WinForms grid control but for some reason the GridTableStyle is being ignored, the data just displays as if no TableStyle was set up I have also tried changing the...
0
1341
by: nukiee | last post by:
I wanted to check the status code that comes back as response from the HttpWebResponse. So I used the following code - res = (HttpWebResponse) req.GetResponse(); It's working great, but if I...
3
2577
by: pankajit09 | last post by:
Please tell me whether the below JS code will run in all the browsers --> function getHTML(num) { var saf = 0 ; var num; if(!num) { num =...
1
1175
by: Neal Becker | last post by:
After just getting bitten by this error, I wonder if any pylint, pychecker variant can detect this error?
2
1110
by: Wayneyh | last post by:
Hi all I am having trouble getting some code working. Would someone please check the following code and tell me why it doesn't work. If additional info is required please ask. Thanks. ...
0
7135
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
7342
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,...
1
7067
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
7505
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...
1
5060
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4729
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3215
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...
0
3201
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1570
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 ...

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.