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.

Recommended Answers

All 6 Replies

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...
        Else
    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

One more question, How Can you edit or update a record in MS Access

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.