MS Access Forum / Forms Programming / June 2007
How do I test the value of a specific character within a string?
|
|
Thread rating:  |
David Anderson - 22 Jun 2007 17:00 GMT I want to write some VBA code for an Access 2000 database app that checks the value of the 5th character in a 6 character string. I used to know how to do this, but my brain has turned to mush and I've failed to find anything useful in the online help.
No doubt the answer is extremely simple, but how do I do this?
David
Steve - 22 Jun 2007 17:25 GMT If Left("MyString",5) = "ValueTestingFor" Then MsgBox "the 5th character in " & "MyString" & " is what I am looking for" Else MsgBox "OOps, wrong value!" End If
PC Datasheet Providing Customers A Resource For Help With Access, Excel And Word Applications resource@pcdatasheet.com
>I want to write some VBA code for an Access 2000 database app that checks >the [quoted text clipped - 7 lines] > > David David Anderson - 22 Jun 2007 18:24 GMT Thanks for the answers, guys. I was pretty sure that 'Left' or 'Right' were the operators I was looking for (Mid$ was a less familiar solution), so why couldn't I find anything about them in the Access online help?
David
> If Left("MyString",5) = "ValueTestingFor" Then > MsgBox "the 5th character in " & "MyString" & " is what I am looking [quoted text clipped - 19 lines] > > > > David fredg - 22 Jun 2007 18:46 GMT > Thanks for the answers, guys. I was pretty sure that 'Left' or 'Right' were > the operators I was looking for (Mid$ was a less familiar solution), so why [quoted text clipped - 25 lines] >>> >>> David It's not intuitive! Open any code window (Ctrl + G) and click on help. Now search for "function". You'll find right, left, mid, etc. in the list.
 Signature Fred Please respond only to this newsgroup. I do not reply to personal e-mail
David Anderson - 22 Jun 2007 22:57 GMT Fred, Thanks for the suggestion, but i've just tried it (with Access 2000) and can't see any trace of Right, Left, etc, in the search results for 'Function'. Please clarify.
David
> It's not intuitive! > Open any code window (Ctrl + G) and click on help. Now search for > "function". > You'll find right, left, mid, etc. in the list. StopThisAdvertising - 22 Jun 2007 19:49 GMT > If Left("MyString",5) = "ValueTestingFor" Then > MsgBox "the 5th character in " & "MyString" & " is what I am looking [quoted text clipped - 7 lines] > Applications > resource@pcdatasheet.com
 Signature You are *not* a resource at all !! Stop advertising here, or get lost for another year or so... http://home.tiscali.nl/arracom/whoissteve.html
ArnoR
Bob Quintal - 22 Jun 2007 22:33 GMT > If Left("MyString",5) = "ValueTestingFor" Then > MsgBox "the 5th character in " & "MyString" & " is what I [quoted text clipped - 8 lines] > Word Applications > resource@pcdatasheet.com Lovely code, but it fails to do what the Original Poster asked.
This is what he wanted. If mid("MyString",5) = Whatever then
is the correct test.
>>I want to write some VBA code for an Access 2000 database app >>that checks the [quoted text clipped - 8 lines] >> >> David
 Signature Bob Quintal
