Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Access 2007 - Instr

Posted on 2011-09-25
11
Medium Priority
?
283 Views
Last Modified: 2012-05-12
My OS is windows 7 and I use Access 2007.  I have a Field which is called Name is  a Text type.
The data in this field contains the first, middle and last name separated by a space.
I would like to separate the first and middle names and put them in a field called First Name
And the last name and put them in a field caleld LastName.
I am trying to use an ADO function to loop thru all the names, and then find the spaces.
Thus if the data has only one space, then the first part would have to be the first name and the rest goes into the last name.
But if the data has more than one spaces, i.e. eg : John Phil Smith, then John Phil would go into the first name and Smith to the last name.  
Is using the InStrr method to find this space the correct way to do it.
The the code to my mind would be like :
 If 1 space, Then
     take all data to the left of that space and put it into a var called First Name
     and all data to the right of that space, put it into a var called Last Name
 Now update the record in the Table.
Go on looping until end of file.

Hope some expert could  help me with the coding, and if possible also the ADO code.  
The name of the Table is called Seniors Club
 The field name where the data is to be extracted from is called Name, and is a text type field.
The firstname field is text type and also the lastname field is also text type.
Thank u.
0
Comment
Question by:jegajothy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 2
11 Comments
 
LVL 13

Expert Comment

by:lee555J5
ID: 36596632
First, "Name" is a reserved word in Access, and you should not use is for anything else. Use something else such as FirstName, LastName, or FullName. You could put it in [] as in [Name], but it's best to avoid it altogether.

Second, Instr is the command to use. This is only valid if all your names to split are constructed according to your example rules, as in at most 2-3 words separated by spaces. What about Jr. Sr. II? What about hyphens? What about 4 or more names?

Just some thoughts.

Lee
0
 
LVL 13

Expert Comment

by:lee555J5
ID: 36596644
Sorry, the command to use to find the last space is InStrRev.

FirstName = Mid([Name], 1, InstrRev([Name], Chr(32)))
LastName = Mid([Name], InstrRev([Name], Chr(32)))
0
 

Author Comment

by:jegajothy
ID: 36596743
In response to lee, thank u, looks like I am out of date of the new InStrRev command.
Thank u.  Hope someone could help me  with the coding.
0
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
LVL 85
ID: 36598240
What do you mean "out of date" with the InStrRev command? Do you mean you cannot use it, or do you have some other issue? The comment by lee should answer your question completely, and should give you the correct values (with the caveat, as indicated by lee, that  your data will have to be in a specific format in order for this to work reliably).
0
 
LVL 13

Expert Comment

by:lee555J5
ID: 36598901
Thank you, LSM.

jegajothy, InStrRev is not new. Where InStr searches from the start of a string, InStrRev starts its search from the end of the string.

Basically, you need to build your connection and run an UPDATE SQL statement. Look here for example connection strings if connecting to another db.

About the code:
It's only a basic shell, and I may have missed a line or two. This should get you started. Add your own error handling and garbage cleanup.

Bonus Tip
Try not to use spaces in object or field names as in Seniors Club. You can, but.... Any time you access it, you have to enclose it with [] as in [Seniors Club]. It's just best to avoid. If you can, try something like tblSeniorsClub. You may not be able to change it, such as with legacy or inherited dbs where it's not worth the effort.
Dim cnn As New ADODB.Connection, cmd As New ADODB.Command
Set cnn = CurrentProject.Connection
cmd.Connection = cnn
cmd.CommandText = "UPDATE [Seniors Club] SET [FirstName] = Mid([Name], 1, InstrRev([Name], Chr(32))), _
    [LastName] = Mid([Name], InstrRev([Name], Chr(32)));"
cmd.Execute

Open in new window

0
 

Author Comment

by:jegajothy
ID: 36602557
in response to LSM consulting, I am refering to my knowledge of vb as out of date as it was many moons (>7 years) that I dabbled in vb.  NOT the responder. thank u.
0
 

Author Comment

by:jegajothy
ID: 36602626
In response to Lee's coding response.  I copied and pasted, but it gave me a User defined type not defined error at the line :
              Dim cnn As New ADODB.Connection
My OS is windows 7 and I am using Access 2007.
Thank u.
0
 
LVL 13

Expert Comment

by:lee555J5
ID: 36662147
That error sounds as if the proper reference isn't set. I thought it would've been because you asked specifically for ADO code. Anyway, open the window for the code and go to Tools -> References.... If it's not checked at the top, scroll to Microsoft ActiveX Data Objects x.x Library and check the box for the one with the highest x.x rev. For Access 2007, I think it should be 2.8.
0
 

Author Comment

by:jegajothy
ID: 36710294
In response to Lee, in the first time I was able to go to the design mode of the Form, then opened the code,  and I was able to click on the Tools, and References showed up.  As suggested I checked the 2.8 but I also saw a 6.0 version too.   When I ran the code again, I got the same error.  I am lost in what is wrong. Thank u.
0
 
LVL 85

Accepted Solution

by:
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 2000 total points
ID: 36711423
Did you get the same error on the same line?

If you're doing this in Access, and if you have linked tables, then you don't really need to use ADO. Just run the SQL statement directly:

Currentdb.Execute "UPDATE [Seniors Club] SET [FirstName] = Mid([Name], 1, InstrRev([Name], Chr(32))), _
    [LastName] = Mid([Name], InstrRev([Name], Chr(32)));"
0
 

Author Comment

by:jegajothy
ID: 36720127
In response to LSMConsulting, the tabes are not linked but is in the file as the mdb file.  Thank u
0

Featured Post

TCP/IP Network Protocol Cheat Sheet

TCP/IP is a set of network protocols which is best known for connecting the machines that make up the Internet. The truth is that TCP/IP is one of the oldest network protocols and its survival is mainly based on its simplicity and universality.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
Instead of error trapping or hard-coding for non-updateable fields when using QODBC, let VBA automatically disable them when forms open. This way, users can view but not change the data. Part 1 explained how to use schema tables to do this. Part 2 h…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

704 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question