Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

REPLACE to remove spaces from postcodes 1

Status
Not open for further replies.

Cnnx

Technical User
Aug 19, 2003
16
GB
I am trying to remove spaces from a postcode field. After searching the various threads I have come across the problem associated with using REPLACE directly in Access so I followed the suggestion of declaring a public function in VB. However there is a compile error (expected identifier) in the code which I do not understand. Please can anyone tell me where my mistake is? - I'm not very confident with VB.

Access 2000
VB v.6

VB code:
Public Function DoReplace(InSightPostcode As String," " As String,"" As String) As String
DoReplace = Replace(InSightPostcode, " ", "")
End Function

Access query:
UPDATE BPCOrInSightTbl SET InSightPostcode = DoReplace(InSightPostcode, " ", "");



 
if you use this code in a module

Code:
Function RemoveSpaces(ByVal AlphaNum As Variant)

   Dim Clean As String
   Dim Pos, A_Char$

   Pos = 1
   If IsNull(AlphaNum) Then Exit Function

   For Pos = 1 To Len(AlphaNum)
      A_Char$ = Mid(AlphaNum, Pos, 1)
      If A_Char$ = " " Then
      
      Else
         Clean$ = Clean$ + A_Char$
      End If
   Next Pos

   RemoveSpaces = Clean$

End Function

this will do the trick for you.

Any other issues let me know
 
Thanks M8KWR
Although I can sort of see what the code does I am out of my depth in implementing it. Does AlphaNum stand for the field name? Also how is the table specified in the function?

I saved your code as a Function with the field name in place of AlphaNum and called it using a macro. However I get the error 'Microsoft Access can't find the name 'ByVal' you entered in the expression'.

Sorry if this is really basic stuff but I haven't found the Help files very useful on this.

Code:
Function RemoveSpaces(ByVal InSightPostcode As Variant)

   Dim Clean As String
   Dim Pos, A_Char$

   Pos = 1
   If IsNull(InSightPostcode) Then Exit Function

   For Pos = 1 To Len(InSightPostcode)
      A_Char$ = Mid(InSightPostcode, Pos, 1)
      If A_Char$ = " " Then
      
      Else
         Clean$ = Clean$ + A_Char$
      End If
   Next Pos

   RemoveSpaces = Clean$

End Function
 
what to do it change the code back to my original post.

Save this code as a new module, called it Module1, for ease.

Now it depends where you want to alter the postcode field:-

- after the user has put it in, and make sure there is no space's within the field.

- do you want to query a table and remove the space

If you can let me know this thne i gave give you want you need.

Do not worry about not knowing what to do, if you follow what i suggest then it will all work fine. Its a good way to learn, to see how things work, and why they work.

 
Thanks M8KWR
I want to run an update query on a table to remove spaces from the postcode field (so that I can match it with some 'unspaced' postcodes from another source)
 
your sql code would be.

Code:
UPDATE "Table_Name" SET "Table_Name"."Field_Name" = RemoveSpaces([Field_Name]);

all you have to do is change table_name and field_name with the relevant correct names.

If you do not want to alter the sql, then create a new query, change it to a update query. insert your postcode field, and in the "Update to" area, all you would need to enter is: -

Code:
RemoveSpaces([field_name])

Again you would need to replace field_name with your postcode field.

Any more issues or problems let me know.
 
Wonderful! Exactly what I want. Many thanks for your patience M8KWR. Never realised it would be that simple.
 
Seems a bit much:

Code:
? Replace("MyDog Has Fleas", Space(1), "")
MyDogHasFleas

as the basic function works quite reasonably. Of course I'm probably not understanding the greater complexity somewhere.





MichaelRed


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top