Results 1 to 10 of 10
  1. #1
    Jaron is offline Advanced Beginner
    Windows 8 Access 2010 32bit
    Join Date
    Jun 2013
    Posts
    63

    using vba to write SQL result to an unbound textbox

    Hi. I have an unbound textbox on a form, and a combobox on the same form. I would like for any change in the combobox value to run an SQL statement and put the results in the textbox. I don't actually have a query, rather I am hoping to just run the SQL from within the VBA. Is this possible?



    Either way, I am getting Run-Timeerror 3061: Too few parameters. Expected 2.


    The SQL does work fine on it's own.

    Here is the code I have in the on change event of the combo:

    Code:
    Private Sub Combo27_Change()
    
    Dim db As Database
    Dim rs As Recordset
    Dim strSQL As String
        strSQL = "SELECT Sum([Amount]) AS TotalPaidtoDate FROM CreditorPayments;"
    Set db = CurrentDb
    Set rs = db.OpenRecordset(strSQL, dbOpenSnapshot)
    Me.Text33.Value = rs!TotalPaidtoDate
    End Sub
    The line that catches is:

    Code:
    Set rs = db.OpenRecordset(strSQL, dbOpenSnapshot)
    Any ideas?

    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,640
    Is CreditorPayments a query with 2 parameters by chance? You might try a DSum() if so.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    You cannot set the Control Source of a Textbox to a SQL statement; you need to save your SQL statement as a Query and then, as Paul suggested, use the appropriate Function against the Query.

    Linq ;0)>

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,640
    Quote Originally Posted by Missinglinq View Post
    You cannot set the Control Source of a Textbox to a SQL statement
    While true, not sure why you've jumped in to point it out. They didn't try to.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    Jaron is offline Advanced Beginner
    Windows 8 Access 2010 32bit
    Join Date
    Jun 2013
    Posts
    63
    Yes, CreditorPayments does have 2 parameters. It says: Look for all the payment records that have the same VendorID and CreditorAccountID as I've entered in two combo boxes in my form. The CreditorAccountID combo is supposed to have the on change event that says: When changed, run some SQL that pulls the sum of all entries from that query and then stick the result in this textbox to show me how much I've paid towards this account to date.

    The query and the table that the form is based on are unrelated, so I've had no success trying to create a separate query that binds them and then basing my form on that so that my textbox can be bound instead of unbound. So the only solution I could think of is to try to force it with VBA. I haven't ever used DSum before, I'll play around with that.

  6. #6
    Jaron is offline Advanced Beginner
    Windows 8 Access 2010 32bit
    Join Date
    Jun 2013
    Posts
    63
    Ok, DSum was very easy to use. I got the results I want with the following:

    Code:
    Dim SumPaid As Currency
    SumPaid = DSum("[Amount]", "CreditorPayments")
    Me.Text33.Value = SumPaid
    End Sub
    Thank you for that suggestion. I'm new as you can tell. Now I just have to address the 'invalid use of null' when I choose something in the combo that doesn't return results....probably an 'if' bit in the code. Thanks for the help.

  7. #7
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,640
    Happy to help. One alternative:

    SumPaid = Nz(DSum("[Amount]", "CreditorPayments"), 0)
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  8. #8
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    Quote Originally Posted by pbaldy View Post

    ...not sure why you've jumped in to point it out. They didn't try to...
    Let's see the thread title was

    "using vba to write SQL result to an unbound textbox"

    and then the OP wrote

    "run an SQL statement and put the results in the textbox"

  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,640
    Both can be interpreted differently, and the code clearly wasn't doing that.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  10. #10
    Jaron is offline Advanced Beginner
    Windows 8 Access 2010 32bit
    Join Date
    Jun 2013
    Posts
    63
    Nice. The Nz worked well. I forgot I'd used that before in a query expression awhile back, but I didn't think of it this time.

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

Similar Threads

  1. unbound textbox to bound textbox
    By thescottsman92 in forum Access
    Replies: 3
    Last Post: 08-29-2013, 02:02 AM
  2. Replies: 4
    Last Post: 05-31-2013, 07:00 PM
  3. Replies: 8
    Last Post: 04-12-2013, 08:59 PM
  4. Unbound, form textbox populated
    By WiReLaD in forum Access
    Replies: 7
    Last Post: 11-19-2012, 12:54 PM
  5. Undo Typing in Unbound Textbox
    By June7 in forum Programming
    Replies: 4
    Last Post: 08-29-2012, 12:14 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