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

Wierd Mid() behaviour

Status
Not open for further replies.

Kennelbloke

Technical User
May 22, 2015
33
AU
Hi Folks

Got a report and in it I have 25 Text fields named txtName1 .... to ... txtName25. the fields were setup and then copied and pasted then the name changed.

I'm using the following to seperate out each char in the pet's name (me!PetName) and put it into each of the text fields (i.e. 1 char per field)

Code:
Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)
Dim vcounter As Variant
Dim tmpltr As String

    vcounter = 1
    If Me!PetName <> "" Or Not IsNull(Me!PetName) Then
        For Each ctrl In Me.Controls
            If ctrl.Name = "txtName" & vcounter Then
                tmpltr = Mid(Trim(Me!PetName), vcounter, 1)
                ctrl.Value = StrConv(tmpltr, 1)
                vcounter = vcounter + 1
                tmpltr = ""
            End If
        Next
    End If
End Sub

However when the vcounter value gets to 8, it won't +1 and resets back to 1. I've changed to vcounter to integer type and it does not make any different.

If I start the vcounter at 9 it works fine from there. I've deleted the field and recreated it and again no change. If I start vcounter from 5 it only goes to 8. If I start vcounter at 8 it only does the 8th one and resets back to 1.

This is so bazaar. Anyone possibly have an idea whats going on.

I'm off to open another bottle of wine.
 
I would rather loop with: [tt]For i=1 To Len(Trim(Me!PetName))[/tt] and refer to control named [tt]"txtName" & i[/tt].
In your code For Each uses internal order of controls, you increase vcounter if name match template and next you increase vcounter. It's a bit random.

combo
 
>I'm using the following to seperate out each char in the pet's name (me!PetName) and put it into each of the text fields (i.e. 1 char per field)
No, you don't
You take only first character from txtName1, only second character from txtName2, third character from txtName3, and so on.


---- Andy

There is a great need for a sarcasm font.
 
Hi guys

Yes combo I could limit it to the name length and will look at that

Andrzejek, isn't that the reverse of what I trying yo do. Fields txtName1, txName2 etc are empty fields at report open until I put something in them.

All I doing (or trying to do) is to take 1 letter (char) at a time from a pet's name and put it into a seperate field.

This is what happens if I set vcounter to 1

problem1_c1bpjv.png


This is what happens if I set it to 8

problem2_l9xnss.png
 
All sorted thanks guys.

It turned out to be the TAB Stop positions and even with all others tab stop turned off it still didn't work.

So... I've hard codes each field to get around the problem.

txtName1 = Mid(Trim(Me!PetName), 1, 1)
txtName2 = Mid(Trim(Me!PetName), 2, 1)

etc..

So thanks guys for responding.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top