Results 1 to 12 of 12
  1. #1
    JasonMann1979 is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2013
    Posts
    10

    How to populate a textbox with a SQL Select statement

    I'm trying to do the following.



    I have a text box named Last_Check on a form.

    On that form load I want to use the following SQL statement to populate that text box.

    SELECT TOP 1 tbl_QA_Check_Sheets.Machine, tbl_QA_Check_Sheets.The_Date, tbl_QA_Check_Sheets.Time
    FROM tbl_QA_Check_Sheets
    ORDER BY tbl_QA_Check_Sheets.ID DESC;


    I've tried everything I can think of and can't get it to work.

    Thanks!

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    You can open a recordset on that SQL and get the value from there.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    AFAIK, doesn't work because can't populate a textbox with a query result.

    Do a saved query object that returns the TOP 1 for each group then include that query in the form/report RecordSource

    If joining query in the form or report RecordSource is not appropriate, an expression using DLookup against that query might serve.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  4. #4
    Dal Jeanis is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2013
    Location
    Dallas TX
    Posts
    1,742
    What is it that you actually want in the text box? You're returning three values...

    It looks like you just want a value from the record with the MAX ID from tbl_QA_Check_Sheets.

    You can easily run some VBA to acquire and load that value. The question is, what is that information doing for the user? If the user just needs to know the machine, date and time of the last check that anyone did on any machine, then put three text boxes up there, inactive so they can't be changed, and load the information either at load, on current, or at whatever events make sense.

    On the other hand, if that information is there for a functional reason, then knowing a little more about how you plan to use it will help us give you more useful advice.

  5. #5
    JasonMann1979 is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2013
    Posts
    10
    Quote Originally Posted by Dal Jeanis View Post
    What is it that you actually want in the text box? You're returning three values...

    It looks like you just want a value from the record with the MAX ID from tbl_QA_Check_Sheets.

    You can easily run some VBA to acquire and load that value. The question is, what is that information doing for the user? If the user just needs to know the machine, date and time of the last check that anyone did on any machine, then put three text boxes up there, inactive so they can't be changed, and load the information either at load, on current, or at whatever events make sense.

    On the other hand, if that information is there for a functional reason, then knowing a little more about how you plan to use it will help us give you more useful advice.
    I'm trying to show the newest record added to a table. I need to show three fields; Machine, Date and time so I can show the user the last machine they checked on a date at a time. I thought I had it using DLast, but I found out a few days later that I wasn't actually getting the newest record from the table. This is what I had that I thought was working, but it seems to show a random record. Using my query above I was able to get the values I need, but I need to be able to show those values to the user on the form that they use. Thanks guys.


    Private Sub Form_Load()




    On Error GoTo Err_Handler
    On Error Resume Next


    Dim Mach As String
    Dim DTE As String
    Dim TME As String


    Mach = DLast("Machine", "tbl_QA_Check_Sheets")
    DTE = DLast("The_Date", "tbl_QA_Check_Sheets")
    TME = DLast("Time", "tbl_QA_Check_Sheets")


    Me.Last_Check = Mach & " on " & DTE & " at " & TME


    Exit_Err_Handler:
    Exit Sub


    Err_Handler:
    MsgBox Err.Description
    Resume Exit_Err_Handler



    End Sub

  6. #6
    JasonMann1979 is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2013
    Posts
    10
    Click image for larger version. 

Name:	Capture.JPG 
Views:	11 
Size:	62.1 KB 
ID:	13959

    Here is a screenshot of the form my users use to give you a better idea of what I'm trying to do.

  7. #7
    JasonMann1979 is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2013
    Posts
    10
    I got it. I ended up using a Dlookup and pointing it to my query.

    Private Sub Form_Load()




    On Error GoTo Err_Handler
    On Error Resume Next


    Dim Mach As String
    Dim DTE As String
    Dim TME As String




    Mach = DLookup("Machine", "qry_QA_Checks_Last")
    DTE = DLookup("The_Date", "qry_QA_Checks_Last")
    TME = DLookup("Time", "qry_QA_Checks_Last")


    Me.Last_Check = Mach & " on " & DTE & " at " & TME


    Exit_Err_Handler:
    Exit Sub


    Err_Handler:
    MsgBox Err.Description
    Resume Exit_Err_Handler


    End Sub

  8. #8
    Dal Jeanis is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2013
    Location
    Dallas TX
    Posts
    1,742
    I was thinking something more like this, but that should work.
    Code:
    Dim IDMax AS Long
    Dim Mach As String
    Dim DTE As String
    Dim TME As String
    
    IDMax = Dmax("[ID]","[tbl_QA_Check_Sheets]")
    Mach = DLookup("[Machine]", "[tbl_QA_Check_Sheets]","[ID]=" & IDMax )
    DTE = DLookup("[The_Date]", "[tbl_QA_Check_Sheets]","[ID]=" & IDMax )
    TME = DLookup("[Time]", "[tbl_QA_Check_Sheets]","[ID]=" & IDMax )

  9. #9
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    I was thinking of making 1 trip to the data instead of several:

    Code:
      Dim strSQL  As String
      Dim db      As DAO.Database
      Dim rs      As DAO.Recordset
    
      Set db = CurrentDb()
      
      strSQL = "SELECT TOP 1 ..."
      Set rs = db.OpenRecordset(strSQL, dbOpenDynaset)
    
      Textbox = rs!FieldName & ...
    
      set rs = nothing
      set db = nothing
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  10. #10
    JasonMann1979 is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2013
    Posts
    10
    I like the idea of one trip and no queires too. I am going to play around with the code above to see if I can get it to work. When I did this before I kept getting #name?

  11. #11
    Dal Jeanis is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2013
    Location
    Dallas TX
    Posts
    1,742
    I thought about that, too, but it seems a bit of overhead to build a recordset just for a single read.

    My rule in this kind of situation is: Do (1) whatever works (2) that will be blatantly obvious to me when I read it a year from now.

  12. #12
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Quote Originally Posted by Dal Jeanis View Post
    I thought about that, too, but it seems a bit of overhead to build a recordset just for a single read.
    I agree, but you're making 4 reads, not 1; each domain aggregate function reads the table separately. I'm not saying the difference will be huge; it may not even be noticeable. I just like to use the more efficient methods throughout my apps, on the assumption that many small gains will lead to an overall more efficient app.
    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. Auto Populate TextBox
    By Shido151 in forum Access
    Replies: 3
    Last Post: 04-23-2013, 10:41 AM
  2. Replies: 1
    Last Post: 02-04-2013, 09:48 AM
  3. Using access calendar to populate textbox
    By khartoum in forum Access
    Replies: 14
    Last Post: 06-29-2012, 09:13 AM
  4. Replies: 2
    Last Post: 04-27-2012, 06:28 PM
  5. Unable to populate textbox based on listbox
    By marcustofeles in forum Forms
    Replies: 21
    Last Post: 10-24-2011, 07:18 AM

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