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

How can I get this excel formula to work in access?

How can I get this excel formula to work in access I know that the field name replaces the cell reference?

Example: Clock In Clock Out = Total Time Worked

Expand|Select|Wrap|Line Numbers
  1. =IF((OR(H9="",G9="")),0,IF((H9<G9),((H9-G9)*24)+24,(H9-G9)*24))
Dec 16 '10 #1

✓ answered by NeoPa

That's not right I'm afraid Delerna.

Access and Excel VBA are the same in this respect. Both have If constructs as well as the IIf() function available to them. Where they differ is as you suggested in your first reply - Excel formulas use IF(), whereas Access formulas use IIf() (just as in VBA). You can't have a line in Access (or any) VBA which just says 0.

I'm afraid the question is very basic in its information, leaving us very little to work from, but in general an Access IIf() function call can include more complex boolean expressions which must be handled using OR() or AND() function references in an Excel formula. This formula could then be written in Access (a formula reference rather than VBA) as :
Expand|Select|Wrap|Line Numbers
  1. =IIf([H9]='' Or [G9]='',0,IIf([H9]<[G9],([H9]-[G9]*24)+24,([H9]-[G9]*24))
Or a little less clumsily as :
Expand|Select|Wrap|Line Numbers
  1. =IIf([H9]='' Or [G9]='',0,24*([H9]-[G9]+IIf([H9]<[G9],1,0)))
Presumably [H9] & [G9] would have names, but we don't, so we'll just make do with the Excel references for now.

13 8017
Delerna
1,134 Expert 1GB
If you mean in a query then
excels
if(condition,true,false)
in access is
iif(condition,true,false)


so basically take you formula
change if to iif
change the cell references to field references
change the or part to (H9="" or G9="")

and your done

something like this (untested)
Expand|Select|Wrap|Line Numbers
  1. iif(H9="" or G9="",0,iif(H9<G9,(H9-G9)*24 +24,(H9-G9)*24)) 
  2.  
  3.  
you will need to change the cel refs to field refs of course
Dec 17 '10 #2
Delerna
1,134 Expert 1GB
If in vba
excels
if(condition,true,false)
in access is

if condition then
true statements
else
false statements
end if

so
Expand|Select|Wrap|Line Numbers
  1. if H9="" or G9="" then
  2.    0
  3. else
  4.    iif H9<G9 then
  5.       (H9-G9)*24 +24
  6.    else
  7.       (H9-G9)*24)
  8.    end if
  9. end if 
  10.  
Dec 17 '10 #3
NeoPa
32,556 Expert Mod 16PB
That's not right I'm afraid Delerna.

Access and Excel VBA are the same in this respect. Both have If constructs as well as the IIf() function available to them. Where they differ is as you suggested in your first reply - Excel formulas use IF(), whereas Access formulas use IIf() (just as in VBA). You can't have a line in Access (or any) VBA which just says 0.

