Read from a database without a control

Posted On 2018-01-14 by SehriYazdani
Tags: VB6 Databases VB6 Miscellaneous Windows
Views: 70

This snippet of code shows how to connect to a database using a Data Source Name (DSN), and a reference to Microsoft ActiveX Data Objects (ADO).

To make the reference to ADO, while in the IDE, click Project, References. In the References window, select the version of MS ADO that you wish to use. In the picture below, I've selected version 6.1.

MSADO Window Screenshot

Dim rs as ADODB.Recordset
Dim Con as ADODB.Connection
Dim ssql as String
Const strCon ="DSN=Contacts;Description=Contacts;SERVER=ServerName;UID=sa;Password=;"

Private Sub combo1_DropDown()

    Set Con = New ADODB.Connection
    Set rs = New ADODB.Recordset
    Con.Open strCon

    'sql statement to select items on the drop down list
    ssql = "Select LastName From Contacts"
    rs.Open ssql, Con

    Do Until rs.EOF
        combo1.AddItem rs("LastName") 'Adds lastnames to dropdown list

    'Close connection and the recordset
    Set rs = Nothing
    Set Con = Nothing

End Sub

About the Author

SehriYazdani has posted a total of 1 articles.

Comments On This Post

No comments on this post yet!

Do you have a thought relating to this post? You can post your comment here. If you have an unrelated question, you can use the Q&A section to ask it.

Or you can drop a note to the administrators if you're not sure where you should post.

Your IP address is:

Before you can post, you need to prove you are human. If you log in, this test goes away.

Code Links