Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Displaying SQL

Status
Not open for further replies.

EscapeUK

Programmer
Jul 7, 2000
438
GB
This is in VB. I have a text box where I get the user to enter a SQL statement. I would then like to execute this and display the results in another text box.
 
All you need is the following

option explicit

Dim oConnection as adodb.connection


Private function GetConnection as Boolean

dim bconnected as boolean


set oConnection = new adodb.connection

with oconnection

.connectionstring = "the connection string for your database"
.open

end with

'Check if the connection opened correctly

if oConnection.state = adstateopen then
bConnected = true
else
bconnected = false
end if

getconnection = bconnected

end function


'This is the part that does the work
Private sub ExecuteSQL()

dim oRs as adodb.recordset

'Establish your connection and test it
if getconnection = true then
'Create the recordset
set oRs new adodb.recordset

'The executes the SQL against the connection and populates the recordset
with oRS
.activeconnection = oconnection
.source = text1.text 'The SQL source textbox
.open
end with

'populate the other textbox

'This will loop until the end of the recordset
while not oRS.eof
text2.text = oRS.fields("Field_Name").value
wend

I hope this gives you a good starting point if you need more info then leave a comment
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top