Search using Combo Box and Filter the results in text boxes (1 Viewer)

comep

Registered User.
Local time
Today, 08:13
Joined
Mar 4, 2017
Messages
20
Dears

I am trying to do a small program on VB 6.0 to find the records in database and print it in text box based on combo box selection but i failed to find a code which allow me to do this.

Any help please.
 

Ranman256

Well-known member
Local time
Today, 11:13
Joined
Apr 9, 2015
Messages
4,339
you need to use database connection control to connect to the database.
(that or ADO connection code)

then use that to connect textbox to the db control.
 

comep

Registered User.
Local time
Today, 08:13
Joined
Mar 4, 2017
Messages
20
you need to use database connection control to connect to the database.
(that or ADO connection code)

then use that to connect textbox to the db control.

Thanks for your help but could you please assist me with a code to connect to database as i am using database with extension .accdb and all codes available is for mdb database.

Note : I want to use a code to connect to database.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 23:13
Joined
May 7, 2009
Messages
19,169
here is a small script:

Dim adoCon
Dim adoRs
Dim strSQL As String


Dim strDB As String

'Change YourDatabaseName to actual database you have
strDB = "c:\path\YourDatabaseName.accdb"



Set adoCon = CreateObject("ADODB.Connection")
adoCon.Open "Provider = Microsoft.ACE.OLEDB.12.0; " & _
"Data Source = " & strDB & ";" & _
"Persist Security Info = False;"

'Change Table1 to your table name in MS Access
'change the name of combobox and the fieldname in MS Access table
'
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' if combo is numeric
strSQL = "SELECT [fieldNameToReturn] FROM Table1 Where [fieldName] = " + [combo].Value + ";"

' if combo is text
'strSQL = "SELECT [fieldNameToReturn] FROM Table1 Where [fieldName] = '" + [combo].Value + "';"
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Set adoRs = CreateObject("ADODB.Recordset")

'Set the cursor type we are using so we can navigate through the recordset
adoRs.CursorType = 2

'Set the lock type so that the record is locked by ADO when it is updated
adoRs.LockType = 3

'Open the tblComments table using the SQL query held in the strSQL varaiable
'adoRs.Open strSQL, adoCon

If Not adoRS.Eof() Then
[yourTextBox] = adoRs(0)
End If
adoRs.Close
adoCon.Close
Set adoRs = Nothing
Set adoCon = Nothing
 
Last edited:

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 23:13
Joined
May 7, 2009
Messages
19,169
insert the code in your Combo's SelectedValueChanged Event.

you should modify also this:

strSQL="SELECT [fieldNameToReturn] FROM Table1 Where [fieldName]='" + [combo].Value + "';"
TO:
strSQL="SELECT [fieldNameToReturn] FROM Table1 Where [fieldName]='" + Me.combo.ToString + "';"

also this one:
[qoute]
[yourTextBox]=adoRs(0)
[/quote]
TO:
Me.yourTextBox.Text = adoRs.Fields(0).Value
 

comep

Registered User.
Local time
Today, 08:13
Joined
Mar 4, 2017
Messages
20
insert the code in your Combo's SelectedValueChanged Event.

you should modify also this:


TO:


also this one:
[qoute]
[yourTextBox]=adoRs(0)
TO:[/QUOTE]

Thanks its worked but which code should be modified to show values in combobox
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 23:13
Joined
May 7, 2009
Messages
19,169
What do you mean "show value in combobox"?
 

comep

Registered User.
Local time
Today, 08:13
Joined
Mar 4, 2017
Messages
20
The program should be work as below :-

- Retrieve all Users ID in combobox where the Users ID is a field on the database.
- Choose User ID from combobox after that the values which related to that user ID should be listed in text box .

Hope this clarify my enquiry
 

Users who are viewing this thread

Top Bottom