+ Reply to Thread
Results 1 to 8 of 8

User-defined variables in MS access query

  1. #1
    Registered User
    Join Date
    07-12-2007
    Posts
    16

    User-defined variables in MS access query

    OK I thought I had this down, but I guess it's not working...

    I'm trying to run an access query from excel (the access query queries an Oracle ODBC). I know, I should query that ODBC directly, and that may be an upgrade in the future.

    Anyway, I have a UserForm with 3 text boxes prompting the user for Project Number, Fiscal Year, and Accounting Period.

    I then set those text boxes equal to 3 variables:
    Please Login or Register  to view this content.
    And then I recorded the code for the MS Access query and inserted the variables where I need them to be (VARIABLES IN BOLD):

    Please Login or Register  to view this content.

    Result:
    When I run this query with numerical information in place of the variables, it works great... but when I run it with the variables in the code, it gives me a "general ODBC error" and highlights the " .Refresh BackgroundQuery:=False" line... that is telling me that something is wrong with my query, but I don't understand why variables wouldn't work for parameters in the query.

    Any idea how to get the user-defined variables to work as parameters in that query?

    Many thanks in advance.

  2. #2
    Forum Contributor Tarball's Avatar
    Join Date
    04-21-2007
    Location
    Atlanta, GA
    Posts
    166
    One issue that I see is that your variables are inside of the quoted string effectively making them text (i.e. they do not resolve to the value they point to).

    Partial code
    Please Login or Register  to view this content.
    should have the variables outside of the quotes using the & operator to concatenate the full string.

    Please Login or Register  to view this content.
    Reach me at excel_help at bellsouth dot net

  3. #3
    Registered User
    Join Date
    07-12-2007
    Posts
    16
    Ahhh I did not even see that it was a concatenation issue.

    Makes sense, but now it's giving me a SQL Syntax Error, and highlighting the ".Refresh Backgroundquery=False"
    Last edited by ratzy; 11-07-2007 at 03:27 PM.

  4. #4
    Registered User
    Join Date
    07-12-2007
    Posts
    16
    OK... so I got the query to work without error, however I had to sacrifice my "String" variable and trade it out for a "Long" variable. My issue is that now, with only the number sequence and not the letter sequence of the project number, the search is not specific enough and I am getting information for other projects which contain the same set of numbers in the project number.

    BTW, the fix was with concatenation; as an example...

    Please Login or Register  to view this content.

  5. #5
    Forum Contributor Tarball's Avatar
    Join Date
    04-21-2007
    Location
    Atlanta, GA
    Posts
    166
    Can you post the full query? Include the block of code before and after it. In MS-Access you can set a variable equal to the SQL String and then pass that SQL string over the connection to the database to return a result set using ADO. You might try this.

  6. #6
    Registered User
    Join Date
    07-12-2007
    Posts
    16
    I'm afraid I only have a couple of educational years of VB coding experience (1st year of college as IT major, then switched to finance). I am not familiar with SQL.

    I will post the full code, but first I wanted to explain my new idea:
    I made a combination box with 3 operators that the user can choose to query for accounting periods (=, <, >), so I am trying to concatenate this operator in my search criteria. I think the problem, again, is that for some reason this query does not like to use Strings in its query criteria. Hence the reason I had to remove the letter from the "ProjectNo" to fix my previous issue.

    P.S. I've switched from querying access to querying our Oracle ODBC directly (the Access query was merely a middle-man, if you will).

    Here's the full query code:
    Please Login or Register  to view this content.
    Please Login or Register  to view this content.
    Last edited by ratzy; 11-08-2007 at 10:34 AM.

  7. #7
    Registered User
    Join Date
    07-12-2007
    Posts
    16
    Gotta bump this.
    I'm so close!

    Here's a snippet (the important stuff) of the VB criteria that I'm trying to implement into my ODBC query:

    Please Login or Register  to view this content.
    ...
    Please Login or Register  to view this content.
    ...
    Please Login or Register  to view this content.
    It's giving me a General ODBC error. that tells me that it doesn't like my "Operator" statement, because without that variable and with a plain "=" the query runs perfectly.

    P.S the query code that IS working is:
    Please Login or Register  to view this content.
    Last edited by ratzy; 11-09-2007 at 09:56 AM.

  8. #8
    Forum Contributor Tarball's Avatar
    Join Date
    04-21-2007
    Location
    Atlanta, GA
    Posts
    166
    Make sure you are referencing the combo box correctly by referencing the sheet where it exists. You can do this by using the "Me" operator if the code you are executing is behind the sheet where the combo box lives, otherwise use the standard worksheet referencing.

    Please Login or Register  to view this content.
    Here is the revised code. You dont need to place the operator inside of the tick marks. Also, watch your "spaces"; make sure you have a space in between your column and the operator otherwise you will be passing a string like COLUMNNAMELIKE which is probably not what you want. I inserted a space after PERIOD.

    Also, instead of dealing with quoting rules, alot of times it is easier to just use the ASCII equivalent. Chr(39) resolves to a tick mark: '

    Another area that may give you problems is the LIKE operator. You will want to use a different string because you will need to pass Oracles wildcard string %

    Please Login or Register  to view this content.

+ 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