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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Newbie Question...IF-Then_Else Error

Status
Not open for further replies.

ChiTownDiva

Technical User
Jan 24, 2001
273
US
I keep getting an error message on a module I'm writing:

Public Function StripBegin(MyString As String, _
strSearch As String) As String

If MyString Like "0*" then strSearch = "0"
Else
If MyString Like "1*" then strSearch = "1"
Else
If MyString Like "2*" then strSearch = "2"
else
If MyString Like "3*" then strSearch = "3"
Else
If MyString Like "7*" then strSearch = "7"
Else
If MyString Like "A*" then strSearch = "A"
Else
If MyString Like "B*" then strSearch = "B"
Else
If MyString Like "C*" then strSearch = "C"
Else
If MyString Like "D*" then strSearch = "D"
Else
If MyString Like "F*" then strSearch = "F"
Else
If MyString Like "H*" then strSearch = "H"
Else
If MyString Like "K*" then strSearch = "K"
Else
If MyString Like "L*" then strSearch = "L"
Else
If MyString Like "NN*" then strSearch = "NN"
Else
If MyString Like "R*" then strSearch = "R"
Else
If MyString Like "W*" then strSearch = "W"
Else
If MyString Like "Y*" then strSearch = "Y"
Else
If MyString Like "ND*" then strSearch = "ND"
Else
If MyString Like "NP*" then strSearch = "NP"
Else
If MyString Like "NS*" then strSearch = "NS"

end if

StripBegin = strSearch

End Function

I keep getting an error saying that I have an "Else" missing...I don't see it.


Thanks [ponder]
 
Change your else (except your last else) to elseif and remove your ifs (except your first else)

An alternate solution is

If left$(mystring,2) in ('NN','ND','NS','NP') then
strSearch = left$(mystring,2)
elseif left$(mystring,1) in ('0','1','2','3','7','A','B','C','D','F','H','K','L','R','W','Y')
strSearch = left$(mystring,1)
end if

StripBegin = strSearch


 
To answer your question, VB requires you to concatenate your Else and If keywords to form 'ElseIf.' Try the following:

Code:
Public Function StripBegin(MyString As String, strSearch As String) As String

  If MyString Like "0*" Then
    strSearch = "0"
  ElseIf MyString Like "1*" Then
    strSearch = "1"
  ElseIf MyString Like "2*" Then
    strSearch = "2"
  ElseIf MyString Like "3*" Then
    strSearch = "3"
  ElseIf MyString Like "7*" Then
    strSearch = "7"
  ElseIf MyString Like "A*" Then
    strSearch = "A"
  ElseIf MyString Like "B*" Then
    strSearch = "B"
  ElseIf MyString Like "C*" Then
    strSearch = "C"
  ElseIf MyString Like "D*" Then
    strSearch = "D"
  ElseIf MyString Like "F*" Then
    strSearch = "F"
  ElseIf MyString Like "H*" Then
    strSearch = "H"
  ElseIf MyString Like "K*" Then
    strSearch = "K"
  ElseIf MyString Like "L*" Then
    strSearch = "L"
  ElseIf MyString Like "NN*" Then
    strSearch = "NN"
  ElseIf MyString Like "R*" Then
    strSearch = "R"
  ElseIf MyString Like "W*" Then
    strSearch = "W"
  ElseIf MyString Like "Y*" Then
    strSearch = "Y"
  ElseIf MyString Like "ND*" Then
    strSearch = "ND"
  ElseIf MyString Like "NP*" Then
    strSearch = "NP"
  ElseIf MyString Like "NS*" Then
    strSearch = "NS"
  Else
    strSearch = ""
  End If
  
  StripBegin = strSearch

End Function
VBSlammer
redinvader3walking.gif
 
Thanks VBSlammer...

I didn't get an error message in the coding, but now when I go to use the function, I get the following error message:

"Undefined function 'StripBegin' in expression."

I used it as the following (in an update query):

StripBegin([tbl_EverybodyBST]![Local_Pers_ID], 2)

I used a function similar to this before, so I don't know what's wrong..

Thanks.[ponder]
 
[bugeyed] Your function defines both arguments as string values so you need to pass in strings:

Code:
StripBegin([tbl_EverybodyBST]![Local_Pers_ID], "2")

VBSlammer
redinvader3walking.gif
 
Hi VB....

I'm really new at this...how do I pass a string back to query? Am I supposed to put "StripBegin([tbl_EverybodyBST]![Local_Pers_ID], "2")" somewhere in the module?

At the moment, I have the module written as above and the update query as "Update to: StripBegin([tbl_EverybodyBST]![Local_Pers_ID], "2")". What did I miss? [sadeyes]

Thanks for your help with this!!!

ChiTownDiva [ponytails]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top