Results 1 to 3 of 3
  1. #1
    Join Date
    May 2012
    Location
    Australia
    Posts
    23

    Question Unanswered: How can do i specify a control name that has a space in vb?

    Hi,

    I am writing some vb so that when a field is clicked it asks for a password. If the correct password is entered, the field is unlocked, otherwise it stays locked.

    The structure of the code is not a problem. What the problem is, is that my text box name has a space in it. Rather than change the name which will affect other code, i wanted to find out if it is possible to refer to a control name that has a space.

    This code works fine with the text box name "Notes"

    Working Code
    Code:
    Private Sub Notes_Click()
    Dim adminpw As String
    adminpw = InputBox("Enter Admin Password", "Admin Only!")
    If adminpw = "AdminPassword" Then
    Me!Notes.Locked = False
    Else
    Me!Notes.Locked = True
    MsgBox "Field Cannot Be Edited", vbExclamation, "Admin Only!"
    End If
    End Sub
    Problem Code
    Code:
    Private Sub Product_Name_Click()
    Dim adminpw As String
    adminpw = InputBox("Enter Admin Password", "Admin Only!")
    If adminpw = "AdminPassword" Then
    Me!Product Name.Locked = False
    Else
    Me!Product Name.Locked = True
    MsgBox "Field Can Only Be Edited by Admin", vbExclamation, "Admin Only!"
    End If
    End Sub
    I have tried with an underscore between Product and Name but it just has an error. I have also tried quotes around "Product Name".

    Is there a way to refer to a control name with a space in vb? I know one thing for sure...No More Spaces!

  2. #2
    Join Date
    May 2012
    Location
    Australia
    Posts
    23
    Just worked it out. I can use square brackets eg. [Product Name] and its fine. Tried one thing too few before posting.

    Thanks for your help heheh

  3. #3
    Join Date
    Jun 2005
    Location
    Richmond, Virginia USA
    Posts
    2,763
    Provided Answers: 19
    Square Brackets, as in [Product Name], is the usual method of handling this problem, in VBA code, but Product_Name, with the Underscore, should have worked, as well!

    If you
    • Place a Textbox Control on your Form
    • Name it Product Name (with a Space)
    • Go to the Property - Events and Click on After Update to go to the Code Module

    Access will generate this, in the Code Module:

    Code:
    Private Sub Product_Name_AfterUpdate()
    
    End Sub
    with an Underscore between Product and Name.

    I'm also confused by your statement "Rather than change the name which will affect other code!" If the name appears elsewhere, you'll have to do the same thing there, as well, so what have you saved?

    And yes, 'No More Spaces' is an excellent policy! Either starting out with [Product_Name], to begin with, or my preference, [ProductName], is far, far better! The problem with Square Brackets is that there are several other situations, in Access apps, when they are required, and the Access Gnomes can become confused, at times, because of this!

    Linq ;0)>
    Last edited by Missinglinq; 06-11-12 at 23:18.
    Hope this helps!

    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •