locked
disappearing double quotation mark RRS feed

  • Question

  • Double quotation mark:

    ""

    written alone into text box disappears after click on another control. And even before click (losing focus) it is invisible from the VBA level (being visible in the text box) -

    Debug.Print Forms![form name]![control name].Text

    does not print ""

    But if double quotation mark is preceded ( "") or followed ("" ) by space, is printed. However it still disappears after click on another control.

    How to preserve alone double quotation mark written into a text box ?

    Monday, December 30, 2013 11:42 PM

Answers

  • Double quotation mark:

    ""

    written alone into text box disappears after click on another control. And even before click (losing focus) it is invisible from the VBA level (being visible in the text box) -

    Debug.Print Forms![form name]![control name].Text

    does not print ""

    But if double quotation mark is preceded ( "") or followed ("" ) by space, is printed. However it still disappears after click on another control.

    How to preserve alone double quotation mark written into a text box ?

    The behavior you've observed is a feature intended to allow the user to enter a zero-length string into a text box.  As you may be aware, a text box that appears blank to the eye may be Null, or it may contain only spaces, or it may contain the zero-length string; that is, a string that contains no characters.  Normally, if a user deletes all the characters in a text box, the text box is set to Null, generally interpreted as "no data". 

    A zero-length string is data, and therefore is different from Null.  But how to enter that, if deleting all the characters sets the value of the control to Null?  The designers of Access added a special feature to text boxes to allow the entry of zero-length strings: if the user enters "" in a text box, the control interprets that as a zero-length string.

    Unfortunately, that makes it impossible to manually enter the actual text value of doubled quotation marks as the value of a text box. At least, I don't know a way to do it. You can use code or a macro to set that value in the control. For example, the line of code:

        Screen.ActiveControl = """"""

    will set the value of the active control to the text value consisting of two quotation marks in a row. You could write an Autokeys macro to do that, if it comes up often enough to be worth it.  You'd use a macro action like

        Action: SetValue
            Item= [Screen].[ActiveControl]
            Expression= """"""


    Dirk Goldgar, MS Access MVP
    Access tips: www.datagnostics.com/tips.html

    • Marked as answer by _DK Tuesday, December 31, 2013 12:43 PM
    Tuesday, December 31, 2013 5:59 AM

All replies

  • Double quotation mark:

    ""

    written alone into text box disappears after click on another control. And even before click (losing focus) it is invisible from the VBA level (being visible in the text box) -

    Debug.Print Forms![form name]![control name].Text

    does not print ""

    But if double quotation mark is preceded ( "") or followed ("" ) by space, is printed. However it still disappears after click on another control.

    How to preserve alone double quotation mark written into a text box ?

    The behavior you've observed is a feature intended to allow the user to enter a zero-length string into a text box.  As you may be aware, a text box that appears blank to the eye may be Null, or it may contain only spaces, or it may contain the zero-length string; that is, a string that contains no characters.  Normally, if a user deletes all the characters in a text box, the text box is set to Null, generally interpreted as "no data". 

    A zero-length string is data, and therefore is different from Null.  But how to enter that, if deleting all the characters sets the value of the control to Null?  The designers of Access added a special feature to text boxes to allow the entry of zero-length strings: if the user enters "" in a text box, the control interprets that as a zero-length string.

    Unfortunately, that makes it impossible to manually enter the actual text value of doubled quotation marks as the value of a text box. At least, I don't know a way to do it. You can use code or a macro to set that value in the control. For example, the line of code:

        Screen.ActiveControl = """"""

    will set the value of the active control to the text value consisting of two quotation marks in a row. You could write an Autokeys macro to do that, if it comes up often enough to be worth it.  You'd use a macro action like

        Action: SetValue
            Item= [Screen].[ActiveControl]
            Expression= """"""


    Dirk Goldgar, MS Access MVP
    Access tips: www.datagnostics.com/tips.html

    • Marked as answer by _DK Tuesday, December 31, 2013 12:43 PM
    Tuesday, December 31, 2013 5:59 AM
  • if the user enters "" in a text box, the control interprets that as a zero-length string

    Unfortunately, that makes it impossible to manually enter the actual text value of doubled quotation marks as the value of a text box.

    That's good, I don't have to modify the code because it is impossible !

    Screen.ActiveControl = """"""

    could be used if content of the box would be known, but it is not known when "" alone is typed because "" can't be red.

    Tuesday, December 31, 2013 12:43 PM
  • That's good, I don't have to modify the code because it is impossible !

    Screen.ActiveControl = """"""

    could be used if content of the box would be known, but it is not known when "" alone is typed because "" can't be red.

    I should never have said "impossible".  I just figured out a way to make it work, for a given text box.  You can capture the user's keystrokes in the text box and decide, in the text box's AfterUpdate event, whether the text box should really hold the character combination (""). 

    Suppose you have a text box on a form named "txtMyTextBox.  Then you could use a module-level string variable in the form's module to capture keystrokes made in the text box, and code in events to work with it.  Something like this:

    Option Compare Database
    Option Explicit
    
    Dim strTextboxData As String
    
    Private Sub txtMyTextbox_AfterUpdate()
        
        If Me.txtMyTextbox = vbNullString _
        And strTextboxData = """""" _
        Then
            Me.txtMyTextbox = strTextboxData
        End If
    
    End Sub
    
    Private Sub txtMyTextbox_GotFocus()
    
        strTextboxData = ""
        
    End Sub
    
    Private Sub txtMyTextbox_KeyPress(KeyAscii As Integer)
    
        If KeyAscii > 13 Then
            strTextboxData = strTextboxData & Chr(KeyAscii)
        ElseIf KeyAscii = vbKeyBack Then
            If Len(strTextboxData) > 0 Then
                strTextboxData = Left(strTextboxData, Len(strTextboxData) - 1)
            End If
        End If
        
    End Sub
    

    I gave that a very quick run-through, and it seemed to work. Note that it may not work in all cases, and certainly won't work if the user pastes the quotes into the text box rather than typing it, but it *may* be good enough.  At least, it's a proof of concept.

    Dirk Goldgar, MS Access MVP
    Access tips: www.datagnostics.com/tips.html

    Tuesday, December 31, 2013 5:55 PM
  • But what about Copy - Paste data ? The KeyPress event would not detect this.
    Tuesday, December 31, 2013 6:02 PM
  • But what about Copy - Paste data ? The KeyPress event would not detect this.

    Yes, that's what I said.  Only you can say whether a user is likely to post two quotes, all by themselves, into the text box.  Unfortunately, my experiment shows that the operation to "fix" the doubled quote takes place before the Change event fires, or the Change event could be used to handle both typing and pasting.  But that proved a no-go.

    To attempt to deal with both typing and copy/pasting would probably involve low-level programming to hook the text box's window procedure and trap messages sent to it by Windows.  To me, that seems like way more work than it's worth.


    Dirk Goldgar, MS Access MVP
    Access tips: www.datagnostics.com/tips.html

    Tuesday, December 31, 2013 6:17 PM
  • way more work than it's worth
    Yes !
    Tuesday, December 31, 2013 6:23 PM
  • way more work than it's worth

    Yes !

    Hi _DK,

    Is a single doublequote or a double singlequote also sufficient. These values can be stored in a field.

    Imb.

    Wednesday, January 1, 2014 12:08 AM