Frustrating text box result (1 Viewer)

kirkm

Registered User.
Local time
Today, 22:43
Joined
Oct 30, 2008
Messages
1,167
Give up time! After nearly 2 days I can't figure why an Access Textbox is behaving strangely.
I'm pasting some text in and using the keydown event to reformat that text.
The form has one textbox only and its Enter key property is set to newline. Code is
Code:
Private Sub Form_Load()
    Text0 = "a) Hutch  (HMV BD 1054)" & vbCrLf
    Text0 = Text0 & "b) Ambrose & His Orchestra v/Anne Shelton  (Decca F 8344)" & vbCrLf
    Text0 = Text0 & "c) Turner Layton  (Columbia FB 2901)" & vbCrLf
End Sub

Private Sub text0_KeyDown(KeyCode As Integer, Shift As Integer)
    If KeyCode = 13 Then
        Dim a As Variant, Dat As String
        a = Split(CRTrim3(Text0.Text), vbCrLf)
        Dat = a(UBound(a))
        If InStr(Dat, "_") > 0 Then
            Dat = Mid(Dat, 7)
            Dat = Replace(Dat, " ", ") ", 1, 1)
            Dat = Replace(Dat, " (", "  (", 1, 1)
            a(UBound(a)) = Dat
            With Me
            .Text0.Text = ""  'This shouldn't be necessary
            .Text0.Text = Join(a, vbCrLf)
            End With
        End If
    End If
End Sub

Private Function CRTrim3(ByVal c) As String
'Remove any tailing crlf from c
    If Len(c) > 0 Then
        Do
            Select Case Asc(Right(c, 1))
                Case 13, 10
                    c = Left(c, Len(c) - 1)
                Case Else
                    Exit Do
            End Select
        Loop Until Len(c) = 0
    End If

When the form opens I click on the 4th line (the first blank one) and paste in 43_028d Joe Loss & His Orchestra (HMV BD 5816) and hit enter.
Why (after it reformats the pasted line as instructed ) does it then add a new line at the start of the textbox?
Printing Asc(text0) shows its 97 which is not a linefeed.
 

arnelgp

once i caught a fish alive...
Local time
Today, 18:43
Joined
May 7, 2009
Messages
13,267
this is a simple replace code:
Code:
Private Sub text0_KeyDown(KeyCode As Integer, Shift As Integer)
Dim t As String
If KeyCode = 13 Then
    t = Me.Location.Text & vbNullString
    If Len(t) > 0 Then
        Do While InStr(1, t, "  ") > 0
            t = Replace$(t, "  ", " ")
        Loop
        t = Replace$(Trim$(t), " ", vbCrLf)
    End If
    With Me.text0
        .Text = t
        .SelStart = Len(t)
        .SelLength = 0
    End With
End If
End Sub
 

kirkm

