Consulting

Results 1 to 9 of 9

Thread: Help needed for creating Dynamic Oracle Query using Excel VBA and Forms

  1. #1

    Help needed for creating Dynamic Oracle Query using Excel VBA and Forms

    Presently,I am using the method : Data--> From other Sources --> From Data Connection Wizard --> Others/Advanced--> Microsoft Provider for Oracle to run a Query on Oracle Database and Storing the Data in the Excel Sheet.
    I am having a requirement to Use Forms to Get the Data from the users and want to run the Dynamic Oracle query and want to return the Data in the Form

    Ex: Select * from Employee_Database where Employee = 'Ravi' and Department = 'HR'

    In the above Query, I want to get the Employee name and Department from the users using Forms and want to run a query in Oracle database and return the value in Forms.

    Please let me know if this can be automated completely using VBA , Forms and Dynamic Oracle Queries. If so, Please assist me how to do it as i am new to VBA programming.

  2. #2
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    Obviously, you would build the SQL string from a TextBox control normally.
    e.g.
    Dim s as string
    s = "Select * from Employee_Database where Employee = '" & TextBox1.Value & "' and Department = '" & TextBox2.Value" & "'"
    and then use that in the query. Try recording what you want in an QueryTable and then modify it. Otherwise, an ADO method might work for you. See some of my recent posts for ADO examples using MySQL or see these ADO examples: http://www.erlandsendata.no/english/...php?t=envbadac

    The main part you will need if you don't use the recorded macro QueryTable method is the connection string. For that, see: http://www.connectionstrings.com/oracle/

  3. #3
    Thank you Kenneth for your quick response.

    I used Record macro to get the VBA code for connecting the Oracle Database and Got the VBA Code,

    But when i am running the recorded macro, it is giving error.

    From your link, i think the below connection string will suit,

    Provider=msdaora;Data Source=MyOracleDB;User Id=myUsername;Password=myPassword

    but my recorded macro is showing different,

    Source = _ oLEDB;Provider=msdaora.1;Data Source=MyOracleDB;User Id=myUsername;Password=myPassword

    Can you please help connecting to the Database thru VBA first, so i can make progress on this work?
    Last edited by vijaysram; 08-14-2013 at 07:03 PM. Reason: Spelling Mistake

  4. #4
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    From the link that I provided for the connection strings, and your recorded macro, this connection string might work.

    Provider=OraOLEDB.Oracle;Data Source=MyOracleDB;User Id=myUsername;
    Password=myPassword;

    See this thread for an example. http://www.vbaexpress.com/forum/show...sing-Excel-VBA

  5. #5

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    I would suggest that you create a parameterised stored procedure in the database, and then pass those form values to the SP call.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  7. #7
    Quote Originally Posted by Kenneth Hobs View Post
    From the link that I provided for the connection strings, and your recorded macro, this connection string might work.

    Provider=OraOLEDB.Oracle;Data Source=MyOracleDB;User Id=myUsername;
    Password=myPassword;

    See this thread for an example.
    Yes, I am now able to connect My Database.. I am now having the requirement to run a dynamic query.

    Ex : In my database , if i am running a query : Select * from Employee_database ; there is a Field and it is returning some value A = 200 or some A = some numeric value.

    I now need to get the input from the VBA form text box and need to run the dynamic oracle query

    i.e. Suppose if i am entering text box value =100 it has to run the query : Select Employee_name, Value from Employee_database where value = 'A= ???'

    Please advise further...

  8. #8
    Can you let me know how to do it ? I don't find the parameter option while i am pulling the data from my database manually... I am using MSExcel 2010

  9. #9
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    Without code, or the code in an example workbook, I don't know how we can help.

    I don't know what you mean by parameter. There are parameter queries in the database and ADO object and parameters for VBA routines.

    ADO would be the best route I suspect. The example links that I gave should suffice.

Tags for this Thread

Posting Permissions

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