PA is y I've altered my email address.
-- Posted via a free Usenet account from http://www.teranews.com
David Anderson - 22 Jun 2007 23:43 GMT Bob, Remember that I was wanting to do my check on just the 5th character. Dale's solution works correctly and so does yours - but only if (like Dale) you add the final parameter of 1. Because of my inability to find anything in the online help, I'm not sure of the difference between Mid and Mid$, but both seem to do the same job in this particular case.
David
> > If Left("MyString",5) = "ValueTestingFor" Then > > MsgBox "the 5th character in " & "MyString" & " is what I [quoted text clipped - 28 lines] > >> > >> David John Marshall, MVP - 22 Jun 2007 23:52 GMT The functions are similar and the difference is in what is returned. MID, LEFT, RIGHT return variants while MID$, LEFT$ and RIGHT$ return strings.
Some of the other $ functions are CHR$, DATE$, TRIM$, UCASE$ STR$, DIR$ and about twenty other functions.
John... Visio MVP
> Bob, > Remember that I was wanting to do my check on just the 5th character. [quoted text clipped - 39 lines] >> >> >> >> David David Anderson - 23 Jun 2007 00:10 GMT John, Thanks for the info. Where exactly is all this covered in the online help?
David
> The functions are similar and the difference is in what is returned. MID, > LEFT, RIGHT return variants while MID$, LEFT$ and RIGHT$ return strings. [quoted text clipped - 47 lines] > >> >> > >> >> David John Marshall, MVP - 23 Jun 2007 00:27 GMT For the VBA stuff, I use the help with VBA, but I must admit it is not as easy in Office 2007 as in older versions.
John... Visio MVP
> John, > Thanks for the info. Where exactly is all this covered in the online help? [quoted text clipped - 56 lines] >> >> >> >> >> >> David David Anderson - 23 Jun 2007 00:49 GMT John, I haven't tried looking in the Access 2007 online help (I've only just got Office 2007 Pro and haven't started to use A2007 yet). I'm still using A2000 and have failed to find any of this stuff in the help, whether called from Access itself or from the VBA coding window.
David
> For the VBA stuff, I use the help with VBA, but I must admit it is not as > easy in Office 2007 as in older versions. [quoted text clipped - 61 lines] > >> >> >> > >> >> >> David Marshall Barton - 23 Jun 2007 02:18 GMT >I haven't tried looking in the Access 2007 online help (I've only just got >Office 2007 Pro and haven't started to use A2007 yet). I'm still using A2000 >and have failed to find any of this stuff in the help, whether called from >Access itself or from the VBA coding window. I never used A2K, but I suspect that, like every other version of Access, you can go to the table of contents, scan down to the VB Language Reference and find the Functions chapter.
 Signature Marsh MVP [MS Access]
David Anderson - 23 Jun 2007 16:56 GMT Marshall, You are right. The functions are indeed all listed in the table of contents. I never tried that, because I assumed that an index search would always include such basic items as the main chapter headings. Silly me, to expect the online help to act in a logical manner! On top of such built-in quirks I also found that my help system was corrupted in some way. Access itself recognised there was a problem and offered to fix it, with the aid of the original installation CD. Before this repair, no search would find either the Right, Left or Mid Functions. Since this repair, I can now find both Right and Mid via an Index search - but not Left. However, an Answer Wizard search does now find all these Functions, including Left.
All these irritating complications make me nostalgic for the days of a simple printed manual....
David
> I never used A2K, but I suspect that, like every other > version of Access, you can go to the table of contents, scan > down to the VB Language Reference and find the Functions > chapter. John Marshall, MVP - 22 Jun 2007 23:43 GMT Actually, you are both wrong.
The OP wanted the fifth character of a six character string.
As usual, stevie's answer is way off base and will get the first five characters of the string.
Bob, you are closer, but you left out the third parameter, the string count. With the third paramter missing MID will return the characters from the fifth position to the end. In this case, the fifth and sixth character. So was it a typo?
So the answer should be: If mid("MyString",5,1) = Whatever then
John... Visio MVP
>> If Left("MyString",5) = "ValueTestingFor" Then >> MsgBox "the 5th character in " & "MyString" & " is what I [quoted text clipped - 28 lines] >>> >>> David Bob Quintal - 23 Jun 2007 11:24 GMT Hello John,
I remembered that I should have added the optional parameter for the number of characters to return from the mid function as soon as I clicked send. :-(
> Actually, you are both wrong. > > The OP wanted the fifth character of a six character string. > > As usual, stevie's answer is way off base and will get the > first five characters of the string.
> Bob, you are closer, but you left out the third parameter, the > string count. With the third paramter missing MID will return [quoted text clipped - 39 lines] >>>> >>>> David
 Signature Bob Quintal
PA is y I've altered my email address.
-- Posted via a free Usenet account from http://www.teranews.com
Dale Fye - 22 Jun 2007 17:28 GMT Mid$([yourfield], 5, 1)
 Signature Email address is not valid. Please reply to newsgroup only.
> I want to write some VBA code for an Access 2000 database app that checks the > value of the 5th character in a 6 character string. I used to know how to do [quoted text clipped - 4 lines] > > David missinglinq - 22 Jun 2007 19:02 GMT Dale's code is correct, but Steve's code
If Left("MyString",5) = "ValueTestingFor" Then MsgBox "the 5th character in " & "MyString" & " is what I am looking for" Else MsgBox "OOps, wrong value!" End If
as is often the case, makes no sense at all!
"Providing Customers A Resource For Help With Access, Excel And Word Applications" has got to be the biggest lie since "Iraq has weapons of mass destruction!"
 Signature There's ALWAYS more than one way to skin a cat!
Answers/posts based on Access 2000
|
|
|