-
Help with Loops
Hi
I wondered if you would be good enough to help me.
As learning exercise I have set-up a table with two fields Fld1 and Fld2 both text 50 characters.
I wanted to place four words in fld1 and using VB – BeforeUpdate on a form I wanted to take the first character of each word and place it in Fld2 separating each letter by a space. For example in fld1 I would place:
How Now Brown Cow
And expect to see in fld2
H N B C
The idea was to learn more about loops (creating codes for long naratives etc) and variables in VB (naturally I am new to VB)
In previous languages I have used values in numerical fields and strings can be incremented by adding them to themselves.
I have gotten a little punch drunk now looking at this an I'm not really sure where it's going any more.
Can you help please?
Thanks
===========================================
Private Sub Fld1_BeforeUpdate(Cancel As Integer)
Dim varFld1 As String
Dim varFld2 As String
Dim varSpace As String
Dim varPos As Integer
Dim varFld1Len As Integer
Dim varNum As Integer
varSpace = " "
varNum = 1
'Value in variable
varFld1 = Fld1
'Overall length of text
varFld1Len = Len(varFld1) - 1
Do Until varNum >= varFld1Len
'placed the first character from fld1 into fld2
If varNum = 1 Then
varFld2 = Left(varFld1, varNum)
Else
'finds the next character
varFld2 = Mid(varFld1, varNum, 1)
'places the next charater in the string separated by a space. Does not seem to concatenate the value this way
varFld2 = [varFld2] & " " & [varFld2]
End If
varPos = InStr(varNum, varFld1, varSpace)
varNum = varPos + 1
Loop
Me.Fld2 = varFld2
End Sub
-
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules