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

Access 2003 form-copy address tick box

Hi Everyone!

I'm relatively new to Access and completely new to VBA! I've been asked to create a tick box on a form that fills in an address from another field. This is what I've managed to put together thus far:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Same_as_clients_address_AfterClick()
  2.  
  3. Dim r As Long
  4. r = MsgBox("Did the incident occur at the client's address?", vbExclamation + vbYesNo + vbDefaultButton2, "Warning")
  5.  
  6. If r = vbYes Then
  7. Me![Clients_Street_Address] = Me![Where_incident_occurred_street_address]
  8. Me![txtClients_Suburb] = Me![Where_incident_occurred_suburb]
  9. Me![txtClients_State] = Me![Where_incident_occurred_state]
  10. Me![txtClients_Postcode] = Me![Where_incident_occurred_postcode]
  11. Else
  12. If r = vbNo Then
  13. Me![Where_incident_occurred_street_address] = Null
  14. End
  15. End If
  16. End If
  17. End If
  18.  
  19. If Me![Same_as_clients_address] Then
  20. Me![Clients_Street_Address] = Me![Where_incident_occurred_street_address]
  21. Me![txtClients_Suburb] = Me![Where_incident_occurred_suburb]
  22. Me![txtClients_State] = Me![Where_incident_occurred_state]
  23. Me![txtClients_Postcode] = Me![Where_incident_occurred_postcode]
  24. Else
  25. Me![Clients_Street_Address] = Null
  26. Me![txtClients_Suburb] = Null
  27. Me![txtClients_State] = Null
  28. Me![txtClients_Postcode] = Null
  29. End If
  30.  
  31. End Sub
When I fill out the form in Access and tick the box, nothing happens and no window pops up.

This has been driving me nuts for days and I appreciate any help!!!

Thanks so much :)
Aug 19 '11 #1
8 1986
MMcCarthy
14,534 Expert Mod 8TB
OK first of all there were a few errors in your code layout. You had some extra end if statements. I've tidied up the code for you so see if that makes a difference first.

Expand|Select|Wrap|Line Numbers
  1. Private Sub Same_as_clients_address_AfterClick()
  2.  
  3. Dim r As integer
  4.     r = MsgBox("Did the incident occur at the client's address?", vbExclamation +         vbYesNo + vbDefaultButton2, "Warning")
  5.  
  6.     If r = vbYes Then
  7.         Me![Clients_Street_Address] = Me![Where_incident_occurred_street_address]
  8.         Me![txtClients_Suburb] = Me![Where_incident_occurred_suburb]
  9.         Me![txtClients_State] = Me![Where_incident_occurred_state]
  10.         Me![txtClients_Postcode] = Me![Where_incident_occurred_postcode]
  11.     Else
  12.         Me![Where_incident_occurred_street_address] = Null
  13.     End If
  14.  
  15.     If Me![Same_as_clients_address]=-1 Then
  16.         Me![Clients_Street_Address] = Me![Where_incident_occurred_street_address]
  17.         Me![txtClients_Suburb] = Me![Where_incident_occurred_suburb]
  18.         Me![txtClients_State] = Me![Where_incident_occurred_state]
  19.         Me![txtClients_Postcode] = Me![Where_incident_occurred_postcode]
  20.     Else
  21.         Me![Clients_Street_Address] = Null
  22.         Me![txtClients_Suburb] = Null
  23.         Me![txtClients_State] = Null
  24.         Me![txtClients_Postcode] = Null
  25.     End If
  26.  
  27. End Sub
  28.  
Aug 19 '11 #2
NeoPa
32,556 Expert Mod 16PB
Also Simone, what's the name of the actual CheckBox control?

I ask that as the name of the event procedure is not right. Assuming the actual name is "Same_as_clients_address" then the procedure definition should be :
Expand|Select|Wrap|Line Numbers
  1. Private Sub Same_as_clients_address_AfterUpdate()
Other pointers that may help with code like this are :
  1. When using multiple references to properties of the same object (in this case Me) it is good practice to use the With ... command.
  2. When using booleans values (which is what the value of a CheckBox is) it makes no sense to compare them to another boolean value. In fact, in many cases people get into difficulties because VBA doesn't treat only TRUE values as true. The proper way to treat booleans within an If or IIf() is simply to reference them.

    The reason for this is that If takes a boolean argument. If {boolean} Then. Thus, it makes sense to use :
    If X = Y Then
    as (X = Y) returns a boolean value. The following lines make equally little sense though :
    Expand|Select|Wrap|Line Numbers
    1. If Me.CheckBox = True Then
    2. If (X = Y) = True Then
    The extra complication within VBA is that any numeric value not equal to zero is treated as TRUE so if you save the result of a calculation in a numeric variable (X) then test it, the following two lines give opposite results (when naturally they would be expected to ehave the same) :
    Expand|Select|Wrap|Line Numbers
    1. X = 52 - 36
    2. If X Then {True result}
    3. If X = True Then {False result}

