Results 1 to 8 of 8

Thread: SQL Query

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2003
    Location
    Auckland
    Posts
    1,139

    SQL Query

    I've connected to a SQL DB and I've added records but how would I get info and put it into a text1.text ??

  2. #2
    Addicted Member
    Join Date
    Mar 2005
    Posts
    174

    Re: SQL Query

    VB Code:
    1. Dim conn As ADODB.Connection
    2. Dim rs As ADODB.Recordset
    3.  
    4. Private Sub Command1_Click()
    5. conn.Open
    6. rs.Open "select * from employee", conn, adOpenStatic, adLockOptimistic
    7. rs.Requery
    8. Text1.Text = rs.Fields("ename")
    9. End Sub
    10.  
    11. Private Sub Form_Load()
    12. Set conn = New ADODB.Connection
    13. Set rs = New ADODB.Recordset
    14. conn.ConnectionString = "Provider=OraOLEDB.Oracle.1;Password=tiger;Persist Security Info=True;User ID=scott"
    15. End Sub

  3. #3
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926

    Re: SQL Query

    Why do you requery the recordset just after you opened it. Are you affraid something changed already?

    Remove the rs.Requery line, because the only thing it does is hammer the database server.
    Frans

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: SQL Query

    Also, if you are looking for specific information in specific records there is no need to build a recordset containing every field in your table. Specifiy the fields that you want in your SELECT.

    Also, insofar as you are after specific information, a WHERE clause, and possibly some AND clauses would be required in order to drill down to exactly what you want.

    If you are going to be displaying the information in a text box, the inference is you are only expecting a one record return.

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2003
    Location
    Auckland
    Posts
    1,139

    Re: SQL Query

    So can someone show me a sample code the correct way

  6. #6
    PowerPoster
    Join Date
    May 2006
    Posts
    2,988

    Re: SQL Query

    I dont know SQL Server nor the connection so i left that out ..
    also you didnt say if you were retrieving multiple records or not ..
    this is a single record .. you would also want to check for a null value.
    VB Code:
    1. OpenConnection
    2.     strSQL = "SELECT myField FROM myTable WHERE myId = 1"
    3.     Set objRs = objConn.Execute(strSQL, , adCmdText)
    4.     If Not objRs.EOF Then
    5.         Text1 = objRs("myField")
    6.     End If
    7.     Set objRs = Nothing
    8.     CloseConnection

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2003
    Location
    Auckland
    Posts
    1,139

    Re: SQL Query

    Quote Originally Posted by rory
    I dont know SQL Server nor the connection so i left that out ..
    also you didnt say if you were retrieving multiple records or not ..
    this is a single record .. you would also want to check for a null value.
    VB Code:
    1. OpenConnection
    2.     strSQL = "SELECT myField FROM myTable WHERE myId = 1"
    3.     Set objRs = objConn.Execute(strSQL, , adCmdText)
    4.     If Not objRs.EOF Then
    5.         Text1 = objRs("myField")
    6.     End If
    7.     Set objRs = Nothing
    8.     CloseConnection

    Yeah I need about 5 or 6 records... what does it change to then?

  8. #8
    PowerPoster
    Join Date
    May 2006
    Posts
    2,988

    Re: SQL Query

    VB Code:
    1. If Not objRs.EOF Then
    2.     Do While Not objRs.EOF
    3.         '// do something
    4.         objRs.MoveNext
    5.     Loop
    6. End If
    Last edited by rory; Aug 25th, 2006 at 11:57 PM.

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