I'm afraid the question is very basic in its information, leaving us very little to work from, but in general an Access IIf() function call can include more complex boolean expressions which must be handled using OR() or AND() function references in an Excel formula. This formula could then be written in Access (a formula reference rather than VBA) as :
Expand|Select|Wrap|Line Numbers
  1. =IIf([H9]='' Or [G9]='',0,IIf([H9]<[G9],([H9]-[G9]*24)+24,([H9]-[G9]*24))
Or a little less clumsily as :
Expand|Select|Wrap|Line Numbers
  1. =IIf([H9]='' Or [G9]='',0,24*([H9]-[G9]+IIf([H9]<[G9],1,0)))
Presumably [H9] & [G9] would have names, but we don't, so we'll just make do with the Excel references for now.
Dec 18 '10 #4
Delerna
1,134 Expert 1GB
That's not right I'm afraid Delerna.
Hi Neopa
I must admit I had always made the assumption that what I said was correct and after reading your post I had doubts so I fired up Access and Excel and Checked

I am sorry but I fail to see where what I said is not correct.

In my trial
IN EXCEL
iif(condition,true,false)
cannot be used in a cells formula.
Neither can it be used in VBA.
If condition then true else fales end
Also cannot be used in a cells formula.
It can be used within VBA of course but since the OP's question said excel formula I gave answers in reference to that.

In ACCESS
if(codition,true,false)
within a query generates an undefined statement error on if.
Again it cannot be used in VBA here since VBA in access or excel or word is just VBA.
It matters not, which application it is written within.


Actually, I don't see where I made any comment as to which constructs exist or don't exist within excel,access or VBA.

Within a query
Mine
iif(H9="" or G9="",0,iif(H9<G9,(H9-G9)*24 +24,(H9-G9)*24))

Yours
IIf([H9]='' Or [G9]='',0,IIf([H9]<[G9],([H9]-[G9]*24)+24,([H9]-[G9]*24))

Appart from the [] are identical


My syntax for VBA is incorrect because
I copied and pasted the code and forgot to change the iif in the nested if, which won't work in VBA.

Is that where you mean I was not correct?
Dec 20 '10 #5
Delerna
1,134 Expert 1GB
Oh I just noticed

Access and Excel VBA are the same in this respect
But as poted above
I made no comment about excel vba.
My answers were relating to the OP's excel formula and gave versions for both cases in ACCESS alone.
within a query and within VBA
Dec 20 '10 #6
NeoPa
32,556 Expert Mod 16PB
Delerna:
In my trial
IN EXCEL
iif(condition,true,false)

cannot be used in a cells formula.
Neither can it be used in VBA.
The last line of the quoted text is not correct Delerna. Excel VBA handles the IIf() function quite happily.

In your post #3 you indicate that an Excel IF formula is equivalent to an If ... Then ... Else construct in Access VBA. There are many differences between these two I'm afraid, principal of which is that the VBA code is not usable within a query and no VBA line can return a simple value, as you have in line #2 of your code. That simply wouldn't compile or run.
Delerna:
Actually, I don't see where I made any comment as to which constructs exist or don't exist within excel,access or VBA.
No. I included my explanation of that in the hope that a single explanation would put the whole thing in perspective for anyone reading the question. A full and definitive answer, if you like. This was not about where your post was in error at all. It was part of my answer.

PS. I didn't see post #6 before I prepared this, but I have now of course. I think everything is still pertinent, but some may now be superfluous.
Dec 21 '10 #7
NeoPa
32,556 Expert Mod 16PB
I forgot to include in my last post (#7) that your code was very similar to mine indeed. I wasn't implying that was wrong. I wanted to post a version that was as equivalent as possible to start with, before posting a tidier version. The only differences were the brackets, as you mentioned, but also the quotes used. These are harder to notice unless you use the [code] tags, but I used the single quotes (') which are SQL standard rather than the doubles (") which Access uses generally, but which are not standard (See Quotes (') and Double-Quotes (") - Where and When to use them).

The later version, which was somewhat different from the OP's code, was what I was really suggesting to use, as the logic is more straightforward, albeit somewhat less obvious than the OP's version.
Dec 21 '10 #8
Delerna
1,134 Expert 1GB
oops.......egg on face.
Dec 21 '10 #9
Delerna
1,134 Expert 1GB
corrected code
Expand|Select|Wrap|Line Numbers
  1. iif(H9='' or G9='',0,iif(H9<G9,(H9-G9)*24 +24,(H9-G9)*24))  
  2.  

Expand|Select|Wrap|Line Numbers
  1. if H9="" or G9="" then 
  2.    result=0  
  3. else 
  4.    iif H9<G9 then 
  5.       result=(H9-G9)*24 +24 
  6.    else 
  7.       result=(H9-G9)*24) 
  8.    end if 
  9. end if  
  10.  


and

iif does indeed exist in vba working similar to a fuction call, which I was not aware of.
A little research reveals that care in it's use needs to be taken because it actually doesn't work exactly like if then else.

For example in an attempt to cover division by zero errors

Expand|Select|Wrap|Line Numbers
  1. dim t
  2. t=0
  3. result=iif(t=0,0,10/t)
  4.  
would still produce the error.
This is because both the true and the false statements are executed, returning the result based on the condition

Anyway, a new tool for my toolbox that I was not aware of before.
Dec 22 '10 #10
NeoPa
32,556 Expert Mod 16PB
Delerna:
This is because both the true and the false statements are executed, returning the result based on the condition
This is something that many people get confused by. Actually, it's not so much about the function executing both of the statements, but in a very real way it's even simpler than that.

When the compiler creates the code, it is done in the standard way for passing values as parameters to a procedure. That is, the compiler ensures that the calculated values are determined and passed to the function, before the function is even instigated. In other words, all of the parameters, the boolean value as well as the True and False values, are worked out beforehand and the function itself merely returns one of the values it has been passed. The calculation that fails (division by zero in your example) will be done prior to the execution of the function.

I hope that helps. So many people get hung up on that and stay confused for ever.
Dec 23 '10 #11
Delerna
1,134 Expert 1GB
Well if we are going to that level of detail :)

I thought VBA was an interpreted language and not a compiled one.
In that case each instruction, immediately prior to execution is interpreted and both true and false conditions are determined immediately prior to the codes execution.
In effect, VBA's process of executing an instruction causes both versions of the truth to be calculated and therefore the error to be generated.
Dec 23 '10 #12
Delerna
1,134 Expert 1GB
Actually going down to this level could be best left to the people we are trying to help to discover for themselves, unless they ask for it directly.
It could be even more confusing for someone who doesn't have enough knowledge to understand it?
Dec 23 '10 #13
NeoPa
32,556 Expert Mod 16PB
In response to post #12 :
VBA is semi-compiled. There are elements of compilation, but also of interpretation. Otherwise you're absolutely right in your understanding. That explains it well.

In response to post #13 :
We all have our opinions. In this case I don't believe this makes it harder to understand. We're not forcing anyone to read any comments. If they don't like what they read they can simply ignore it and go onto the next post, but if it's helpful and what they need, they can't read it if it's not posted. That said, if your opinion is otherwise, I can live with that too.
Dec 24 '10 #14

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

Similar topics

6
by: alphaomega3 | last post by:
I have a formula used in Excel that I am trying to convert for use in a query. the original formula is: ...
3
by: Widge | last post by:
I have made a spreadsheet that will calculate the distance from a set of addresses to a specified postcode in Excel. I'm looking to move this to Access so I can start thinking about hooking it up to...
3
by: skiddle | last post by:
I've got an Excel formula that calculates the total business hours that a help desk ticket is open. As one would expect, it factors out holidays and non-work hours. What would be the corresponding...
1
by: Freud52 | last post by:
y = 1E-07x6 - 0.0001x5 + 0.0147x4 - 0.635x3 + 12.906x2 - 115.76x + 401.3 this is a formula for a percentile score. the "x" represents th raw score and the number after it is supposed to be...
2
by: welshkaiboy | last post by:
Due to the size of the data I have to manipulate I need to apply a excel formula in access which determines date of manufacture from a serial number 716001 so I use...
2
by: rtilson | last post by:
I am not sure why excel and access use the same method in formula. I am not access expert but been trying to figure it out. Here is the excel formula that I would like to use in access expression...
0
by: Lina Arraiz | last post by:
I'm trying to export an Excel 2003 spreadsheet to Access 2007. I need to calculate the time elapsed between column D (START) and column E (END) with the results appearing in column F (TIME). The...
1
by: Hal Kaelin | last post by:
Compile Error Ambiguous name detected: OpenReferenceDocument See attached file Private Sub lstRecapFileList_DblClick(Cancel As Integer) Call OpenReferenceDocument(RecapFolder &...
21
by: Sabrina Smith | last post by:
I am not a programmer - so I don't know code, however, I do need some help if anyone can oblige me... I have a project that requires me to add 4 workdays (excluding holidays) to a beginning date...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
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
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
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...

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.