EG.
Expand|Select|Wrap|Line Numbers
  1. Private Sub Same_as_clients_address_AfterUpdate()
  2.     With Me
  3.         If .Same_as_clients_address Then
  4.             .Clients_Street_Address = .Where_incident_occurred_street_address
  5.             .txtClients_Suburb = .Where_incident_occurred_suburb
  6.             .txtClients_State = .Where_incident_occurred_state
  7.             .txtClients_Postcode = .Where_incident_occurred_postcode
  8.         Else
  9.             .Clients_Street_Address = Null
  10.             .txtClients_Suburb = Null
  11.             .txtClients_State = Null
  12.             .txtClients_Postcode = Null
  13.         End If
  14.     End With
  15. End Sub
PS. This code indicates what I would guess is the logic you're after assuming [Same_as_clients_address] is the CheckBox control on your form and once it's been set there is no need to re-ask the same question.
Aug 19 '11 #3
Mihail
759 512MB
Hi all !

Simone,
1) As NeoPa teach you, is a good practice to use With - End With when you refer the same object multiple times.
But, in your case, is not strongly necessary to refer Me.
This keyword (Me) refer your current form and Access work in "Me" as default. So, NeoPa's code will work even if you remove With Me and End With lines from the code.

2) You can use MsgBox function in IF syntax:
Expand|Select|Wrap|Line Numbers
  1. If MsgBox(.....) = vbYes then
  2.      'Code if TRUE
  3. Else
  4.      'Code if FALSE
  5. End If
Yes, the code is more elegant as you design it but... what is variable "r" ? Of course, now you know: "r" store the MsgBox result, but after 3 month (or 3 years) you need to (re)understand your own code to see what mean "r As Long". For this purpose (maintenance) my practice is to create a module (I name it all the time "modPublic". In this module I define global variables, subroutines, functions and so on. One of this variable is ( invariable :) )
Expand|Select|Wrap|Line Numbers
  1. Public Msg As VbMsgBoxResult
I use this variable instead your "r":
Expand|Select|Wrap|Line Numbers
  1.     Msg = MsgBox("Did the incident occur at the client's address?", vbExclamation + vbYesNo + vbDefaultButton2, "Warning")
  2.     If Msg = vbYes then
  3.          'Code if TRUE
  4.     Else
  5.          'Code if FALSE
  6.     End If
or, using Select Case,
Expand|Select|Wrap|Line Numbers
  1.     Select Case Msg
  2.         Case vbYes
  3.             'Code if Msg = vbYes
  4.         Case vbNo
  5.             'Code if Msg = vbNo
  6.         Case vbCancel
  7.             'Code if Msg = vbCancel
  8.         Case vbRetry
  9.             'Code if Msg = vbRetry
  10.     End Select
That is about MsgBox function. As NeoPa say, I think is not necessary for now in your code.

3) Is a question for NeoPa as expert
Expand|Select|Wrap|Line Numbers
  1. Me![Clients_Street_Address] = Me![Where_incident_occurred_street_address]
This is a line from Simone code.
I don't understand what type of object are Clients_Street_Address and Where_incident_occurred_street_address. Reading next lines from Simone's code and your posts, NeoPa, I understand that must be objects in the form (text boxes). In this case I can't see any reason to populate a text box in a form with the value of other text box from the same form.
More: Are this text boxes bound to a table ? If YES, the first line from ELSE clause
Expand|Select|Wrap|Line Numbers
  1. .Clients_Street_Address = Null
damage the data base, isn't it ? The field [Clients_Street_Address] will loose the data.

I think that if the check box Same_as_clients_address will be bound to a new field [Same_as_clients_address] in the table can solve the entire problem.

I ask you please, NeoPa, to check my reasoning because I am in a dark fog.
I think that an answer for me is also an answer for Simone (so I have not, again :), some problems with the forum rules).

