I think there are a couple of better ways to do this. One choice would be to
use the Switch function:
strTitle = "Mr."
strTitle = Replace(strTitle, ".","") 'Remove periods for consistency
strGender = Switch(strTitle = "Mr", "M", strTitle = "Mrs", "F", strTitle =
"Miss", "F")
You said you could hit about 90%. I think that is optimistic. Dr, for
example is non gender specific, so will be a problem.
Another more dynamic solution would be to use a table with two fields, Title
and Gender. Then use the DLookup to determine the Gender. This way, when
you get one in that is not in your list, it is easy to add it to the table
rather than have to modify your code.
strGender = Nz(DLookup("[Genter]", "tblGenderTitles", "[Title] = '" &
Me.txtTitle & "'"),"U")
Agent Dagnamit said:
Thanks everyone, I didnt appreciate the importance of line spacing, the code
now works.
PS - yes, dealing with titles like Dr, Prof, etc isnt easy, but if I can
establish 90% of genders, I'll be happy enough
:
It should look like this, instead:
----------
Public Sub test_ifthenelse()
Dim title, gend As String
title = "Mrs"
If title = "Mr" Then
gend = "M"
ElseIf title = "Mrs" Then
gend = "F"
End If
End Sub
----------
Notice, I placed space between the lines, and also indented where necessary.
Code indentation is very important, because it's much easier to spot where a
block begins/ends, and especially when you begin nesting loops inside of
Select Case staments, nested in If statements that are nested in other loops
that are nested in If statements, that are also nested inside Select Case
Statements.
I'm still new to VBA and trying to write a simple function to derive
gender
from title, like this:
Public Sub test_ifthenelse()
Dim title, gend As String
title = "Mrs"
If title = "Mr" Then gend = "M"
ElseIf title = "Mrs" Then gend = "F"
End If
when this compliles, Access puts a colon ":" between the Else and If, and
when it runs, I get an "Else without if" error message - I'm tearing my
hair
out and cant see what I'm going wrong.
Help?