Results 1 to 17 of 17

Thread: [RESOLVED]create a prompt in Excel and run a sql server query

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2002
    Posts
    150

    [RESOLVED]create a prompt in Excel and run a sql server query

    hi,
    I usually run a query against sql server by going to Get external Data --> run database query.

    can someone instruct me to how I can embbed a prompt in excel so when someone opens the excel spreadsheet they would get the prompts with paramters that they want to pass to the query?

    the prompts must be a dropdown list populated from query then the final step is running last query with the three parameters they selected.

    thank you very much for any help.
    waely
    Last edited by waely; Jun 22nd, 2006 at 01:41 PM.

  2. #2
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: create a prompt in Excel and run a sql server query

    in the code window:
    In "ThisWorkbook"

    VB Code:
    1. Private Sub Workbook_Open()
    2.     Project_ID = InputBox("Please enter the Project ID", "Project ID")
    3.     Status = InputBox("Enter the STATUS for Project ID:" & Project_ID, "Status?")
    4.     Manager = InputBox("Enter the MANAGERS name:", "Manager?")
    5.    
    6. End Sub

    then just pass the three variable into your query
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Nov 2002
    Posts
    150

    Re: create a prompt in Excel and run a sql server query

    thanks Static. how can I prepopulate three drop down list with these values? it would nice to dynamic but it doesn't have to. so three dropdown list with my values and after selecting then the parameters will be passed to my query.

    thank you

  4. #4
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: create a prompt in Excel and run a sql server query

    how about... in the Open event...
    Create an ADO connection to your DB and run a query
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  5. #5
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: create a prompt in Excel and run a sql server query

    here is a code sample:

    VB Code:
    1. Dim CNN As New Connection
    2.  
    3. Private Sub Workbook_Open()
    4. Dim rs As New Recordset
    5. CNN.Open "Driver={Microsoft Access Driver (*.mdb)};Dbq=C:\path to database\databasename.mdb"
    6. If CNN.State <> 0 Then
    7.     rs.Open "SELECT PROJECT_ID FROM TABLE1", CNN, adOpenDynamic, adLockOptimistic
    8.     Do While Not rs.EOF
    9.         Sheet1.ComboBox1.AddItem rs!ID
    10.         rs.MoveNext
    11.     Loop
    12.     rs.close
    13.     rs.Open "SELECT STATUS FROM TABLE2", CNN, adOpenDynamic, adLockOptimistic
    14.     Do While Not rs.EOF
    15.         Sheet1.ComboBox2.AddItem rs!ID
    16.         rs.MoveNext
    17.     Loop
    18.     rs.close
    19.     rs.Open "SELECT MANAGER FROM TABLE3", CNN, adOpenDynamic, adLockOptimistic
    20.     Do While Not rs.EOF
    21.         Sheet1.ComboBox3.AddItem rs!ID
    22.         rs.MoveNext
    23.     Loop
    24.     rs.close
    25. End If
    26. set rs = Nothing
    27. CNN.Close
    28. set CNN = Nothing
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Nov 2002
    Posts
    150

    Re: create a prompt in Excel and run a sql server query

    static - I'm using sql server connection not mdb and it doesn't recognize the connection object. am I missing any libraries?


    Dim CNN As ADODB.Connection
    Dim rs As ADODB.Recordset
    CNN.Open "Driver={SQL Server};Server=servername;Database=databasename;Uid=sa;Pwd=pass"


    thank you
    Last edited by waely; Jun 20th, 2006 at 12:29 PM.

  7. #7
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: create a prompt in Excel and run a sql server query

    see the Connection Strings link in my signature for connection strings to many DBMS's.. including SQL Server.

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Nov 2002
    Posts
    150

    Re: create a prompt in Excel and run a sql server query

    actually i got the connection from that site. my problem is that connection or recordset objects are not recognized. am I missing a library or a referance?

    thank you
    Last edited by waely; Jun 20th, 2006 at 12:46 PM.

  9. #9
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: create a prompt in Excel and run a sql server query

    Ah right, sorry.. I was reading too quickly!

    You need a reference (I think under to "Tools" -> "References") to "Microsoft ActiveX Data Objects X.X".

  10. #10
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: create a prompt in Excel and run a sql server query

    Thanks Si.. I was at lunch lol

    got it Waely?
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Nov 2002
    Posts
    150

    Re: create a prompt in Excel and run a sql server query

    Static -- hope you had a good lunch.

    I get this error : Run-Time '3265' : application-defined or object defined error
    -------------------------
    Do While Not rs.EOF
    Sheet1.OLEObjects("cbox1").AddItem rs!ID ----> ERROR LINE
    ' OR Sheet1.OLEObjects.cbox1.AddItem rs!ID --still same error

    rs.MoveNext
    Loop
    -----------------------

  12. #12
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: create a prompt in Excel and run a sql server query

    Sheet1.cbox1.AddItem rs!ID
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  13. #13

    Thread Starter
    Addicted Member
    Join Date
    Nov 2002
    Posts
    150

    Re: create a prompt in Excel and run a sql server query

    Quote Originally Posted by Static
    Sheet1.cbox1.AddItem rs!ID

    another error message : Method or data member not found

    my textbox snapshot:
    Attached Images Attached Images  

  14. #14
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: create a prompt in Excel and run a sql server query

    when u added the control.. did u just use the "Control Toolbox" bar or "Forms" bar?

    use the Controls Toolbox...

    and is it on Sheet1? or did u name the sheet?
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  15. #15

    Thread Starter
    Addicted Member
    Join Date
    Nov 2002
    Posts
    150

    Re: create a prompt in Excel and run a sql server query

    thank you static is works now on my mdb but when I switched the connection to sql server I got this error: Automation error upspecified error
    on this line
    CNN.Open "Driver={SQL Server};Server=serverName;Database=dbname;Uid=user;Pwd=pass;"

    do you know what might be the issue.
    thanks for your help.
    waely

  16. #16
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: create a prompt in Excel and run a sql server query

    sure do

    look at that line:

    Is the Server really called "ServerName"??
    is the database on the server called "dbname"??
    user?
    pass?
    you need to change each of the values (in bold) to suit your needs
    VB Code:
    1. CNN.Open "Driver={SQL Server};Server=[B]serverName[/B];Database=[B]dbname[/B];Uid=[B]user[/B];Pwd=[B]pass[/B];"
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  17. #17

    Thread Starter
    Addicted Member
    Join Date
    Nov 2002
    Posts
    150

    Re: create a prompt in Excel and run a sql server query

    Quote Originally Posted by Static
    sure do

    look at that line:

    Is the Server really called "ServerName"??
    is the database on the server called "dbname"??
    user?
    pass?
    you need to change each of the values (in bold) to suit your needs
    VB Code:
    1. CNN.Open "Driver={SQL Server};Server=[B]serverName[/B];Database=[B]dbname[/B];Uid=[B]user[/B];Pwd=[B]pass[/B];"
    Static - all parameters where changed from their original values to dummy names for security reasons. so the connection looks good?

Posting Permissions

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



Click Here to Expand Forum to Full Width