Thank you !
Aug 20 '11 #4
NeoPa
32,556 Expert Mod 16PB
Mihail:
This keyword (Me) refer your current form and Access work in "Me" as default. So, NeoPa's code will work even if you remove With Me and End With lines from the code.
Technically that's true, yet such advice is not helpful as there is value in using Me in the code to allow easier maintenance and clearer, more readable, code. It's not an accident that so many experienced Access developers use it. Nor is it because they just never bothered to find another way. Experience teaches us to use the better ways when we find and understand them.

Mihail:
3) Is a question for NeoPa as expert
The reason is that Simone does exactly want to clear the values from these controls when the CheckBox is cleared. Remember, the values have been set automatically in the code when the CheckBox was set, so the existing values are known logically. With the Checkbox now cleared it makes sense that the client values no longer match the address data for the incident.

Technically, it would be a good idea that when these controls are set to match the controls for the incident, they are also set so they cannot be changed by the operator. When they are cleared, they should then (and only then) be re-enabled for operator entry.

The reasoning behind the question though, makes good sense, even if a little more consideration needs to be applied to the code.

Does that make things somewhat clearer?
Aug 20 '11 #5
Mihail
759 512MB
Yes, NeoPa and thank you.
I think you understand English as I understand... Romanian :) . In this context (the data must be cleared) what you say has sens.
But a bit of fog remain:

In your code:
Expand|Select|Wrap|Line Numbers
  1. If .Same_as_clients_address Then
  2.      .Clients_Street_Address = .Where_incident_occurred_street_address
why to fill .Clients_Street_Address (it is a text box, isn't it ?) with data from .Where_incident_occurred_street_address (it is also a text box from the same form; or I miss understood?) ?
Aug 20 '11 #6
NeoPa
32,556 Expert Mod 16PB
The question revolves around a form which has two sets of controls for addresses. One is for the client's address and the other is for the address of where the incident occurred. On this form it is not necessary to enter the address in for the client and the incident if the CheckBox to say they are the same is checked. When a record (on the form) indicates the addresses are the same the form enters the address in automatically by copying the address held for the incident into the client's address controls.

Imagine a form that reports crimes of personal attacks. If someone were to be attacked in a shopping centre then separate addresses would be required for the client's home and where the attack took place. For those incidents where the attack takes place at the client's address, it is unnecessary (and prone to potential error) to allow the address to be entered twice by the operator, so instead the form does it for them automatically when the CheckBox is set.

Does that make it all clearer?
Aug 20 '11 #7
Mihail
759 512MB
Yes. Now it is clear. Thank you very much, NeoPa.
Also I wish to thank Simone for the post because I learn a lot.

Good luck, Simone !
Aug 21 '11 #8
Hey everyone!

Thank you so much for your help, I never expected such helpful responses! I will try those suggestions now and let you know how I go :)
Thanks again
Aug 22 '11 #9

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

Similar topics

13
by: Manuel Lopez | last post by:
I have a puzzling form timer problem that I didn't experience prior to Access 2003 (though I'm not sure access 2003 is to blame). Here's the situation: a computer has two access 2003 databases on...
5
by: Tom | last post by:
Am about to upgrade several machines running access 97 to new boxes with access 2003. Got a massive access front end DB with a SQL back end. Guess I will have to "upgrade" that '97 front. ...
2
by: Mark | last post by:
I am trying to export an access 2003 query to XML and need to get it to export in a very specific format. I am stuck and could use some help badly (time is critical with this project). This is...
3
by: Dave | last post by:
Hi, I wrote a VBA program to read and process data from an ODBC source and put it into an Access database. I want to make the program into an MDE file and run it on systems with Access 2003. ...
7
by: Marcin | last post by:
Hello all! A few years ago I created a form with button which let me send an email with an attachment. It was created in Access 97. Now I would like to move this application into Access 2003....
3
by: RayPower | last post by:
I'm having a system using Access 2000 as both front-end (queries, forms, reports & temp tables for reports) & back-end (data) with back-end running on the server. The application runs on the...
10
by: Mike Charney | last post by:
Is there a simple way to send SMTP email from Access VBA? Mike m charney at dunlap hospital dot org
17
by: Mell via AccessMonster.com | last post by:
Is there a way to find out where an application was created from? i.e. - work or home i.e. - if application sits on a (work) server/network, the IT people know the application is sitting...
0
by: jayohare | last post by:
Hello, I have code within my DB application to process credit cards through authorize.net. Ive been using the same code for several years without a problem. I have an order entry computer and...
2
by: boyleyc | last post by:
Hi all the following code works perfectly well. Basically it populates a series of check boxes on my form, depending on whether dlookup finds an associated record. The problem i have is that...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
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:
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,...
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...

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.