How can I connect my msacces database and use SQL query command like "Select Field FROM Table Where Field1 = foo AND Field2 = foo2" in Visual basic 6 and display the query result to a textbox.

Use the ADODB object library to establish a connection, create a recordset, execute the SQL statement you want and retrieve the records. There are some pretty good examples in the VB help file. Just search for "ADODB".

Try something similar to -

Dim cnCheck As ADODB.Connection
Set cnCheck = New ADODB.Connection
Dim rsCheck As ADODB.Recordset
Set rsCheck = New ADODB.Recordset

cnCheck.CursorLocation = adUseClient

cnCheck.Open "provider = microsoft.jet.oledb.4.0;persist security info=false;data source = " & App.Path & "\MyDatabaseName.mdb"

rsCheck.Open "SELECT Field1, Field2 FROM TableNameHere WHERE Field1='foo' AND Field2='foo2'", cnCheck, adOpenDynamic, adLockPessimistic

If rsCheck.BOF = True Or rsCheck.EOF = True Then
    ''No records found...
    txtShow.Text = rsCheck!Field1 ''Or whatever field you want.
End If

Why can't I use this "SELECT Field1, Field2 FROM TableNameHere WHERE Field1=Text1.Text AND Field2=Text2.Text", cnCheck, adOpenDynamic, adLockPessimistic

How do I use a textbox instead of a constant string?

"SELECT Field1, Field2 FROM TableNameHere WHERE Field1='" & Text1.Text & "' AND Field2='" & Text2.Text & "'", cnCheck, adOpenDynamic, adLockPessimistic

That is an entire new question. Please mark this question as solved and open a new thread with your question. I will gladly help in the new question.

This question has already been answered. Start a new discussion instead.