Results 1 to 8 of 8
  1. #1
    kleaverjr is offline Advanced Beginner
    Windows 7 64bit Access 2013 64bit
    Join Date
    Apr 2022
    Posts
    53

    Having issue with Access 2013 VBA Code SELECT-WHERE

    I am just become comfortable programming with VBA in Access 2013 (I'm used to using MYSQL and VB6 when I used to write programs for work 20 years ago!). So I am am an experienced programmer but by no means am I an expert at it, and I have run into a problem I can't seem to find a solution for, so I understand Office 2013 is about to be no longer supported, but since this program is going on a computer that is isolated from any exterior threat (i.e. not connected to the Internet) and I don't wish to spend more money on software that does the job. VBA for Access 2013 is designed to do what I am asking it to do, I just can't figure out why it's not working. It's a simple SQL SELECT Statement with a few conditions, that when I look at the Table, the record matches, but when I run it, there are no records being found. Here are the specifics:

    I have a table "Inventory_New" that has a field "Sold_Location" that is blank until the item is Sold at a show or online and the field is updated to "Online" or "Show". Since there can be multiple quantities of the same Item SKU (Field "Item_SKU") I have the Select statement setup as follows:

    TableFindItem = "Inventory_New"
    rcdFindItemSKU = The SKU it is searching for

    "SELECT * FROM " + TableFindItem + " WHERE Item_SKU = '" + rcdFindItemSKU + "' AND LEFT(Sold_Location, 1) <> 'S'" ' AND LEFT(Sold_Location, 1) <> 'O' ORDER BY Vendor_Purchase_Date ASC"

    So it is selecting from the table, where the SKU matches the search SKU AND it is searching for all records that do not have an "S" or "O" as the first character in Sold Location. When I did a simple where Sold_Location = '', sometimes I would get an error that the field was null and would crash the program. Is there any other code that can be used to select the records that match the searchSKU and has no entry in the Sold_Location field?

    I have spent hours on trying to figure out why this is happening, and have yet to find an answer.

    Thanks for any help that can be provided!



    Ken L

  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,570
    Use Nz() ' extra text as message too short
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  3. #3
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,425
    I recommend you use the query design tool and copy/paste into vba if you want to build sql in code. Pretty sure you didn't, otherwise you wouldn't have plus signs in your sql like that. & will only concatenate strings, but + is likely to perform conversions and attempt sums when it can. Or swap your + with & and see if you're OK after that. When troubleshooting vba sql, always a good idea to debug.print and review in the immediate window, and if necessary, copy/paste to a new query and switch to datasheet view to test.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  4. #4
    kleaverjr is offline Advanced Beginner
    Windows 7 64bit Access 2013 64bit
    Join Date
    Apr 2022
    Posts
    53
    I will try that...

    I never took classes to program VB or VBA, I learned on my own, so that shows. I always used "+" signs in my SELECT strings, and up until now they always worked. Maybe that is contributing to the issue.

    Thanks.

    Ken L.

  5. #5
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,425
    Maybe this will help explain the danger of using an arithmetic operator for concatenation (in Access).
    https://docs.microsoft.com/en-us/off.../plus-operator
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  6. #6
    kleaverjr is offline Advanced Beginner
    Windows 7 64bit Access 2013 64bit
    Join Date
    Apr 2022
    Posts
    53
    Thank you for teaching me something new.

    Thanks all for your contributions to the solution. Things seem to be working now.

    Ken L

  7. #7
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,772
    & character is preferred for string concatenation. The 'S'" ' appears wrong.

    Consider:

    "SELECT * FROM " & TableFindItem & " WHERE Item_SKU = '" & rcdFindItemSKU & "' AND LEFT(Sold_Location, 1) <> 'S' AND LEFT(Sold_Location, 1) <> 'O' ORDER BY Vendor_Purchase_Date ASC"

    or

    "SELECT * FROM " & TableFindItem & " WHERE Item_SKU = '" & rcdFindItemSKU & "' AND NOT Left(Sold_Location, 1) IN ('S', 'O') ORDER BY Vendor_Purchase_Date ASC"

    or

    "SELECT * FROM " & TableFindItem & " WHERE Item_SKU = '" & rcdFindItemSKU & " AND Sold_Location LIKE ""[!'S', 'O']*"" ORDER BY Vendor_Purchase_Date ASC"

    + character can be useful for some concatenation. Keep in mind: Anything + Null returns Null. Anything & Null returns Anything. So say you have fields for Employee and Spouse and want to concatenate. But what if Spouse is Null? Employee & " and " & Spouse could return: John Jones and; whereas Employee & " and " + Spouse will return: John Jones


    Why is the table name dynamic? Why would there be multiple inventory tables?





    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  8. #8
    kleaverjr is offline Advanced Beginner
    Windows 7 64bit Access 2013 64bit
    Join Date
    Apr 2022
    Posts
    53
    There are two types of Inventory and two totally different types of inventory "Collections" and "New", each requiring their own Table Design. I found it was easier for several reasons to keep the two in separate tables. There are enough in common where I can use the same SELECT statement, as each of them have a "SKU", "Sold Location" etc fields. Not sure if that makes sense or not, but so far it is working. And the recommendations made fixed the problem. Your suggestions for other SELECT statements will come in handy as I can see other situations where that specific Syntax would be used.

    Ken L

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

Similar Threads

  1. MS Access 2013 Select table to delete from
    By morrist1 in forum Access
    Replies: 2
    Last Post: 09-13-2016, 07:31 AM
  2. Network Access Issue with Microsft Access 2013
    By dferreira in forum Access
    Replies: 6
    Last Post: 07-21-2016, 11:41 AM
  3. Replies: 2
    Last Post: 10-31-2014, 06:24 AM
  4. Replies: 0
    Last Post: 10-11-2013, 12:51 PM
  5. Access 2013 Display Issue?
    By Alan S in forum Access
    Replies: 2
    Last Post: 05-26-2013, 03:21 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