have a user form where a user must input a text value. I am wanting to check if the input string is 2 words ore more, and if the input string is one word.
What I need is if the input string is 2 words or more, then I need to take the first character from the 1st word and 2nd word. If the input string is one word, I need to take the first two characters from the first word.
I google ninjad this code below but it seems that MachineAbbrev is always null?
What I need is if the input string is 2 words or more, then I need to take the first character from the 1st word and 2nd word. If the input string is one word, I need to take the first two characters from the first word.
I google ninjad this code below but it seems that MachineAbbrev is always null?
Code:
Option Compare Database
Option Explicit
Private Sub cmdAddMachine_Click()
Dim MachineId As String
Dim MachineName As String
Dim MachineAbbrev As String
Dim str As String
Dim NumWords As Variant
Dim c As Long
Dim Sp As Variant, s As Variant
If Not IsNull(Me.txtAddNewMachine.Value) Then
MachineName = Me.txtAddNewMachine.Value
NumWords = UBound(Split(Me.txtAddNewMachine.Value, " ")) + 1
If NumWords >= 2 Then
MachineAbbrev = GetMachineName(MachineName, NumWords)
End If
If NumWords = 1 Then
MachineAbbrev = GetMachineName(MachineName, NumWords)
End If
MachineId = ""
Debug.Print MachineAbbrev
End If
End Sub
Private Function GetMachineName(MachineName As String, NumWords As Variant)
Dim Sp, ct, s As Variant
Dim formatMachineName As String
Sp = Split(MachineName, " ")
ct = 1
For Each s In Sp
If ct >= 1 And ct <= 2 And NumWords >= 2 Then
formatMachineName = s + s
End If
If ct >= 1 And NumWords = 1 Then
formatMachineName = s
End If
Next s
End Function