Page 1 of 3 123 LastLast
Results 1 to 15 of 42
  1. #1
    geniass is offline Novice
    Windows 7 Access 2007
    Join Date
    Aug 2010
    Posts
    20

    VBA problem in query

    Hi,
    I have written this VBA module for a school project in Access 2007, and it works fine in the Immediate Window in the Visual Basic Editor. However when I use it in a query (SQL) the value just doesn't show up. I have no idea why.
    Here is the module code:
    Code:
    Option Compare Database
    
    Function LoopIngredients(itemName As String) As Long
    Dim strSQL As String
    Dim rst As Recordset
    Dim field As field
    Dim temp As Boolean
    strSQL = "Select Table2.[Ingredient], Table2.[Price] From Table2"
    
    'open the results read-only
    Set rst = CurrentDb.OpenRecordset(strSQL, dbOpenForwardOnly)
    If rst.RecordCount > 0 Then
    'rst.MoveFirst
    Do While Not rst.EOF
    For Each field In rst.Fields
        If field.Name = "Ingredient" Then
            temp = False
            If InStr(itemName, field.Value) Then
                temp = True
                Debug.Print ("Name " & field.Value)
            End If
        End If
        If field.Name = "Price" Then
            If temp Then
                LoopIngredients = LoopIngredients + field.Value
                Debug.Print ("Price " & LoopIngredients)
            End If
        End If
    Next field
    rst.MoveNext
    Loop
    End If
    End Function
    And here is the output in the immediate window:
    Code:
    ?LoopIngredients("Cheese and Tomato Sandwich")
    Name Cheese
    Price 1
    Name Tomato
    Price 3
     3
    Here is the SQL query:
    Code:
    SELECT Table1.ID, Table1.[Item Name], LoopIngredients("Cheese and Tomato Sandwich") AS Price, Table2.ID, Table2.Ingredient, Table2.Price
    FROM Table1, Table2;
    Any help would be appreciated.

  2. #2
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 Access 2007
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922

  3. #3
    geniass is offline Novice
    Windows 7 Access 2007
    Join Date
    Aug 2010
    Posts
    20
    I was trying to work out why the function works fine in in the debugging area, but as soon as i use it in a query is doesn't return anything at all, not even 0.

  4. #4
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 Access 2007
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Is it in a Standard Module rather than in a form module?

  5. #5
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 Access 2007
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Does the immediate window still show the Debug.Print's?

  6. #6
    ajetrumpet is offline VIP
    Windows Vista Access 2007
    Join Date
    Mar 2010
    Location
    N/A
    Posts
    2,694
    Quote Originally Posted by geniass View Post
    Here is the SQL query:
    Code:
    SELECT Table1.ID, Table1.[Item Name], LoopIngredients("Cheese and Tomato Sandwich") AS Price, Table2.ID, Table2.Ingredient, Table2.Price
    FROM Table1, Table2;
    Any help would be appreciated.
    Does it have anything to do with pulling values from 2 dif tables that are not joined? in the actual function your sql string only include table 2. It's been a while since I've messed with this idea of pulling from two dif unrelated tables, but could it be that issue I wonder?

    Or maybe also, something that has to do with opening table2 in memory once it is already opened by the query?

  7. #7
    geniass is offline Novice
    Windows 7 Access 2007
    Join Date
    Aug 2010
    Posts
    20
    Here's a screenshot of the VBA editor:

    I think it is a regular module.
    I really have no idea what is going on because it debugs fine.

  8. #8
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 Access 2007
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    OK, your code is in a Standard Module and Public since that is the default. What happens when you run the query? Have you looked at the Immediate window *after* running the query to see if the Debug.Print statements executed? Have you set a break point to see what is being returned?

  9. #9
    ajetrumpet is offline VIP
    Windows Vista Access 2007
    Join Date
    Mar 2010
    Location
    N/A
    Posts
    2,694
    gen,

    Ruralguy is probably the one to ask about all this, but I just wonder what your tables are like. Relationships between 1 and 2? On ID perhaps? what do you want out of this situation? Example - if you want the total price of each product listed in table A and you've got 1 and 2 related one to many on the ID field, the DSUM() function is the solution (no function required).

  10. #10
    geniass is offline Novice
    Windows 7 Access 2007
    Join Date
    Aug 2010
    Posts
    20
    Ok, basically, I have 2 tables.
    The first table has the fields: ID, Item Name and Cost Price.
    The second table has the following fields:ID, Ingredient and Cost Price.
    Now basically what this query must do is, for each item in table 1, check if this item contains each ingredient in table 2. If it does contain this ingredient, add its cost price to the items total price. Eg. Cheese and tomato sandwich, Does it contain cheese? Yes, add 1.50. Does it contain lettuce? No. Does it contain Tomato? Yes Add 2.00.

    Also what exactly does joining tables do?
    Thanks

  11. #11
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 Access 2007
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Did you try anything I suggested in Post #8?

  12. #12
    geniass is offline Novice
    Windows 7 Access 2007
    Join Date
    Aug 2010
    Posts
    20
    How do I check the immediate window while running a query?
    Thanks

  13. #13
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 Access 2007
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    *After* you run the query use ^G to open the Immediate window and see what is there.

  14. #14
    ajetrumpet is offline VIP
    Windows Vista Access 2007
    Join Date
    Mar 2010
    Location
    N/A
    Posts
    2,694
    Quote Originally Posted by geniass View Post
    Now basically what this query must do is, for each item in table 1, check if this item contains each ingredient in table 2.
    That's great, but HOW do you check? does the ID in table 2 match one of those in table1? These are the types of questions you need to ask. For example, if IDs in table2 are arbitrary and mean nothing except to identify the record numbers in that table, there is no way to know which items have what ingredients because there's no connection between the two tables' data at all. Does that make sense?

    Quote Originally Posted by geniass View Post
    Also what exactly does joining tables do?
    Thanks
    joining tables happen automatically when queries are built through the interface and not code. One to many relationships between tables are the most common situations that result in joins. To know more, type in JOIN and read the help articles on the different types.


    This might not be best, but the way I see your tables set up is like this:

    table1
    1 - ID (auto)
    2 - item name
    3 - price (if this is different than all included ingredient prices combined to equal this price)

    table2
    1 - ID (matches IDs in 1, meant to relate ingredients to what item they make up
    2 - ingredient
    3 - price

    If you had this setup, the query would be as simple as:
    Code:
    SELECT ID, [ITEM NAME],
    
    DSUM("price", "table2", "[ID] = " & [ID]) AS TotalPrice
    or ... if the price you want equals the item price + all the ingredient prices related to that item, it's a little different:
    Code:
    SELECT ID, [ITEM NAME], 
    ([PRICE] + DSUM("price", "table2", "[ID] = " & [ID])) AS TotalPrice
    Remember, I'm not this great expert but please also remember to give as much (relevant) information as you can when you post. If you know what is relevant that is (some people don't)

  15. #15
    geniass is offline Novice
    Windows 7 Access 2007
    Join Date
    Aug 2010
    Posts
    20
    You mean Ctrl G right? I pressed that and it just beeps.

Page 1 of 3 123 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. query problem i have a problem wi
    By maxx3 in forum Queries
    Replies: 0
    Last Post: 06-29-2009, 02:29 AM
  2. Problem with query
    By maxx3 in forum Queries
    Replies: 8
    Last Post: 06-08-2009, 11:58 AM
  3. query problem
    By maxx3 in forum Queries
    Replies: 35
    Last Post: 05-29-2009, 04:07 PM
  4. Top 25 query problem
    By tmcrouse in forum Queries
    Replies: 3
    Last Post: 05-07-2009, 09:02 AM
  5. Problem in Query
    By Bruno Trindade in forum Queries
    Replies: 4
    Last Post: 03-28-2009, 04:10 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