MS Access Forum / Forms / May 2008
If statement
|
|
Thread rating:  |
Jason - 23 May 2008 09:05 GMT How do I make lcase("A") = ucase("A") return false. I can perform the oposite in DOS/CMD (if /i A==a returns true, if A==a returns fasle ) but how is it done in Access?
Thanks, J.
Jeanette Cunningham - 23 May 2008 11:18 GMT Jason, from access vba help. StrComp Function Example This example uses the StrComp function to return the results of a string comparison. If the third argument is 1, a textual comparison is performed; if the third argument is 0 or omitted, a binary comparison is performed.
Dim MyStr1, MyStr2, MyComp MyStr1 = "ABCD": MyStr2 = "abcd" ' Define variables. MyComp = StrComp(MyStr1, MyStr2, 1) ' Returns 0. MyComp = StrComp(MyStr1, MyStr2, 0) ' Returns -1. MyComp = StrComp(MyStr2, MyStr1) ' Returns 1.
Jeanette Cunningham -- Melbourne Victoria Australia
> How do I make lcase("A") = ucase("A") return false. I can perform the > oposite in DOS/CMD (if /i A==a returns true, if A==a returns fasle ) but [quoted text clipped - 3 lines] > Thanks, > J. Jason - 23 May 2008 21:55 GMT thanks, I've already created a module using:
Option Compare Binary Option Explicit
Function
and calling that function. It took a while to find - and didn't see strcomp.
> Jason, > from access vba help. [quoted text clipped - 18 lines] > > Thanks, > > J. Jason - 23 May 2008 22:02 GMT The result is still false even though both MyStr1 = "ABCD" MyStr2 = "ABCD"
> Jason, > from access vba help. [quoted text clipped - 18 lines] > > Thanks, > > J. Jeanette Cunningham - 23 May 2008 23:56 GMT Jason, did you change back to Option Compare Database Option Explicit ?
If it still doesn't work, post a copy and paste of your code.
Jeanette Cunningham -- Melbourne Victoria Australia
> The result is still false even though both > MyStr1 = "ABCD" [quoted text clipped - 23 lines] >> > Thanks, >> > J. Jason - 24 May 2008 00:58 GMT Still get the error:
If StrComp(OP & "", DLookup("P", "S", "C = """ & SD & """"), 1)
> Jason, > did you change back to [quoted text clipped - 32 lines] > >> > Thanks, > >> > J. Jeanette Cunningham - 24 May 2008 01:13 GMT Jason, in your code put some debug.print statements like this: Debug.Print "OP: " & OP Debug.Print "DLookup: " & DLookup("P", "S", "C = """ & SD & """") If StrComp(OP & "", DLookup("P", "S", "C = """ & SD & """"), 1) ... etc
Run the code by opening the form in normal view and clicking the buttons etc.
Now do Ctl + G to open the immediate window. You will see what values access got for OP and DLookup("P", "S", "C = """ & SD & """") This will be the clue to why it doesn't work.
Jeanette Cunningham -- Melbourne Victoria Australia
> Still get the error: > [quoted text clipped - 39 lines] >> >> > Thanks, >> >> > J. Jason - 24 May 2008 01:26 GMT I've done that using MsgBox - Debug window also still shows identical values.
> Jason, > in your code put some debug.print statements like this: [quoted text clipped - 55 lines] > >> >> > Thanks, > >> >> > J. Jeanette Cunningham - 24 May 2008 04:30 GMT Jason, it would help if you would copy and paste the values into a post.
Jeanette Cunningham -- Melbourne Victoria Australia
> I've done that using MsgBox - Debug window also still shows identical > values. [quoted text clipped - 62 lines] >> >> >> > Thanks, >> >> >> > J. Jason - 24 May 2008 04:39 GMT op: j3678 DLookup: j3678
> Jason, > it would help if you would copy and paste the values into a post. [quoted text clipped - 67 lines] > >> >> >> > Thanks, > >> >> >> > J. Jeanette Cunningham - 24 May 2008 05:14 GMT Jason, there seems to be some confusion. op = j3678 DLookup = j3678
These are both the same. When I test these using strComp I get 0 as the value. The 0 tells me that the value is the same as the value for DLookup. Which is what I expect. It seems to me that access is doing the right thing.
The second test used op = j3678 DLookup = J3678 This returned a value of 1 which tells us that j3678 is not the same as J3678.
In other words, the return value of 0 means that both strings are the same. The return value of 1 means that both strings are not the same. Does this help?
Jeanette Cunningham -- Melbourne Victoria Australia
> op: j3678 > DLookup: j3678 [quoted text clipped - 77 lines] >> >> >> >> > Thanks, >> >> >> >> > J. Jason - 24 May 2008 07:41 GMT So returns false (0) when equal and true (not 0) when not equal. Shall try different approach.
> Jason, > there seems to be some confusion. [quoted text clipped - 100 lines] > >> >> >> >> > Thanks, > >> >> >> >> > J. Jason - 24 May 2008 07:44 GMT I've tried adding = 0 before then. Now it is saying that when J3678 is entered it is equal and when j3678 is entered that is equal also.
> Jason, > there seems to be some confusion. [quoted text clipped - 100 lines] > >> >> >> >> > Thanks, > >> >> >> >> > J. Jason - 24 May 2008 07:56 GMT Don't know what's going on I have changed to: If StrComp(OP & "", DLookup("P", "S", "C = """ & SD & """"), 0) = 0 then This appears to work
> Jason, > there seems to be some confusion. [quoted text clipped - 100 lines] > >> >> >> >> > Thanks, > >> >> >> >> > J. Jeanette Cunningham - 24 May 2008 23:39 GMT Jason, it's morning here and I am back on the discussion group. I think it's easier to understand the code when it is written differently. Here's an example
Dim lngReturnValue as Long
lngReturnValue = StrComp(OP & "", DLookup("P", "S", "C = """ & SD & """"), 0) Debug.Print lngReturnValue
If Len(lngReturnValue) >0 Then If lngReturnValue = 0 Then 'code here with action to take ElseIf lngReturnValue = 1 Then 'code here with other action to take Else 'code here with other action to take End If End If
The 3 parts of the if statement cover the 3 possible return values.
Jeanette Cunningham -- Melbourne Victoria Australia
> Don't know what's going on > I have changed to: [quoted text clipped - 113 lines] >> >> >> >> >> > Thanks, >> >> >> >> >> > J. Jason - 25 May 2008 00:12 GMT The one line statement works fine. However, I notice that you have also changed the third number from 1 to 0. Why is Len(lngReturnValue) required? looks like you are trying to get the string length from an integer.
> Jason, > it's morning here and I am back on the discussion group. [quoted text clipped - 138 lines] > >> >> >> >> >> > Thanks, > >> >> >> >> >> > J. Jeanette Cunningham - 25 May 2008 07:47 GMT Jason,
Len(lngReturnValue) is a way of testing whether the result of strComp is Null. So in a sense there are 4 possible values from the strComp function. It makes sense to cover all 4 possibilities in your code in order to cover for any errors due to unforseen values for OP or the DLookup.
Jeanette Cunningham -- Melbourne Victoria Australia
> The one line statement works fine. However, I notice that you have also > changed the third number from 1 to 0. [quoted text clipped - 157 lines] >> >> >> >> >> >> > Thanks, >> >> >> >> >> >> > J.
|
|
|