Option Explicit
Private Sub cmdConnect_Click() Dim objConnection As Object Dim objContents As Object Dim strSQL As String 'Create the ADO connection. This is the handiest way to 'connect to a database in my uneducated opinion, so if you 'disagree, write your own code. ;-) Set objConnection = CreateObject("ADODB.Connection") 'Next, open the connection to the database. objConnection.Open "Driver={SQL Server};Server=Server;Database=Database;uid=sa;pwd=;" 'Now, for this next part to make sense, you'll need at least 'a little experience writing SQL queries. This is the simplest. strSQL = "SELECT * FROM Table" Set objContents = objConnection.execute(strSQL) varResult = objContents(0) varResult = objContents("<FIELD NAME>") While objContents.BOF = False And objContents.EOF = False varResult = objContents("<FIELD NAME>") ListBox1.AddItem varResult objContents.MoveNext ' This moves on To the next ROW Wend End Sub We're going to assume we're using a SQL Server that is named 'Server'. We're also going to assume the target database's name is 'Database'. The default UID in SQL is 'sa', so let's just use that, and the default password is either 'sa' or blank (I'm going to use blank). Finally, let's assume the Table name is 'Table'. |