Results 1 to 14 of 14

Thread: Perform Select and Insert on same table in database in a click event problem.

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Perform Select and Insert on same table in database in a click event problem.

    Error:
    Run-time error '-2147217908 (80040e0c)':
    Command text was not set for the command object.

    Highlighted line is the line debug points to.

    What I am trying to do is first check the database table to see if the username value exists, if it does, throw up a form. If it doesnt exist, then to add the record to the database. I am sure thats the part that is at fault here.

    Thanks in advance for any help.
    VB Code:
    1. Private Sub cmdAddOperator_Click()
    2.     Dim strSQL As String
    3.    
    4.     Dim conn As ADODB.Connection
    5.     Set conn = New ADODB.Connection
    6.    
    7.     Dim rs As ADODB.Recordset
    8.     Set rs = New ADODB.Recordset
    9.    
    10.     conn.CursorLocation = adUseClient
    11.     conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    12.                "Data Source =Database.mdb;" & _
    13.                "Persist Security Info=False"
    14.    
    15.     [b][color=red]rs.Open strSQL, conn, adOpenKeyset, adLockOptimistic[/color][/b][color=red][/color]
    16.    
    17.     strSQL = "SELECT Username "
    18.     strSQL = strSQL & "FROM Users "
    19.     strSQL = strSQL & "WHERE Username = '" & txtOperatorUsername.Text & "')"
    20.  
    21.     If txtOperatorUsername.Text = rs.Fields("Username").Value Then
    22.         frmUsernameExists.Show
    23.    ElseIf Val(txtOperatorPassword2.Text) <> Val(txtOperatorPassword.Text) Then
    24.         frmOperatorPasswordNotSame.Show
    25.     Else
    26.         strSQL = "INSERT INTO Users "
    27.         strSQL = strSQL & "(id,Username,Password,Operator,FullAccess) "
    28.         strSQL = strSQL & "VALUES ('', "
    29.         strSQL = strSQL & "'" & txtOperatorUsername.Text & "', "
    30.         strSQL = strSQL & "'" & txtOperatorPassword.Text & "', "
    31.         strSQL = strSQL & "'" & txtOperatorName.Text & "', "
    32.         strSQL = strSQL & "'" & cboCompleteAccessYesNo.Text & "')"
    33.     End If
    34.  
    35.     rs.Close
    36.     Set rs = Nothing
    37.     conn.Close
    38.     Set conn = Nothing
    39. End Sub

  2. #2
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Perform Select and Insert on same table in database in a click event problem.

    You have not build the STRSQL string yet - you cannot open the recordset.

    Right?

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Re: Perform Select and Insert on same table in database in a click event problem.

    Quote Originally Posted by szlamany
    You have not build the STRSQL string yet - you cannot open the recordset.

    Right?
    VB Code:
    1. 'CHECK DB FOR USERNAME ALREADY EXIST
    2. strSQL = "SELECT Username "
    3.     strSQL = strSQL & "FROM Users "
    4.     strSQL = strSQL & "WHERE Username = '" & txtOperatorUsername.Text & "')"
    5. 'IF USERNAME EXIST, INFORM END ADMIN
    6.     If txtOperatorUsername.Text = rs.Fields("Username").Value Then
    7.         frmUsernameExists.Show
    8. 'IF PASSWORDS DONT MATCH, INFORM END ADMIN
    9.    ElseIf Val(txtOperatorPassword2.Text) <> Val(txtOperatorPassword.Text) Then
    10.         frmOperatorPasswordNotSame.Show
    11.     Else
    12. 'IF EVERYTHING IS FINE, INSERT THE RECORD
    13.         strSQL = "INSERT INTO Users "
    14.         strSQL = strSQL & "(id,Username,Password,Operator,FullAccess) "
    15.         strSQL = strSQL & "VALUES ('', "
    16.         strSQL = strSQL & "'" & txtOperatorUsername.Text & "', "
    17.         strSQL = strSQL & "'" & txtOperatorPassword.Text & "', "
    18.         strSQL = strSQL & "'" & txtOperatorName.Text & "', "
    19.         strSQL = strSQL & "'" & cboCompleteAccessYesNo.Text & "')"
    20.     End If

  4. #4
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Perform Select and Insert on same table in database in a click event problem.

    Is that part of the other code? Looks like strSQL is not in scope. Is it declared privately somewhere else? Check it's value with a msgbox.

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Re: Perform Select and Insert on same table in database in a click event problem.

    Quote Originally Posted by dglienna
    Is that part of the other code? Looks like strSQL is not in scope. Is it declared privately somewhere else? Check it's value with a msgbox.
    its not declared as private anywhere
    i also tried:
    VB Code:
    1. Private Sub cmdAddOperator_Click()
    2.     Dim strSQL As String
    3.     Dim strSQL2 As String
    4.    
    5.     Dim conn As ADODB.Connection
    6.     Set conn = New ADODB.Connection
    7.    
    8.     Dim rs As ADODB.Recordset
    9.     Set rs = New ADODB.Recordset
    10.    
    11.     conn.CursorLocation = adUseClient
    12.     conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    13.                "Data Source =Database.mdb;" & _
    14.                "Persist Security Info=False"
    15.    
    16.     rs.Open strSQL, conn, adOpenKeyset, adLockOptimistic
    17.    
    18.     strSQL = "SELECT Username "
    19.     strSQL = strSQL & "FROM Users "
    20.     strSQL = strSQL & "WHERE Username = '" & txtOperatorUsername.Text & "')"
    21.     conn.Execute strSQL
    22.    
    23.     If txtOperatorUsername.Text = rs.Fields("Username").Value Then
    24.         frmUsernameExists.Show
    25.     ElseIf Val(txtOperatorPassword2.Text) <> Val(txtOperatorPassword.Text) Then
    26.         frmOperatorPasswordNotSame.Show
    27.     Else
    28.         strSQL2 = "INSERT INTO Users "
    29.         strSQL2 = strSQL2 & "(id,Username,Password,Operator,FullAccess) "
    30.         strSQL2 = strSQL2 & "VALUES ('', "
    31.         strSQL2 = strSQL2 & "'" & txtOperatorUsername.Text & "', "
    32.         strSQL2 = strSQL2 & "'" & txtOperatorPassword.Text & "', "
    33.         strSQL2 = strSQL2 & "'" & txtOperatorName.Text & "', "
    34.         strSQL2 = strSQL2 & "'" & cboCompleteAccessYesNo.Text & "')"
    35.         conn.Execute strSQL2
    36.     End If
    37.    
    38.     rs.Close
    39.     Set rs = Nothing
    40.     conn.Close
    41.     Set conn = Nothing
    42. End Sub
    and got the same error.
    will try the msg box and see what happens
    i tried the msgbox and it wouldnt let me get to it, as soon as the error comes up, i click close and the app closes, tried in VB and same thing happened.

  6. #6
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Perform Select and Insert on same table in database in a click event problem.

    Ok - let's go slow here..

    The code you posted is simply got a small problem.

    The line it blows up on is because the prior line is in the wrong place - this is the correction.

    Code:
    Private Sub cmdAddOperator_Click()
        Dim strSQL As String
        
        Dim conn As ADODB.Connection
        Set conn = New ADODB.Connection
        
        Dim rs As ADODB.Recordset
        Set rs = New ADODB.Recordset
        
        conn.CursorLocation = adUseClient
        conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
                   "Data Source =Database.mdb;" & _
                   "Persist Security Info=False"
        
        strSQL = "SELECT Username "
        strSQL = strSQL & "FROM Users "
        strSQL = strSQL & "WHERE Username = '" & txtOperatorUsername.Text & "')"
    
        rs.Open strSQL, conn, adOpenKeyset, adLockOptimistic
        '  Move the OPEN, that uses strSQL, to after the string is built
        '  Before the string is built makes it execute an empty command...
    
    
        If txtOperatorUsername.Text = rs.Fields("Username").Value Then
            frmUsernameExists.Show
       ElseIf Val(txtOperatorPassword2.Text) <> Val(txtOperatorPassword.Text) Then
            frmOperatorPasswordNotSame.Show
        Else
            strSQL = "INSERT INTO Users "
            strSQL = strSQL & "(id,Username,Password,Operator,FullAccess) "
            strSQL = strSQL & "VALUES ('', "
            strSQL = strSQL & "'" & txtOperatorUsername.Text & "', "
            strSQL = strSQL & "'" & txtOperatorPassword.Text & "', "
            strSQL = strSQL & "'" & txtOperatorName.Text & "', "
            strSQL = strSQL & "'" & cboCompleteAccessYesNo.Text & "')"
        End If
    
        rs.Close
        Set rs = Nothing
        conn.Close
        Set conn = Nothing
    End Sub

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  7. #7
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Perform Select and Insert on same table in database in a click event problem.

    My bad. I should have said to put the msgbox before the open statement.

  8. #8

    Thread Starter
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Re: Perform Select and Insert on same table in database in a click event problem.

    Quote Originally Posted by szlamany
    Ok - let's go slow here..

    The code you posted is simply got a small problem.

    The line it blows up on is because the prior line is in the wrong place - this is the correction.

    Code:
    Private Sub cmdAddOperator_Click()
        Dim strSQL As String
        
        Dim conn As ADODB.Connection
        Set conn = New ADODB.Connection
        
        Dim rs As ADODB.Recordset
        Set rs = New ADODB.Recordset
        
        conn.CursorLocation = adUseClient
        conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
                   "Data Source =Database.mdb;" & _
                   "Persist Security Info=False"
        
        strSQL = "SELECT Username "
        strSQL = strSQL & "FROM Users "
        strSQL = strSQL & "WHERE Username = '" & txtOperatorUsername.Text & "')"
    
        rs.Open strSQL, conn, adOpenKeyset, adLockOptimistic
        '  Move the OPEN, that uses strSQL, to after the string is built
        '  Before the string is built makes it execute an empty command...
    
    
        If txtOperatorUsername.Text = rs.Fields("Username").Value Then
            frmUsernameExists.Show
       ElseIf Val(txtOperatorPassword2.Text) <> Val(txtOperatorPassword.Text) Then
            frmOperatorPasswordNotSame.Show
        Else
            strSQL = "INSERT INTO Users "
            strSQL = strSQL & "(id,Username,Password,Operator,FullAccess) "
            strSQL = strSQL & "VALUES ('', "
            strSQL = strSQL & "'" & txtOperatorUsername.Text & "', "
            strSQL = strSQL & "'" & txtOperatorPassword.Text & "', "
            strSQL = strSQL & "'" & txtOperatorName.Text & "', "
            strSQL = strSQL & "'" & cboCompleteAccessYesNo.Text & "')"
        End If
    
        rs.Close
        Set rs = Nothing
        conn.Close
        Set conn = Nothing
    End Sub
    If I type in a username that already exists, it shows the prompt like it is supposed to. When you click on the ok button, it clears the username field, then if you enter in a username that doesnt exist, then it is supposed to add the record (as long as the password fields match), but instead of adding it, I get the following error message (which i believe means username doesnt exist)

    Also, if i complete the form fields with a username that doesnt exist, (with the passwords not matching or the passwords matching) then i get the same error message. ugh

    What is supposed to happen is when you have completed the form and clicked on the add button, its supposed to check the db to see if the username exists, if it does, throw up a form to say it exists. If the username doesnt exist then its supposed to check to see if the passwords fields match. if the passwords fields dont match, then to inform user. If everything is fine (username doesnt exist in db and password fields match) then to add the record to the db.
    run-time error '3021':
    either bof or eof is true, or the current record has been deleted. requested operation requires a current record.
    Update:
    I changed part of the code to look like the following and still get the b/eof error:
    VB Code:
    1. If txtOperatorUsername.Text = rs.Fields("Username").Value Then
    2.         frmUsernameExists.Show
    3.     ElseIf Val(txtOperatorPassword2.Text) <> Val(txtOperatorPassword.Text) Then
    4.         frmOperatorPasswordNotSame.Show
    5.     ElseIf (rs.BOF And rs.EOF) = False Then
    6.         strSQL = "INSERT INTO Users "
    7.         strSQL = strSQL & "(id,Username,Password,Operator,FullAccess) "
    8.         strSQL = strSQL & "VALUES ('', "
    9.         strSQL = strSQL & "'" & txtOperatorUsername.Text & "', "
    10.         strSQL = strSQL & "'" & txtOperatorPassword.Text & "', "
    11.         strSQL = strSQL & "'" & txtOperatorName.Text & "', "
    12.         strSQL = strSQL & "'" & cboCompleteAccessYesNo.Text & "')"
    13.     End If
    Complete code to date:
    VB Code:
    1. Private Sub cmdAddOperator_Click()
    2.     Dim strSQL As String
    3.    
    4.     Dim conn As ADODB.Connection
    5.     Set conn = New ADODB.Connection
    6.    
    7.     Dim rs As ADODB.Recordset
    8.     Set rs = New ADODB.Recordset
    9.    
    10.     conn.CursorLocation = adUseClient
    11.     conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    12.                "Data Source =Database.mdb;" & _
    13.                "Persist Security Info=False"
    14.    
    15.     strSQL = "SELECT Username "
    16.     strSQL = strSQL & "FROM Users "
    17.     strSQL = strSQL & "WHERE Username = '" & txtOperatorUsername.Text & "'"
    18.    
    19.     rs.Open strSQL, conn, adOpenKeyset, adLockOptimistic
    20.    
    21.     If txtOperatorUsername.Text = rs.Fields("Username").Value Then
    22.         frmUsernameExists.Show
    23.     ElseIf Val(txtOperatorPassword2.Text) <> Val(txtOperatorPassword.Text) Then
    24.         frmOperatorPasswordNotSame.Show
    25.     ElseIf (rs.BOF And rs.EOF) = False Then
    26.         strSQL = "INSERT INTO Users "
    27.         strSQL = strSQL & "(id,Username,Password,Operator,FullAccess) "
    28.         strSQL = strSQL & "VALUES ('', "
    29.         strSQL = strSQL & "'" & txtOperatorUsername.Text & "', "
    30.         strSQL = strSQL & "'" & txtOperatorPassword.Text & "', "
    31.         strSQL = strSQL & "'" & txtOperatorName.Text & "', "
    32.         strSQL = strSQL & "'" & cboCompleteAccessYesNo.Text & "')"
    33.     End If
    34.    
    35.     rs.Close
    36.     Set rs = Nothing
    37.     conn.Close
    38.     Set conn = Nothing
    39. End Sub
    I also did a msgbox on the select statement, it shows the select to be correct but when i click ok, i then get the b/eof error. so it is either the password field check or the insert. still working on it. select capture enclosed.

  9. #9
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Perform Select and Insert on same table in database in a click event problem.

    You still are using VAL() on the passwords. And ths:
    (rs.BOF And rs.EOF) = False
    should be

    Not rs.BOF And Not rs.EOF

    except that if the record isn't found, you want to add it, not if a record was returned, right?

  10. #10
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Perform Select and Insert on same table in database in a click event problem.

    Plus, don't you want an ADMIN to add people? Seems like anyone that tries to log in would be able to create a new account.

  11. #11

    Thread Starter
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Re: Perform Select and Insert on same table in database in a click event problem.

    Quote Originally Posted by dglienna
    Plus, don't you want an ADMIN to add people? Seems like anyone that tries to log in would be able to create a new account.
    the add/delete/edit operator options are only available to admins, when a reg user logs in, those options are not available.

  12. #12

    Thread Starter
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Re: Perform Select and Insert on same table in database in a click event problem.

    Quote Originally Posted by dglienna
    You still are using VAL() on the passwords. And ths:


    should be

    Not rs.BOF And Not rs.EOF

    except that if the record isn't found, you want to add it, not if a record was returned, right?
    Yes, only add a record if username doesnt exist and passwords match. otherwise prompt user when username is found or passwords dont match. ill make the alterations now

    Update:
    I made the modification. If the username existed, I was informed. If i changed the username to a username that didnt exist, i got the bof/eof error msg. didnt make a difference if passwords matched or not and didnt get the prompt if the passwords didnt match.

  13. #13
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Perform Select and Insert on same table in database in a click event problem.

    If you are using VAL() on a string, it won't work, rather they'd be 0

  14. #14

    Thread Starter
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Re: Perform Select and Insert on same table in database in a click event problem.

    Quote Originally Posted by dglienna
    If you are using VAL() on a string, it won't work, rather they'd be 0
    removed val()
    still not able to get this to work so it seems ill have to do a two step process. check for username and if it doesnt exist, then to bring up the addition form.

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