+ Reply to Thread
Results 1 to 5 of 5

Data Connections: Parameterized Query of Access DB... Is it possible?

Hybrid View

  1. #1
    Registered User
    Join Date
    06-13-2012
    Location
    Hamsterdam
    MS-Off Ver
    Excel 2010
    Posts
    17

    Question Data Connections: Parameterized Query of Access DB... Is it possible?

    Hello forum, I've been Googling and searching for this answer. I have a data connection to an access database and I wrote a simple SQL query in the SQL field.

    select * from games where Title = "Game 1"
    Is it possible to parameterize that query? Ideally, I'd like it to reference a cell on an Excel sheet where I can use a drop down to change the game title.

    Thanks in advance!

  2. #2
    Forum Contributor tkowal's Avatar
    Join Date
    11-20-2010
    Location
    Miami, Fl
    MS-Off Ver
    Excel 2010
    Posts
    150

    Re: Data Connections: Parameterized Query of Access DB... Is it possible?

    It is not that straight forward..... excel doe not naturally accept parameterized queries. Using VBA coding is the most straight forward way of passing parameters but you will have to manage the connections to the database from code. However, it appears that you want a non-code way of doing it. This is not so straight foward.

    Basically, when connection to your database using the Data Tab choose "From Other Sources." Then choose From Microsoft Query. You will have to create a Parameterize Query from there to your data source.... Then in excel go to Data choose connections then choose properties then click on the Definition Tab. If you have set up a proper Parameter Query the Parameters button on the bottom will be highlighted choose that and then you can reference the parameters to Excel Cells .....

    Here is a reference link and solutions with same issue as you:
    http://www.pcreview.co.uk/forums/exc...-t3519096.html
    Last edited by tkowal; 06-19-2012 at 12:49 PM.
    Ted
    "Live Long and Prosper"

  3. #3
    Registered User
    Join Date
    06-13-2012
    Location
    Hamsterdam
    MS-Off Ver
    Excel 2010
    Posts
    17

    Re: Data Connections: Parameterized Query of Access DB... Is it possible?

    Thanks tk, I have dived into the VBA and it doesn't seem too daunting.

    I created a dropdown and populated it using the Workbook_Open() function.

    Then I used the ComboBox1_Change() function to fire off the code to change the SQL call. So here is where I am: I want to know if I can streamline my code any further. I generally like to code in the most elegant and minimal way possible. What are the minimum requirements? I used the "Record Macro" function to give me a baseline, but I feel there is a lot of extraneous code. Let me know what you think I can cut out:

     Dim SQL_Code As String
        SQL_Code = "SELECT * from table"
        With ActiveWorkbook.Connections("The Original Existing Connection to the DB").OLEDBConnection
            .BackgroundQuery = False
            .CommandText = SQL_Code
            .CommandType = xlCmdSql
            .Connection = "OLEDB;Provider=Microsoft.ACE.OLEDB.12.0;User ID=Admin;Data Source=G:\My Database.accdb;Mode=ReadWrite;Extended Properties="""";Jet OLEDB:System database="""";Jet OLEDB:Registry Path="""";Jet OLEDB:Engine Type=6;Jet OLEDB:Database Locking Mode=1;Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Global Bulk Transactions=1;Jet OLEDB:New Database Password="""";Jet OLEDB:Create System Database=False;Jet OLEDB:Encrypt Database=False;Jet OLEDB:Don't Copy Locale on Compact=False;Jet OLEDB:Compact Without Replica Repair=False;Jet OLEDB:SFP=False;Jet OLEDB:Support Complex Data=False;Jet OLEDB:Bypass UserInfo Validation=False"
            .RefreshOnFileOpen = False
            .SavePassword = False
            .SourceConnectionFile = ""
            .ServerCredentialsMethod = xlCredentialsMethodIntegrated
            .AlwaysUseConnectionFile = False
        End With
        With ActiveWorkbook.Connections("The Original Existing Connection to the DB")
            .Name = "The Original Existing Connection to the DB"
            .Description = ""
        End With
        ActiveWorkbook.Connections("The Original Existing Connection to the DB").Refresh
        
        
        Dim Sign_SQL As String
        Sign_SQL = "select * from table2"
        With ActiveWorkbook.Connections("A second connection to a different DB").OLEDBConnection
            .BackgroundQuery = False
            .CommandText = Sign_SQL
            .CommandType = xlCmdSql
            .Connection = "OLEDB;Provider=Microsoft.ACE.OLEDB.12.0;User ID=Admin;Data Source=G:\Another DB.accdb;Mode=ReadWrite;Extended Properties="""";Jet OLEDB:System database="""";Jet OLEDB:Registry Path="""";Jet OLEDB:Engine Type=6;Jet OLEDB:Database Locking Mode=1;Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Global Bulk Transactions=1;Jet OLEDB:New Database Password="""";Jet OLEDB:Create System Database=False;Jet OLEDB:Encrypt Database=False;Jet OLEDB:Don't Copy Locale on Compact=False;Jet OLEDB:Compact Without Replica Repair=False;Jet OLEDB:SFP=False;Jet OLEDB:Support Complex Data=False;Jet OLEDB:Bypass UserInfo Validation=False"
            .RefreshOnFileOpen = False
            .SavePassword = False
            .SourceConnectionFile = ""
            .ServerCredentialsMethod = xlCredentialsMethodIntegrated
            .AlwaysUseConnectionFile = False
        End With
        With ActiveWorkbook.Connections("A second connection to a different DB")
            .Name = "A second connection to a different DB"
            .Description = ""
        End With
        ActiveWorkbook.Connections("A second connection to a different DB").Refresh
    It just seems like lots of that stuff is unnecessary. I would assume I just need the new SQL query, the DB connection and a refresh command.

    Can I do this without having an initial DB connection? Thanks!
    Last edited by twointum; 06-26-2012 at 12:57 PM.

  4. #4
    Forum Contributor tkowal's Avatar
    Join Date
    11-20-2010
    Location
    Miami, Fl
    MS-Off Ver
    Excel 2010
    Posts
    150

    Re: Data Connections: Parameterized Query of Access DB... Is it possible?

    There are lots of ways of connecting to databases through vba each has their own requirements... say for example you want to connect through ADODB.... Make sure the VBA IDE has the Microsoft ActiveX Data Objects 2.x Library checked under references under the Tools menu....

    Below is a slim'd down version of the connection and queries assuming you need two queries open at the same time.... The connection is made when you need it and closed when your finished withit


    'Make sure the Tools/References  Microsoft ActiveX Data Objects 2.X is checked
    'Or Modify code below if you are using something other than an ADO connection
    Dim sQL1 As String, sQL2 As String
    Dim cn As ADODB.Connection
    Dim rs1 As ADODB.Recordset, rs2 As ADODB.Recordset
    
    Set cn = New ADODB.Connection
    cn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=.\Test.accdb;Persist Security Info=False"
    
    sQL1 = "Select * from Table1;"
    sQL2 = "Select * from Table2;"
    
    Set rs1 = New ADODB.Recordset
    Set rs2 = New ADODB.Recordset
    
    rs1.Open sQL1, cn
    rs2.Open sQL2, cn
    
    Range("A10").CopyFromRecordset rs1
    Range("D10").CopyFromRecordset rs2
    
    rs1.Close
    rs2.Close
    Set rs1 = Nothing
    Set rs2 = Nothing
    cn.Close
    Set cn = Nothing
    above code was tested! Note: You will be unable to use the built in Data Refresh commands in Excel. You will have to manage the refreshing of data your self through code...
    Last edited by tkowal; 06-26-2012 at 02:16 PM.

  5. #5
    Registered User
    Join Date
    06-13-2012
    Location
    Hamsterdam
    MS-Off Ver
    Excel 2010
    Posts
    17

    Re: Data Connections: Parameterized Query of Access DB... Is it possible?

    I think you went in a direction that I'm not familiar with. I am a PHP master, but VBA noob. When you went the recordset way, you lost me.

    So basically, if anyone else runs along this, I pared this down to the bare essentials for me.

    So if you have an existing data connection that populates a pivot table and want to make it dynamic based on a drop down, you can do this:

    Dim Choice as String
    Choice = Sheets("Sheet1").ComboBox1.Text
    
    Dim SQL_Code as String
    SQL_Code = "select * from table where choice = """ & Choice & """
    
    With ActiveWorkbook.Connections("Pre-Existing Data Connection").OLEDBConnection
            .CommandText = SQL_Code
    End With
    ActiveWorkbook.Connections("Pre-Existing Data Connection").Refresh
    So basically, you need to only use one attribute/property on the with command. 10 lines of code. I think this works for me. Thanks for the brainstorming tkowal!

+ 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