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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

vb6 help creating listbox recordsource?

Status
Not open for further replies.

AccessDevJunior

Programmer
Apr 14, 2004
81
GB
hya,
i am new to vb6 my experience lies with access.
i am currently designing an application using vb6 but i do not know how to get vb6 to retreive the data i want.
I have set up an access database and created the tables and relationships i need.
in my vb6 form i have a list box, i would like this listbox on the click of a command button (cmdrun) to retreive data from one the access database table.
i really need step by step instruction on how to do this, if anyone could give me any pointers it would be a great help?
 
AccesDevJr

To do this make a access DB called "names". Put a field of First and Last in there. Put 4 or 5 first and last names in it. Create a system ODBC connection calles Names (control Panel- Administrator Tools - ODBC .

Open up a new exe VB6 project.
On the form put a listbox and a command button.
The listbox is listbox1 and the command button is command1 by default so leave them that way.

Double click the command button and place this code between the lines of the procedure header and footer.
--------------------------------------
Dim rs1 As ADODB.Recordset
Dim Conn As ADODB.Connection
Dim Sql As String


Set Conn = New ADODB.Connection
Set rs1 = New ADODB.Recordset

Conn.Open "DSN=NAMES"

Sql = "SELECT FIRST, LAST FROM NAMES"

With rs1
If rs1.State = 1 Then rs1.Close
.ActiveConnection = Conn
.CursorLocation = adUseClient
.CursorType = adOpenDynamic
.Source = Sql
.Open
End With

While Not rs1.EOF

List1.AddItem rs1(0) & vbTab & rs1(1)
rs1.MoveNext
Wend
----------------------------------

In your dev enviroment go to the menue project then project refences then find and click on on MS Active X data objects library 2.5 or somthing close.

When you run the code and click on the command button it will populate the list box from the database.

Good luck. After you get it working research every line detail of the code and you see whats going on and what other options are available.



Enjoy

Dan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top