Results 1 to 9 of 9
  1. #1
    amc1902 is offline Novice
    Windows 7 64bit Access 2003
    Join Date
    Oct 2014
    Posts
    10

    Displaying Query Result in a textbox

    Hi all

    I have a query which works very well and displays the single-value result in a datafield display. What I am trying to do is to display this result in a textbox instead, located on the form.

    The SQL that I have is:
    SELECT Count(Table1.[Date Received]) AS [Count]


    FROM Table1
    WHERE (((Table1.[Date Received])>=[Select DateFrom] And (Table1.[Date Received])<=[Select Date to]));

    I am trying to display the result in textbox80, located on Form2.

    Can anybody amend the above to allow this? I've also tried using the DLookup in the textbox80 Control Source, however, I keep getting an error result.

    Any suggestions would be appreciated

    Many thanks!!


  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,550
    Create a query with this sql, THEN if you need to fill the result into a text box (or msgbox)

    txtBox = Dlookup("[Count]","qsMyTotalQry")

  3. #3
    amc1902 is offline Novice
    Windows 7 64bit Access 2003
    Join Date
    Oct 2014
    Posts
    10
    Quote Originally Posted by ranman256 View Post
    Create a query with this sql, THEN if you need to fill the result into a text box (or msgbox)

    txtBox = Dlookup("[Count]","qsMyTotalQry")
    Hi, thanks for your response - unfortunatly all I get is the #Error message in the textbox

  4. #4
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,550
    Then dont use the word count (its reserved)
    try connecting a form to the count query... connect a text box to the field TOTAL (rename the count to total)
    NOW it will show.

  5. #5
    amc1902 is offline Novice
    Windows 7 64bit Access 2003
    Join Date
    Oct 2014
    Posts
    10
    Hi, thanks for this. Pretty new to the world of Access, so this is getting a bit beyond me

    Just to recap, I have a query (qrycountBF) which is working fine but is displaying the result in a datasheet view. I would like to display it in a textbox (textbox80), located in Form2 if possible.
    The sql for the query is:

    SELECT Count(Table1.[Date Received]) AS [Count]
    FROM Table1

    WHERE (((Table1.[Date Received])>=[Select DateFrom] And (Table1.[Date Received])<=[Select Date to]));

    Based upon the above (and what you suggested), could you give me an idea as to completing the DLookUp statement?

    Thanks!!!

  6. #6
    hansendl is offline Advanced Hobbyist
    Windows 7 64bit Access 2010 32bit
    Join Date
    Sep 2012
    Posts
    38
    I just tested ranman256's suggestion in a test database and it works fine on my system. The ControlSource property of textbox80 should read: =Dlookup("[Count]", "qrycountBF")

    If that doesn't work, need to look at your SQL statement. Where do the values for the [Select DateFrom] and [Select Date to] fields come from? Are these fields in Table1, or are they parameters entered when you run the query?

  7. #7
    amc1902 is offline Novice
    Windows 7 64bit Access 2003
    Join Date
    Oct 2014
    Posts
    10
    Hi hansendl

    They are parameters entered when the query is run - I'll give your suggestion a try, thanks

  8. #8
    amc1902 is offline Novice
    Windows 7 64bit Access 2003
    Join Date
    Oct 2014
    Posts
    10
    Hi hansendl

    My apologies, I quoted the wrong query earlier. The query should read 'qrydaterec'. With this in mind, I've followed your suggestion and in the ControlSource property box of textbox80 I have added =DLookUp("[Count]","qrydaterec").

    It hasn't worked, however, interesting enough, the #Error now shows #Name? in textbox80

    What could that indicate?

  9. #9
    hansendl is offline Advanced Hobbyist
    Windows 7 64bit Access 2010 32bit
    Join Date
    Sep 2012
    Posts
    38
    Not sure where the #Name? error is coming from, but when I added a parameter to my saved query, it produces an error in the DLookUp function (i.e., "#Error" shows up in the textbox).

    I recommend you use VBA code. From a user interface perspective, I also recommend you add textboxes to your form for the user to specify the start and end dates for the query. If you name these textboxes "txtDateFrom" and "txtDateTo", your code would look something like this:

    Code:
    Private Function GetCount() As Long
        Dim lngReturn As Long
        Dim rst As DAO.Recordset
        Dim db As DAO.Database
        Dim strSQL As String
        
        strSQL = "SELECT Count(Table1.[Date Received]) AS [Count] " _
            & "FROM Table1 " _
            & "WHERE (((Table1.[Date Received])>=#" & Me.txtDateFrom _
            & "# AND (Table1.[Date Received])<=#" & Me.txtDateTo & "#));"
        Set db = CurrentDb
        Set rst = db.OpenRecordset(strSQL, dbOpenSnapshot)
        If Not rst.BOF Then
            GetCount = rst![Count]
        Else
            GetCount = 0
        End If
        
    End Function
    Then just set the ControlSource property of your textbox to: =GetCount()

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

Similar Threads

  1. Displaying a Query result in a text box
    By amc1902 in forum Queries
    Replies: 4
    Last Post: 10-07-2014, 11:42 AM
  2. Replies: 3
    Last Post: 01-14-2014, 03:56 PM
  3. Enter query result in a form textbox
    By Damo10 in forum Forms
    Replies: 2
    Last Post: 06-30-2013, 06:43 PM
  4. displaying query result as a subform
    By uzair_rosli in forum Access
    Replies: 4
    Last Post: 02-26-2013, 04:29 AM
  5. Replies: 1
    Last Post: 01-06-2013, 01:47 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