473,405 Members | 2,154 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,405 software developers and data experts.

Problem with Macros

I have a macro setup to add up the hours each person spends on a project and display that on the form.

Action = SetValue
Item = [Forms]![Project P&E]![Total Man Hours]
Experssion = [Forms]![Project P&E]![Man Hours1]+[Forms]![Project P&E]![Man Hours2]+[Forms]![Project P&E]![Man Hours3]+[Forms]![Project P&E]![Man Hours4]+[Forms]![Project P&E]![Man Hours5]

Instead of it adding the values up it puts each in the block (i.e. if each person spent 1 hour the results displayed shows up 11111). When I try to subtract, multiply or divide it works fine. I'm not sure if it matters but I am using access 2003.

Thanks,
James
Jan 8 '09 #1
2 1170
Stewart Ross
2,545 Expert Mod 2GB
Hi. This behaviour arises when for whatever reason the values you are trying to add are actually text strings, not numbers, as the '+' operator acts differently when faced with text strings:

Numeric: 1+1 = 2
String: '1' + '1' = '11'

A dead give-away if you look at the form or table showing your data is that the values will be left-aligned, not right-aligned as numbers usually are.

Access is very good at converting types 'on the fly', and as there is no string equivalent of subtraction or multiplication it converts to numbers when you use the '-' and '*' operators.

You should really fix the problem underlying this - which may well be that the base table from which the numbers are derived has a text field definition instead of a numeric one. Otherwise you will need to convert the strings to numbers before you add them, using a conversion function such as Val, CLng (for whole numbers) or CDbl (for floating-point values).

Example:
Expand|Select|Wrap|Line Numbers
  1. Expression = Val([Forms]![Project P&E]![Man Hours1])+
  2. Val([Forms]![Project P&E]![Man Hours2])+
  3. Val([Forms]![Project P&E]![Man Hours3])+
  4. Val([Forms]![Project P&E]![Man Hours4])+
  5. Val([Forms]![Project P&E]![Man Hours5])
All of this can be done without using a macro at all, instead using an unbound text box directly on your form, setting its control source to be =Val(...) + Val(...) similar to the expression in your macro above. You could then dispense with the Forms![Form Name] qualifiers and simply refer to each control by name within the controlsource expression:

Expand|Select|Wrap|Line Numbers
  1. =Val([Man Hours1])+Val([Man Hours2])+
  2. Val([Man Hours3])+Val([Man Hours4])+Val([Man Hours5])

-Stewart
Jan 8 '09 #2
Thank you. I didn't realize that having the properties set to text instead of number would matter.
Jan 13 '09 #3

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

Similar topics

21
by: Chris Reedy | last post by:
For everyone - Apologies for the length of this message. If you don't want to look at the long example, you can skip to the end of the message. And for the Python gurus among you, if you can...
699
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
16
by: mike420 | last post by:
Tayss wrote: > > app = wxPySimpleApp() > frame = MainWindow(None, -1, "A window") > frame.Show(True) > app.MainLoop() > Why do you need a macro for that? Why don't you just write
24
by: Bangalore | last post by:
Hi all, I have a problem in accessing elements using overloaded operator . Consider, const int SIZE=10; int FALSE=0; class Array { private: int x; public:
7
by: hierro | last post by:
I have a list of functions (all with suffix T). For each one, I need to implement the following: FunctionT() { if (some_condition) { // do some conversion first FunctionW(); // then do some...
3
by: Stephen Sprunk | last post by:
On a project I'm working on, I ran across the following macros: /* assume s is struct stream *, s->p is char, v is unit16_t or uint32_t */ #define in_uint16_le(s,v) { v = *((s)->p++); v +=...
16
by: Shwetabh | last post by:
Hi, This is a question asked to me in an interview. I haven't been able to figure out an answer for it. Please try to see what can be done about the following problem. /* I have been given two...
47
by: Emil | last post by:
Is there any hope that new versions of PHP will support macros similar to C or C++? I've searched manual and didn't find anything except define directive, but it can be used to define constant...
11
by: San | last post by:
hi there, I am new to c++ and tryig to learn the basics of the c++ concepts. While I was reading the templates, I realize that the templates are a syntax that the compilar expands pased upon the...
33
by: Robert Seacord | last post by:
When writing C99 code is a reasonable recommendation to use inline functions instead of macros? What sort of things is it still reasonable to do using macros? For example, is it reasonable to...
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
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
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
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.