Access 2007 - Instr

Posted on 2011-09-25
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.
Question by:jegajothy
  • 5
  • 4
  • 2
LVL 13

Expert Comment

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.

LVL 13

Expert Comment

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)))

Author Comment

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.
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

LVL 84
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).
LVL 13

Expert Comment

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)));"

Open in new window


Author Comment

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.

Author Comment

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.
LVL 13

Expert Comment

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.

Author Comment

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.
LVL 84

Accepted Solution

Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 500 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)));"

Author Comment

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

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Undo Button 1 31
Database maintenance 36 98
MS Access Query Question 2 33
Having 'UserName' as a field name - seems to be causing issues 9 23
Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …

679 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