Results 1 to 12 of 12
  1. #1
    hjnash is offline Advanced Beginner
    Windows 7 64bit Access 2013
    Join Date
    Mar 2013
    Posts
    34

    How to use the form combo# in a VBA table query?

    I am experimenting with VBA doing calculations of a filed in my database table. I have a string that is working


    but the last element in the command is what is being searched for (hard coded). Instead of doing this I want to
    use the value after I select from my combo box the selected value so I use the build in comvo21 like a variable.
    So If I select medical from my combobox "Combo21" and I have selected from the event triger for that combobox
    after update I can do MsgBox Combo21 and the selected value from the combobox will be displayed in a message box
    I need to use combo21 variable in my string I can't figure out how to change my command.
    How do I replace Medical with combo21 variable? Disregard the [ ] below

    [Private Sub Combo21_AfterUpdate()]
    [Dim stotal As Currency]
    [stotal = DSum("Spending", "Spending", "Category = 'Medical'")]
    [MsgBox stotal]




    [End Sub

  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
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    hjnash is offline Advanced Beginner
    Windows 7 64bit Access 2013
    Join Date
    Mar 2013
    Posts
    34
    Thank you for the link I have some success; but, an error is generated if the database table has no entry for the selection.
    The error photo below was generated because I selected in my form combobox21 medical as the spending category, in my
    table there are no entries yet for medical so I get and error.

    [ Private Sub Combo21_AfterUpdate()][Dim stotal As Currency]
    ['stotal = DSum("Spending", "Spending", "Category = 'Medical'")]
    [stotal = DSum("Spending", "Spending", "Category ='" & Forms!SpendingTest!Combo21 & "'")]
    [MsgBox stotal]




    [End Sub]
    Attached Thumbnails Attached Thumbnails error3.jpg  

  4. #4
    Bradonnmorgan is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2013
    Location
    Ohio
    Posts
    13
    HjNash,

    How are you today? Sorry to hear about your issue. May I sujest trying:

    [ Private Sub Combo21_AfterUpdate()][Dim stotal As Currency]
    ['stotal = DSum("Spending", "Spending", "Category = 'Medical'")]
    NZ([stotal = DSum("Spending", "Spending", "Category ='" & Forms!SpendingTest!Combo21 & "'")])
    [MsgBox stotal]

    The Nz function just makes Null a 0. Not 100% sure if that will give you the same end result or not but it should ^_^ hope this helps.

    Regards,

    Bradon Morgan

  5. #5
    hjnash is offline Advanced Beginner
    Windows 7 64bit Access 2013
    Join Date
    Mar 2013
    Posts
    34

    Using the Nz I get error now see photo, also showing the code change

    Thanks for your help I have looked into the Nz and I think it should work BUT!
    I tried the Nz function But I always get an error now, perhaps I am not ding it correctly! Please see image below
    it shows the error and the code change.
    Attached Thumbnails Attached Thumbnails Nzerror.jpg  

  6. #6
    Bradonnmorgan is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2013
    Location
    Ohio
    Posts
    13
    hjnash,

    Would you mind uploading a copy of your database as long as it contains no confidential information so that I may mess with the code without having to competly recreate the database?
    I feel that this my be something as little as removing the [ ] from your last line of code.

    Thanks,

    Bradon Morgan

  7. #7
    Bradonnmorgan is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2013
    Location
    Ohio
    Posts
    13
    hjnash,

    Here is the proper syntax. very sorry I should have tested the code before I posted it.

    Dim stotal As Currency
    'stotal = DSum("Spending", "Spending", "Category = 'Medical'")]
    Nz (stotal = DSum("Spending", "Spending", "Category ='" & Forms!SpendingTest!Combo21 & "'"))
    MsgBox (stotal)



    Regards,

    Bradon Morgan

  8. #8
    hjnash is offline Advanced Beginner
    Windows 7 64bit Access 2013
    Join Date
    Mar 2013
    Posts
    34
    Thanks for the code update. The good news is that this somewhat works; that is, If I am
    entering the first record for category medical now I do get back a 0 and I can continue working.
    The bad news is that if I enter a record for groceries where I have a lot of records I still get a 0
    not the amount of money spent under groceries. I looked this up but did not see anything wrong
    so I am overlooking something. If you have some time I still would like to finish this.
    Thanks Harry Nash

  9. #9
    Bradonnmorgan is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2013
    Location
    Ohio
    Posts
    13
    Harry,

    I would love to continue assisting you with this. If you could upload a copy of your database I will take a look and see if there isnt anything that sticks out to me.\
    Two is always better than one ^_^

    Regards,

    Bradon Morgan

  10. #10
    hjnash is offline Advanced Beginner
    Windows 7 64bit Access 2013
    Join Date
    Mar 2013
    Posts
    34

    Attached is a MDB version of my database, again thanks for your help

    I saved my access 2013 file as a 2003 MDB I hope you can open it OK.
    I am also searching for some idea what I can do.
    Attached Files Attached Files

  11. #11
    Bradonnmorgan is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2013
    Location
    Ohio
    Posts
    13
    Harry,

    Sorry for the delay very busy yesterday and today. I will try and get back with you on a fix around 5pmest.


    Thanks,

    Bradon Morgan

  12. #12
    hjnash is offline Advanced Beginner
    Windows 7 64bit Access 2013
    Join Date
    Mar 2013
    Posts
    34
    Thank you so much, I am also trying to learn more about this command and how it may be failing.
    Thanks Harry Nash

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

Similar Threads

  1. Replies: 25
    Last Post: 03-19-2013, 10:08 PM
  2. Combo box to Select Table to Query
    By RedWolf3x in forum Programming
    Replies: 3
    Last Post: 11-15-2012, 04:31 PM
  3. Replies: 7
    Last Post: 09-21-2012, 11:09 AM
  4. Please Help, Combo Box Query Form Table Issue
    By Keeyter in forum Programming
    Replies: 9
    Last Post: 04-29-2010, 09:15 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