Results 1 to 7 of 7

Thread: Query is not working properly (RESOLVED)

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2004
    Posts
    104

    Query is not working properly (RESOLVED)

    I am trying to store the end result of this query into a variable

    VB Code:
    1. user = txtUsername.Text
    2. perms = "SELECT permissions FROM users WHERE username = '" & user & "'"

    Any ideas
    Last edited by Polariss; May 25th, 2004 at 09:07 AM.

  2. #2
    Fanatic Member vb_dba's Avatar
    Join Date
    Jun 2001
    Location
    Somewhere aloft between the real world and insanity
    Posts
    1,016
    Use a recordset:
    VB Code:
    1. Dim oCon   As Connection
    2. Dim oRS     As Recordset
    3. Dim user    As String
    4. Dim perms As String
    5. Dim sSql    As String
    6.  
    7. user = txtUsername.Text
    8. sSql = "SELECT permissions FROM users WHERE username = '" & user & "'"
    9.  
    10. 'Open a Connection to the database
    11. Set oCon = New Connection
    12. oCon.Open strYourConnectionString
    13.  
    14. 'Open the recordset
    15. Set oRS = New Recordset
    16. oRS.Open sSql, oCon, adOpenStatic, adLockReadOnly
    17.  
    18. 'Assign the result to the variable
    19. perms = oRS.Fields("permissions").Value
    20.  
    21. oRS.Close
    22. Set oRS = Nothing
    23. oCon.Close
    24. Set oCon = Nothing
    Chris

    Master Of My Domain
    Got A Question? Look Here First

  3. #3

    Thread Starter
    Lively Member
    Join Date
    May 2004
    Posts
    104

    Error: Either BOF or EOF is true or the record has been deleted

    Here is the whole code for the form

    VB Code:
    1. Dim mySQL As ADODB.Connection
    2.  
    3. Private Sub cmdLogin_Click()
    4. Dim mrs As Recordset
    5. Dim perms As String
    6. Dim permQuery As String
    7. Dim writeQuery As String
    8. writeQuery = "INSERT INTO verify(username, permissions) VALUES ('" & user & "', '" & perms & "')"
    9. permQuery = "SELECT permissions FROM users WHERE username = '" & user & "'"
    10. user = txtUsername.Text
    11. Set mrs = New Recordset
    12. mrs.Open permQuery, mySQL, adOpenStatic, adLockReadOnly 'ERROR OCCURS HERE
    13. perms = mrs.Fields("permissions").Value
    14. mrs.Close
    15. Set mrs = Nothing
    16. If UserCheck(txtUsername.Text, txtPassword.Text) = True Then
    17.         mySQL.Execute writeQuery
    18.         mySQL.Close
    19.         Set mySQL = Nothing
    20.         frmMenu.Show
    21.         Unload Me
    22.     Else
    23.         MsgBox "You have no account in our database", vbCritical
    24.     End If
    25. End Sub
    26.  
    27. Public Function UserCheck(strUserName As String, strPassword As String) As Boolean
    28.  
    29.     Dim rs As New ADODB.Recordset
    30.    
    31.     rs.Open "SELECT username, password FROM users WHERE username = '" & strUserName & "'", mySQL, adOpenKeyset, adLockBatchOptimistic
    32.  
    33.     If Not rs.EOF Then
    34.         If rs("password") = strPassword Then
    35.             rs.Close
    36.             Set rs = Nothing
    37.             UserCheck = True
    38.             Exit Function
    39.         Else
    40.             rs.Close
    41.             Set rs = Nothing
    42.             UserCheck = False
    43.             Exit Function
    44.  
    45.         End If
    46.     Else
    47.         rs.Close
    48.         Set rs = Nothing
    49.         UserCheck = False
    50.         Exit Function
    51.     End If
    52.  
    53. End Function
    54.  
    55. Private Sub cmdQuit_Click()
    56. End
    57. End Sub
    58.  
    59. Private Sub Form_Load()
    60. Set mySQL = New ADODB.Connection
    61.     mySQL.ConnectionString = ("Provider=MSDASQL;Driver={MySQL ODBC 3.51 Driver};Database=workorder;Server=server;UID=root;PWD=pass;OPTION=147458")
    62.     mySQL.Open
    63. End Sub
    Last edited by Polariss; May 25th, 2004 at 07:31 AM.

  4. #4

    Thread Starter
    Lively Member
    Join Date
    May 2004
    Posts
    104

    I got the error figured out by throwing in

    VB Code:
    1. Private Sub cmdLogin_Click()
    2. Dim mrs As Recordset
    3. Dim perms As String
    4. Dim permQuery As String
    5. Dim writeQuery As String
    6. writeQuery = "INSERT INTO verify(username, permissions) VALUES ('" & user & "', '" & perms & "')"
    7. permQuery = "SELECT permissions FROM users WHERE username = '" & user & "'"
    8. user = txtUsername.Text
    9. Set mrs = New Recordset
    10. mrs.Open permQuery, mySQL, adOpenStatic, adLockReadOnly
    11.         If Not mrs.EOF Then
    12.             perms = mrs.Fields("permissions").Value
    13.             mrs.Close
    14.             Set mrs = Nothing
    15.         End If
    16. If UserCheck(txtUsername.Text, txtPassword.Text) = True Then
    17.         mySQL.Execute writeQuery
    18.         mySQL.Close
    19.         Set mySQL = Nothing
    20.         frmMenu.Show
    21.         Unload Me
    22.     Else
    23.         MsgBox "You have no account in our database", vbCritical
    24.     End If
    25. End Sub

    But now I have another problem with my writeQuery. Its returning 0 values in the database for the variables user and permissions.

  5. #5
    Fanatic Member vb_dba's Avatar
    Join Date
    Jun 2001
    Location
    Somewhere aloft between the real world and insanity
    Posts
    1,016
    You don't assign a value to the "user" variable until after you've already declared your permissions query:
    VB Code:
    1. Dim mySQL As ADODB.Connection
    2.  
    3. Private Sub cmdLogin_Click()
    4. Dim mrs As Recordset
    5. Dim perms As String
    6. Dim permQuery As String
    7. Dim writeQuery As String
    8.  
    9. 'Move this declaration to here
    10. user = txtUsername.Text
    11.  
    12. writeQuery = "INSERT INTO verify(username, permissions) VALUES ('" & user & "', '" & perms & "')"
    13. permQuery = "SELECT permissions FROM users WHERE username = '" & user & "'"
    14.  
    15. Set mrs = New Recordset
    16. mrs.Open permQuery, mySQL, adOpenStatic, adLockReadOnly 'ERROR OCCURS HERE
    17. perms = mrs.Fields("permissions").Value
    18. mrs.Close
    19. Set mrs = Nothing
    20. If UserCheck(txtUsername.Text, txtPassword.Text) = True Then
    21.         mySQL.Execute writeQuery
    22.         mySQL.Close
    23.         Set mySQL = Nothing
    24.         frmMenu.Show
    25.         Unload Me
    26.     Else
    27.         MsgBox "You have no account in our database", vbCritical
    28.     End If
    29. End Sub
    Chris

    Master Of My Domain
    Got A Question? Look Here First

  6. #6

    Thread Starter
    Lively Member
    Join Date
    May 2004
    Posts
    104

    Ok now returning a value for user variable but not perms

    Now it is returning a value for the variable user into the verify table however perms is still returning an empty value.

    VB Code:
    1. Private Sub cmdLogin_Click()
    2. Dim mrs As Recordset
    3. Dim perms As String
    4. Dim permQuery As String
    5. Dim writeQuery As String
    6. user = txtUsername.Text
    7.  
    8. writeQuery = "INSERT INTO verify(username, permissions) VALUES ('" & user & "', '" & perms & "')"
    9. permQuery = "SELECT permissions FROM users WHERE username = '" & user & "'"
    10.  
    11. Set mrs = New Recordset
    12. mrs.Open permQuery, mySQL, adOpenStatic, adLockReadOnly
    13.         If Not mrs.EOF Then
    14.             perms = mrs.Fields("permissions").Value
    15.             mrs.Close
    16.             Set mrs = Nothing
    17.         End If
    18. If UserCheck(txtUsername.Text, txtPassword.Text) = True Then
    19.         mySQL.Execute writeQuery
    20.         mySQL.Close
    21.         Set mySQL = Nothing
    22.         frmMenu.Show
    23.         Unload Me
    24.     Else
    25.         MsgBox "You have no account in our database", vbCritical
    26.     End If
    27. End Sub

    Am I not doing something right with the recordset? It seems to keep returning a null value.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    May 2004
    Posts
    104

    Got it

    Just moved the other line of writeQuery and now its returning a good value for permissions. Thanks for all your help Chris

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