Registered User.
Local time
Today, 22:43
Joined
Oct 30, 2008
Messages
1,167
Tahnks Arne your With-End With (which i don't understand at all) seems to fix everything. I swiped that into mine and it works as hoped,
But what did it put in the extra line at the start ?
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 11:43
Joined
Jul 9, 2003
Messages
13,247
With-End With (which i don't understand at all)

The with end-with is just a shorthand to save you typing out lots of code. Before using with end-with your code would look like this:-

Code:
        Me.text0.Text = t
        Me.text0.SelStart = Len(t)
        Me.text0.SelLength = 0

After implementing with end-with your code looks like this:-

Code:
    With Me.text0
        .Text = t
        .SelStart = Len(t)
        .SelLength = 0
    End With

With the with end-with block this piece of code only appears once:- Me.text0
 

kirkm

Registered User.
Local time
Today, 22:43
Joined
Oct 30, 2008
Messages
1,167
I wish there was some eureka moment but only adds to the puzzlement.
I follow that .Text is being assigned to t which is the changed/fixed text.
Then you set a selection to zero from the last char in t. Is that right ?
How does that prevent the mysterious extra line ?
 

Isaac

Lifelong Learner
Local time
Today, 03:43
Joined
Mar 14, 2017
Messages
5,386
I have a feeling your function wasn't doing anything like you thought it was.

I don't use Functions the way you have, with ByVal, changing C and then not assigning the function any value.
I leave off byval and byref and let it default, and don't forget to assign a value to the function before exiting!

Also, I changed Private Function to Public Function (or just Function) and put it on a separate Module. This makes it much easier to troubleshoot!

I didn't validate all your code, but to at least get to the point where it might do what you expect, try this:

Code:
Public Function CRTrim3(c As String) As String
'Remove any tailing crlf from c
    If Len(c) > 0 Then
        Do
            Select Case Asc(Right(c, 1))
                Case 13, 10
                    c = Left(c, Len(c) - 1)
                Case Else
                    Exit Do
            End Select
        Loop Until Len(c) = 0
    End If
CRTrim3 = c
End Function

Now that is a function that really does trim the last line break off of a string - AND returns the actual newly minted string back to the caller.
And it's one you can easily and actually test in the Immediate window, without any form displayed.

1612800947133.png
 

kirkm

Registered User.
Local time
Today, 22:43
Joined
Oct 30, 2008
Messages
1,167
Hi Isaac, did you find something not working with my CRTrim function? Unless I'm wrong, instead of returning a value, it changes variable "C" which is used by the calling routine. But CRTrim wasn't the problem. The main use for the routine was to reformat the last line (only) in the textbox. Then replace all the text. The problem was it added what looks like (but isn't) an extra CR at the start. There's no code instruction to do that and the asc value of the textbox is 97. You would agree if you put some text in a textbox you'd expect it to be at row 1, LH side, not starting on Line 2?
 

Isaac

Lifelong Learner
Local time
Today, 03:43
Joined
Mar 14, 2017
Messages
5,386
I don't think you can use a function like that and expect it to actually change the textbox.

If you really must do a function that way, yes, you can do that - IF you pass a variable into the function, then it will change the variable. I am not sure it will change the text box. The way you are using it you are expecting it to change the textbox value - you are not actually passing a variable into it; you're passing a control value.
 

kirkm

Registered User.
Local time
Today, 22:43
Joined
Oct 30, 2008
Messages
1,167
I'm really up for being corrected, but I don't see what's wrong. Isn't this code passing text into the textbox ?
me.Text0.Text = Join(a, vbCrLf)
'a' is my text array which is joined with crlf. I don't follow this being a "control value" ?
It is doing what's wanted, except adding a extra line at the start. Arne and Uncle Gizmo have corrected that with Selstart, Sellen commands but I was hoping to find why it happens in the first place.
 

Isaac

Lifelong Learner
Local time
Today, 03:43
Joined
Mar 14, 2017
Messages
5,386
Have you stepped through the code using F8? This would reveal the problem. I wish I could, but after replicating your situation EXACTLY - form name, form load code, textbox, pasting the value in, etc., I get a runtime error because the variant a has no levels to its expected array at all!. I get a runtime error on the line:
Dat = a(UBound(a))
(subscript out of range)
.... which is because what you expect a to be, an array, turns out to be nothing........

... which is because this line:

a = Split(CRTrim3(Text0.Text), vbCrLf)
returns nothing but a zero length string, because your CRTrim3 function is expected to be passed a variable, then you do something with the variable, but that's not what you're doing.

Attached is the database, following your described scenario to a T. It doesn't even run.

If you can get this database to the point where it actually runs and I can replicate the problem you're describing, I will debug further.
This would be MUCH easier if you could do the function the way I showed, where the function actually returns a value. But if you are going to do it the way you have posted, then you need to actually HAVE a variable for CRTrim3 to change in the first place. Like:
dim strSomething as string
strSomething=CRTrim3(Text0.text)
...continue to use strSomething
 

Attachments

  • Database5.accdb
    376 KB · Views: 73

kirkm

Registered User.
Local time
Today, 22:43
Joined
Oct 30, 2008
Messages
1,167
Your a contains nothing, but mine does. Just figuring out why....
 

kirkm

Registered User.
Local time
Today, 22:43
Joined
Oct 30, 2008
Messages
1,167
Whoops, something went wrong pasting in the code - the last 2 lines were missing. Sorry I didn';t nortice that.

Code:
Private Function CRTrim(ByVal c) As String
    If Len(c) > 0 Then
        Do
            Select Case Asc(Right(c, 1))
                Case 13, 10
                    c = MyTruncate(c, 1)
                Case Else
                    Exit Do
            End Select
        Loop Until Len(c) = 0
    End If
    CRTrim = c
End Function
 

Isaac

Lifelong Learner
Local time
Today, 03:43
Joined
Mar 14, 2017
Messages
5,386
No wonder I got confused, see why I thought there was something about the function? ok let me try some more digging now
 

Isaac

Lifelong Learner
Local time
Today, 03:43
Joined
Mar 14, 2017
Messages
5,386
But now it doesn't line up. Is it private function crtrim or crtrim3?? The other code refers to crtrim3
 

kirkm

Registered User.
Local time
Today, 22:43
Joined
Oct 30, 2008
Messages
1,167
It is CrTRim3. I have a few copies while tying to not go slowly mad..
Your db is attached with new Form 11 that fails and your Form 1 that works. Why can't I see the differnce?
The bit remmed out is Uncle Gizmo's fix... if I rem out mine and replace it works.
Yours just works.. no sel len etc.
Pity about this emissing lines it's really mucked all this up.
 

Attachments

  • Database5a - Copy.accdb
    672 KB · Views: 31

Users who are viewing this thread

Top Bottom