Results 1 to 6 of 6
  1. #1
    jo15765's Avatar
    jo15765 is offline Expert
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2010
    Location
    6 Feet Under
    Posts
    670

    Set Form TextBox Text From VBA

    Hi - I want to run a query and set the query result (it will always only be one) as the text for a text box. I tried this code, but I get an error of


    'The control must have focus'

    What is the proper way to achieve this?

    Code:
    Private Sub Form_Load()
    Dim dc As String
    
    dc = "Select primaryCallerName From phoneInfo;"
    
    Debug.Print dc
    Me.txtPrimaryCaller.Text = dc
    End Sub

  2. #2
    jo15765's Avatar
    jo15765 is offline Expert
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2010
    Location
    6 Feet Under
    Posts
    670
    And I just tried to do
    Code:
    Me.txtPrimaryCaller.Value = dc
    but this sets the default text of the textbook to the literal string not the value that should be returned from the string, lol

  3. #3
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,518
    You can't set a control source to SQL. You could set a combo's row source to it, and then select the first item programmatically. You could use a DLookup() to get the value.

    Me.txtPrimaryCaller.Value = DLookup(...)


    You realize that SQL will return the first record found, not necessarily any particular record.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  4. #4
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,737
    In order to set the Text property of a control, it must have the focus. Would think you would want to set the value instead, but if it worked at all as you have it, you'd simply make the field hold the entire string and not some query result (which I guess you pointed out in your 2nd post). Anyway, at least now you know the reason for the message.
    Last edited by Micron; 06-05-2019 at 10:04 PM. Reason: added comment
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  5. #5
    jo15765's Avatar
    jo15765 is offline Expert
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2010
    Location
    6 Feet Under
    Posts
    670
    Quote Originally Posted by pbaldy View Post
    You can't set a control source to SQL. You could set a combo's row source to it, and then select the first item programmatically. You could use a DLookup() to get the value.

    Me.txtPrimaryCaller.Value = DLookup(...)


    You realize that SQL will return the first record found, not necessarily any particular record.
    DLOOKUP - I always forget about this amazing tool!

    That solved it, thanks!

  6. #6
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,518
    Happy to help!
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 3
    Last Post: 02-01-2018, 03:10 AM
  2. Replies: 6
    Last Post: 02-26-2016, 05:28 AM
  3. AutoFit Text size to display in a form textbox
    By QA_Compliance_Advisor in forum Programming
    Replies: 6
    Last Post: 08-08-2015, 05:00 PM
  4. Restricting Access Form Textbox to Text only.
    By Accession in forum Programming
    Replies: 4
    Last Post: 09-30-2011, 01:51 PM
  5. Replies: 3
    Last Post: 03-28-2009, 06:06 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums