+ Reply to Thread
Results 1 to 20 of 20

Query Help

  1. #1
    Forum Contributor
    Join Date
    11-28-2011
    Location
    St. Louis, Missouri
    MS-Off Ver
    Excel 2007 & 2013
    Posts
    129

    Query Help

    Ok I do not know if my title is ok for what I am asking about. I am trying to take a list of mileages and mark them as a certain category. So here is some examples:

    These mileages
    35833
    39861
    35107
    59933
    48913
    20197

    Have to fall into these categories:
    24000
    36000
    50000
    60000

    So the mileages are each a separate record and in the column next to it I want to have one of the categories. So for the first one the record is 35833, the column next to it should show 36000.

    Let me know if this is possible or should be through a Query.
    Last edited by ncurran217; 02-13-2012 at 10:17 AM. Reason: Solved

  2. #2
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2405 Win 11 Home 64 Bit
    Posts
    23,888

    Re: Query Help

    Will there be only four categories?
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  3. #3
    Forum Contributor
    Join Date
    11-28-2011
    Location
    St. Louis, Missouri
    MS-Off Ver
    Excel 2007 & 2013
    Posts
    129

    Re: Query Help

    Well in the end there will be alot of different ones depending on brand and a type of that brand. If that makes sense

  4. #4
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2405 Win 11 Home 64 Bit
    Posts
    23,888

    Re: Query Help

    This could be done in a query, but would require nested iif statements. I think it would be better to do it in a form with a Select Case Statement in VBA.

    Here are links to both types of tutorials.

    Nested IIF http://office.microsoft.com/en-us/ac...001228853.aspx

    Select Case http://www.techonthenet.com/access/f...anced/case.php

  5. #5
    Forum Contributor
    Join Date
    11-28-2011
    Location
    St. Louis, Missouri
    MS-Off Ver
    Excel 2007 & 2013
    Posts
    129

    Re: Query Help

    Ok so want to make sure I can do this as well, And statements within either the IIF statements or the select case statement? So in excel it would look like this:

    IF(AND(A2="AADIAM",B2<24001),24000,IF(AND(A2="AADIAM",B2>24000,B2<36001),36000, and so on...

    Is using that and statement possible still in the IIF statements in access and or the select case statement?

  6. #6
    Forum Contributor
    Join Date
    11-28-2011
    Location
    St. Louis, Missouri
    MS-Off Ver
    Excel 2007 & 2013
    Posts
    129

    Re: Query Help

    Ok, so I got the Nested IIF statement to work. Found the EVAL formula.

  7. #7
    Forum Contributor
    Join Date
    11-28-2011
    Location
    St. Louis, Missouri
    MS-Off Ver
    Excel 2007 & 2013
    Posts
    129

    Re: Query Help

    Well my IIF statement is more than 1024 letters, so can the select case statement do the same Eval with AND statements?

    Yea I am lost with the select case statement.

    How do I reference the different columns? Let me know if you want some examples of what I would be doing with this. Thanks again for the help!
    Last edited by ncurran217; 02-10-2012 at 11:05 AM.

  8. #8
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2405 Win 11 Home 64 Bit
    Posts
    23,888

    Re: Query Help

    You have to use the select case in VBA. Which means you will need to use a form to get this information. This also then opens up the possibility of using IF-THEN--ELSE statements in VBA which resemble in concept the IIF statement, but has to be written in code. There is no limitation on length or number of IF statements in either of these.

    Just thought of another angle that you might be able to do in a query. Try using the Choose function. Here is a link on how it works.

    http://www.techonthenet.com/access/f...ced/choose.php

    This may be the easiest of all to do.

    Alan

  9. #9
    Forum Contributor
    Join Date
    11-28-2011
    Location
    St. Louis, Missouri
    MS-Off Ver
    Excel 2007 & 2013
    Posts
    129

    Re: Query Help

    I do not know if how that will work with what I am doing...here is the Excel version of what I am doing:

    I can do it either this way:

    IF(AND(A2="AADIAM",B2<24001),24000,IF(AND(A2="AADIAM",B2>24000,B2<36001),36000, and so on...

    or

    I have setup and INDEX and MATCH formula to look up off a list. Which that formula looks like this:

    {=INDEX(maxpurchmiles,MATCH(1,INDEX((pmcoverages=AI2)*(maxpurchmiles>=N2),0),0))}

    Let me know what you think!

  10. #10
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2405 Win 11 Home 64 Bit
    Posts
    23,888

    Re: Query Help

    I've built a solution for you. You will need to create a form with just a button on it. Then you will need to put this code in the on-click event for the button. It will then cycle through your table and add the category according to the first posting. You will need to add a field to your table. I used the term Category

    I've attached a database with all the information so you can see how it works.
    Attached Files Attached Files

  11. #11
    Forum Contributor
    Join Date
    11-28-2011
    Location
    St. Louis, Missouri
    MS-Off Ver
    Excel 2007 & 2013
    Posts
    129

    Re: Query Help

    Ok I see that, now what if there are the mileages between a certain point, but then there is a brand that is associated with as well. So here is what I guess I am trying to explain:

    CCPLAT will have the categories of 24000,36000,50000 & 60000
    CCDIAM will have the categories of 50000,75000,100000,125000, & 150000

    How in the case select statement make it look for the brand and the mileage category?

  12. #12
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2405 Win 11 Home 64 Bit
    Posts
    23,888

    Re: Query Help

    You would have to combine an IF-Then Statement with the Select Case, ie.
    Please Login or Register  to view this content.
    You can have as many of these as you like. It will take a little bit of work to set them up. I would do it with a test db first. Once you have it in place, then the code runs very quickly.

  13. #13
    Forum Contributor
    Join Date
    11-28-2011
    Location
    St. Louis, Missouri
    MS-Off Ver
    Excel 2007 & 2013
    Posts
    129

    Re: Query Help

    Ok I will try that, and I would have to use the button? Can't just have it in the query field or anything?

  14. #14
    Forum Contributor
    Join Date
    11-28-2011
    Location
    St. Louis, Missouri
    MS-Off Ver
    Excel 2007 & 2013
    Posts
    129

    Re: Query Help

    Here is what I have and it keeps saying, "Compile Error: Loop Without Do":

    Private Sub Command4_Click()
    Dim mydb As DAO.Database
    Dim myset As DAO.Recordset
    Dim mycategory As String

    Set mydb = CurrentDb
    Set myset = mydb.OpenRecordset("Table1")
    Do Until myset.EOF

    myset.Edit
    If Coverage = "CCPLAT" Then
    Select Case myset!Mileage
    Case 0 To 24000
    [mycategory] = "24,000"
    Case 24001 To 36000
    [mycategory] = "36,000"
    Case 36001 To 50000
    [mycategory] = "50,000"
    Case 50001 To 60000
    [mycategory] = "60,000"
    Case Else
    [mycategory] = "N/A"
    End Select
    Else
    If Coverage = "CCDIAM" Then
    Select Case myset!Mileage
    Case 0 To 50000
    [mycategory] = "50,000"
    Case 50001 To 75000
    [mycategory] = "75,000"
    Case 75001 To 100000
    [mycategory] = "100,000"
    Case 100001 To 125000
    [mycategory] = "125,000"
    Case 125001 To 150000
    [mycategory] = "150,000"
    Case Else
    [mycategory] = "N/A"
    End Select
    End If
    myset("Category").Value = mycategory
    myset.Update

    myset.MoveNext
    Loop
    End Sub

  15. #15
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2405 Win 11 Home 64 Bit
    Posts
    23,888

    Re: Query Help

    Code looks ok. Usually that error pops when there is no End If statement, but there is. So now, open your form in design view. Click on Alt +F11 to open the VBA window. Click on Tools. Now References. See if there are any references missing. It will tell you. If so, then add them and try again.

  16. #16
    Forum Contributor
    Join Date
    11-28-2011
    Location
    St. Louis, Missouri
    MS-Off Ver
    Excel 2007 & 2013
    Posts
    129

    Re: Query Help

    These are the ones that are checked:

    Visual Basic for Applications
    Microsoft Access 12.0 Object Library
    OLE Automation
    Microsoft ActiveX Data Objects 2.1 Library
    Microsoft DAO 3.6 Object Library

  17. #17
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2405 Win 11 Home 64 Bit
    Posts
    23,888

    Re: Query Help

    None indicated as missing?

  18. #18
    Forum Contributor
    Join Date
    11-28-2011
    Location
    St. Louis, Missouri
    MS-Off Ver
    Excel 2007 & 2013
    Posts
    129

    Re: Query Help

    nope nothin coming up saying anything is missing

  19. #19
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2405 Win 11 Home 64 Bit
    Posts
    23,888

    Re: Query Help

    A little bit of tweaking and I got it to work.

    Please Login or Register  to view this content.

  20. #20
    Forum Contributor
    Join Date
    11-28-2011
    Location
    St. Louis, Missouri
    MS-Off Ver
    Excel 2007 & 2013
    Posts
    129

    Re: Query Help

    Awesome that's great! Thanks for the help!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1