We help IT Professionals succeed at work.

Using vb6 to retreive value from sql and place in textbox

SirNick asked
Last Modified: 2013-12-25
Hello All

I have been using vb6 on and off for a few years but am new to using databases in vb6.  I am using the code below that I got from the net to connect which works fine however in the last statement I want to retrieve a value from the name column and place it in a textbox but I cant get it to work.  Any comments would be appreciated.

'Declaring your database
Public MyDatabase As ADODB.Connection

'Tables into your database
Public MyTable1 As ADODB.Recordset

Private Sub Form_Load()

'Set your variables to nothing (just for stability)
Set MyDatabase = Nothing
Set MyTable1 = Nothing

'Connecting to your DB
Set MyDatabase = New ADODB.Connection
Set MyTable1 = New ADODB.Recordset

'Here's where you're to set all your database options
ConnectionString = "Provider=SQLOLEDB.1" _
              & ";User ID=sa" _
              & ";Password=theknapp1" _
              & ";Initial Catalog=aTable" _
              & ";Data Source=mypc\SQLEXPRESS"

'If your database is in another computer in the local network you can replace with it's ip adress
MyDatabase.Open ConnectionString
MyTable1.CursorLocation = adUseClient

Text1.text = MyDatabase.Execute ("SELECT name FROM Table_1")

The above statement is the problem

End sub

Watch Question

MyDatabase.Execute returns a recordset object, so it can't be converted to string directly.
In order to be able to move through the results of the SQL do:

Dim r As ADODB.Recordset
r=MyDatabase.Execute ("SELECT name FROM Table_1")

Text1.text = r.Fields("name").Value


Thanks but I'm getting an error,  'Invalid use of property'..
Unlock this solution and get a sample of our free trial.
(No credit card required)


I'm sure I tried that, but I just tried again and its worked....so I couldn't have

Thanks for that, you will obviuously get the points.........

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.