|
-
Aug 25th, 2006, 04:23 AM
#1
Thread Starter
Frenzied Member
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 ??
-
Aug 25th, 2006, 04:36 AM
#2
Addicted Member
Re: SQL Query
VB Code:
Dim conn As ADODB.Connection
Dim rs As ADODB.Recordset
Private Sub Command1_Click()
conn.Open
rs.Open "select * from employee", conn, adOpenStatic, adLockOptimistic
rs.Requery
Text1.Text = rs.Fields("ename")
End Sub
Private Sub Form_Load()
Set conn = New ADODB.Connection
Set rs = New ADODB.Recordset
conn.ConnectionString = "Provider=OraOLEDB.Oracle.1;Password=tiger;Persist Security Info=True;User ID=scott"
End Sub
-
Aug 25th, 2006, 06:08 AM
#3
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.
-
Aug 25th, 2006, 06:18 AM
#4
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.
-
Aug 25th, 2006, 08:37 PM
#5
Thread Starter
Frenzied Member
Re: SQL Query
So can someone show me a sample code the correct way
-
Aug 25th, 2006, 11:38 PM
#6
PowerPoster
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:
OpenConnection
strSQL = "SELECT myField FROM myTable WHERE myId = 1"
Set objRs = objConn.Execute(strSQL, , adCmdText)
If Not objRs.EOF Then
Text1 = objRs("myField")
End If
Set objRs = Nothing
CloseConnection
-
Aug 25th, 2006, 11:41 PM
#7
Thread Starter
Frenzied Member
Re: SQL Query
 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:
OpenConnection
strSQL = "SELECT myField FROM myTable WHERE myId = 1"
Set objRs = objConn.Execute(strSQL, , adCmdText)
If Not objRs.EOF Then
Text1 = objRs("myField")
End If
Set objRs = Nothing
CloseConnection
Yeah I need about 5 or 6 records... what does it change to then?
-
Aug 25th, 2006, 11:47 PM
#8
PowerPoster
Re: SQL Query
VB Code:
If Not objRs.EOF Then
Do While Not objRs.EOF
'// do something
objRs.MoveNext
Loop
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|