+ Reply to Thread
Results 1 to 23 of 23

Connect to SQL SERVER with a excel 2010 userform

  1. #1
    Registered User
    Join Date
    09-10-2012
    Location
    usa
    MS-Off Ver
    Excel 2010
    Posts
    86

    Question Connect to SQL SERVER with a excel 2010 userform

    I created a userform with a text box that says Status and below it is a command button that says check status. There is also a list box that provides the result of the query.
    When the user enters transaction id in the text box and clicks submit. It should connect to a SQL database and search a table called TBL_TRANSACTION. when it finds the Transaction id. it shows the resultus in the list box (Lisbox1) example if pending, cancelled, updating. It should show it in the list box. Here is what i did so far but its not working

    Please Login or Register  to view this content.
    Last edited by jeffreybrown; 09-11-2012 at 03:20 PM. Reason: Please use code tags

  2. #2
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Connect to SQL SERVER with a excel 2010 userform

    you don't have any code that actually queries the database or populates any controls so it's not unexpected that it doesn't work ;-) you need to open the recordset using the query string and then assign the recordset data to the listbox using
    Please Login or Register  to view this content.
    Josie

    if at first you don't succeed try doing it the way your wife told you to

  3. #3
    Registered User
    Join Date
    09-10-2012
    Location
    usa
    MS-Off Ver
    Excel 2010
    Posts
    86

    Re: Connect to SQL SERVER with a excel 2010 userform

    Please Login or Register  to view this content.
    get error " operation is not allowed when object is closed"

    Please advise
    Last edited by jeffreybrown; 09-11-2012 at 03:21 PM.

  4. #4
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Connect to SQL SERVER with a excel 2010 userform

    I did say you had to open the recordset first :-)

    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    09-10-2012
    Location
    usa
    MS-Off Ver
    Excel 2010
    Posts
    86

    Re: Connect to SQL SERVER with a excel 2010 userform

    I am currently using my uname and Pwd within the connection string how do i change it so that the user enters their info. I currently have

    Please Login or Register  to view this content.
    Please let me know what I need to do
    Last edited by jeffreybrown; 09-11-2012 at 03:21 PM.

  6. #6
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Connect to SQL SERVER with a excel 2010 userform

    what security does the sql server use? windows logins or database user ids and passwords? also which provider are you using?

  7. #7
    Registered User
    Join Date
    09-10-2012
    Location
    usa
    MS-Off Ver
    Excel 2010
    Posts
    86

    Re: Connect to SQL SERVER with a excel 2010 userform

    Database logins.
    Provider is SqlOledb

  8. #8
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Connect to SQL SERVER with a excel 2010 userform

    I reckon you need to set the prompt property of the connection to adpromptalways before you set the connection (leave the userid and password parts out)
    Please Login or Register  to view this content.

  9. #9
    Registered User
    Join Date
    09-10-2012
    Location
    usa
    MS-Off Ver
    Excel 2010
    Posts
    86

    Re: Connect to SQL SERVER with a excel 2010 userform

    I received error 3220 when I the uid and Pwd and replace with your code

    Please advise

  10. #10
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Connect to SQL SERVER with a excel 2010 userform

    what is your complete code now? that error is about trying to change providers whilst one is in use

  11. #11
    Registered User
    Join Date
    09-10-2012
    Location
    usa
    MS-Off Ver
    Excel 2010
    Posts
    86

    Re: Connect to SQL SERVER with a excel 2010 userform

    Here is my current code
    Provider = sqloledb
    Data source = some source
    Initial catalog = test
    Conn.properties("prompt")= adpromptalways

  12. #12
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Connect to SQL SERVER with a excel 2010 userform

    that's not code

  13. #13
    Registered User
    Join Date
    09-10-2012
    Location
    usa
    MS-Off Ver
    Excel 2010
    Posts
    86

    Re: Connect to SQL SERVER with a excel 2010 userform

    Private Sub cmdStatus_Click()
    Dim conn As New ADODB.Connection
    Dim myRecSet As New ADODB.Recordset
    Dim SQL As String

    conn.Open _
    "Provider=SQLOLEDB; " & _
    "Data Source=SQLEXPRESS; " & _
    "Initial Catalog=test;" & _
    "User ID=test;" & _
    "Password= test;"


    If txtTransaction = "" Then
    MsgBox ("You have not entered a Transaction_ID"), vbExclamation

    Else
    SQL = "select TR_Status from TBL_TEST WHERE TR_Id = '" & frmTransaction.txtTransaction.Text & "'"

    myRecSet.Open SQL, conn
    If Not myRecSet.EOF Then
    Me.ListBox1.Column = myRecSet.GetRows


    End If
    End If
    End Sub

  14. #14
    Registered User
    Join Date
    09-10-2012
    Location
    usa
    MS-Off Ver
    Excel 2010
    Posts
    86

    Re: Connect to SQL SERVER with a excel 2010 userform

    Here is the one with the new line


    Private Sub cmdStatus_Click()
    Dim conn As New ADODB.Connection
    Dim myRecSet As New ADODB.Recordset
    Dim SQL As String

    conn.Open _
    "Provider=SQLOLEDB; " & _
    "Data Source=MORSQLEXPRESS; " & _
    "Initial Catalog=test;" & _

    conn.Properties("Prompt") = adPromptAlways


    If txtTransaction = "" Then
    MsgBox ("You have not entered a Transaction_ID"), vbExclamation

    Else
    SQL = "select TR_Status from TBL_TEST WHERE TR_Id = '" & frmTransaction.txtTransaction.Text & "'"

    myRecSet.Open SQL, conn
    If Not myRecSet.EOF Then
    Me.ListBox1.Column = myRecSet.GetRows


    End If
    End If
    End Sub

  15. #15
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Connect to SQL SERVER with a excel 2010 userform

    move this line
    Please Login or Register  to view this content.
    before the line that opens the connection

    also note that a moderator has been adding code tags to your posts for you but if you don't start doing it yourself you will probably get reprimanded pretty soon

  16. #16
    Registered User
    Join Date
    09-10-2012
    Location
    usa
    MS-Off Ver
    Excel 2010
    Posts
    86

    Re: Connect to SQL SERVER with a excel 2010 userform

    Error 3220 again

  17. #17
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Connect to SQL SERVER with a excel 2010 userform

    my bad-was testing with the wrong provider
    Please Login or Register  to view this content.

  18. #18
    Registered User
    Join Date
    09-10-2012
    Location
    usa
    MS-Off Ver
    Excel 2010
    Posts
    86

    Re: Connect to SQL SERVER with a excel 2010 userform

    You are a Rick star thanks a lot

  19. #19
    Registered User
    Join Date
    09-10-2012
    Location
    usa
    MS-Off Ver
    Excel 2010
    Posts
    86

    Re: Connect to SQL SERVER with a excel 2010 userform

    Last question when the SQL login box pops up I notice that when I click cancel it returns to my code on line .conn. how do I prevent that from happening . Maybe a message box Can pop up

  20. #20
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Connect to SQL SERVER with a excel 2010 userform

    there is no line .conn
    I reckon you'll have to add an error handler to trap the opening of the connection failing

  21. #21
    Registered User
    Join Date
    09-10-2012
    Location
    usa
    MS-Off Ver
    Excel 2010
    Posts
    86

    Re: Connect to SQL SERVER with a excel 2010 userform

    It's on .open not .con

    Run time error invalid authorization

  22. #22
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Connect to SQL SERVER with a excel 2010 userform

    like I said you need an error handler
    Please Login or Register  to view this content.

  23. #23
    Registered User
    Join Date
    09-10-2012
    Location
    usa
    MS-Off Ver
    Excel 2010
    Posts
    86

    Re: Connect to SQL SERVER with a excel 2010 userform

    quick question instead of using the SQL statement
    Else
    SQL = "select TR_Status from TBL_TEST WHERE TR_Id = '" & frmTransaction.txtTransaction.Text & "'"

    i would like to replace that with a store procedure 'sp_transmon_get_transactions_by_status'

    and also how can a export my listbox entries into a new spreadheet. my list box has 3 colums name, address, date i would like to export all 3 colums into a excel spresheet with the click of a button the spresheet must have all there colums

    any help would be appreciated

+ 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