Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing the first character of a string if it equals something

Tags:

vba

ms-access

I am practicing VBA for Access 2010.

I read all the suggested post regarding my post but did not find anything specific. I know how to move specific characters in a string, what I do not know is how I can remove specific character that equals to something.

I want to move the character 1 or 1- from telephone numbers if one is there.

Example: 17188888888 to 7188888888 or 1-7188888888 to 7188888888

I am trying to use an if statement starting first with just removing the 1.

The phone number is entered as a string not numbers.

This is what I have started: I am getting an error message that RemoveFirstChar is ambiguous.

Public Function RemoveFirstChar(RemFstChar As String) As String
If Left(RemFstChar, 1) = "1" Then
  RemFstChar = Replace(RemFstChar, "1", "")
End If
RemoveFirstChar = RemFstChar
End Function
like image 563
Asynchronous Avatar asked Dec 26 '11 05:12

Asynchronous


People also ask

How do I remove the first letter of a string in R?

To remove the string's first character, we can use the built-in substring() function in R. The substring() function accepts 3 arguments, the first one is a string, the second is start position, third is end position.


2 Answers

I have tested your function in Access 2010 and it worked just fune.. You can also use this code:

Public Function RemoveFirstChar(RemFstChar As String) As String
Dim TempString As String
TempString = RemFstChar
If Left(RemFstChar, 1) = "1" Then
    If Len(RemFstChar) > 1 Then
        TempString = Right(RemFstChar, Len(RemFstChar) - 1)
    End If
End If
RemoveFirstChar = TempString
End Function
like image 122
Karel Avatar answered Sep 20 '22 13:09

Karel


There is nothing particularly wrong with your code, the "ambiguous" error message in this context is very likely to be because you have another sub or function in a different module with the same name. Search to find the duplicate name.

If you put the function in the module belonging to a form or report, it is probably best to skip "Public". If you intend the function to be used by several forms, create a new module that is not attached to a form and put the functions that are intended for all forms and reports in that.

It is nearly always good to provide the full error message and error number.

like image 22
Fionnuala Avatar answered Sep 20 '22 13:09

Fionnuala